0% found this document useful (0 votes)
6 views24 pages

Lec 2

This document provides an overview of Object-Oriented Programming (OOP), covering key concepts such as classes, instances, inheritance, polymorphism, abstraction, and encapsulation. It explains how classes serve as blueprints for objects, the mechanisms of inheritance, and the importance of encapsulation through accessors and mutators. Additionally, it discusses class diagrams and the visibility of attributes in the context of OOP.

Uploaded by

shenalthegreat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views24 pages

Lec 2

This document provides an overview of Object-Oriented Programming (OOP), covering key concepts such as classes, instances, inheritance, polymorphism, abstraction, and encapsulation. It explains how classes serve as blueprints for objects, the mechanisms of inheritance, and the importance of encapsulation through accessors and mutators. Additionally, it discusses class diagrams and the visibility of attributes in the context of OOP.

Uploaded by

shenalthegreat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Lecture 2

Object Oriented Programming


DA3331 - Business Application Development
Supun Gothama
Introduction
• Object-Oriented programming is a widely used concept to
write applications
• It is an approach for modeling real-world things
• OOP uses the concept of objects and classes
• Properties and behaviors are organized into objects
Classes vs Instances
• Classes defined data structures with attributes and
behaviors
• A class serves as a blueprint for defining something, but
does not contain specific data
• An object created from a class and contains real data
Inheritance
• Inheritance is a mechanism for creating a new class that can
inherit behavior and properties from another class
• The base class is called the parent class
• The newly formed class is called a child class
• Child classes inherit all of the parent’s attributes and
methods
Inheritance

class Animal: class Dog(Animal):


def __init__(self, def __init__(self,
name): name):
self.name = name
super().__init__(name)
def run(self):
print("running!") def bark(self):
print("Woof!")
Polymorphism
• Polymorphism means having multiple forms
• The same entity (method or object) can perform different
operations in different scenarios
• Two approaches
− Method Overloading
− Method Overriding
Method Overloading
• Multiple methods in a class have the same name but
different types or numbers of parameters
• These methods are called overloaded methods
• Python does not support method overloading
Method Overloading
class Sample
{
int multiply(int a, int b)
{
return a * b;
}

int multiply(int a, int b, int c)


{
return a * b * c;
}
}
Method Overriding
• In method overriding, a child class can change a function
defined by its parent class
• The child class should have the same method name and the
same number of parameters as the parent class
• Important for extending and customizing functionality in a
child class
• Supported in Python
Method Overriding

class Animal: class Turtle(Animal):


def __init__(self, def __init__(self, name):
name): super().__init__(name)
self.name = name
def run(self):
def run(self): print("Running slowly!")
print("Running!")
Abstraction
• Abstraction is the process of hiding the internal details of a
class from outside
• Provides only the functionality of a class to outside
• Outside knows what a class does, but not how the class
does it
Encapsulation
• Encapsulation refers to the bundling of attributes and
methods inside a single class
• Wrap up the data and the code acting on the data together
as a single unit
• Prevents outer classes from accessing and changing
attributes and methods of a class
Encapsulation
• Restricts accessing properties directly and can prevent the
accidental modification of data
− Known as protected/private variables
• This also helps to achieve data hiding
Encapsulation
• Protected properties cannot be accessed outside the class
but can be accessed from within the class and its subclasses
− In Python prefix the name of the attribute by a single
underscore “_”
− Just a convention in Python
− Trust your class's user to not do anything stupid
Encapsulation
• Private properties cannot be accessed outside the class
− In Python prefix the name of the attribute by double
underscores “__”
− Python actually hides the property to outside
Encapsulation
class Base: class Derived(Base):
def __init__(self): def __init__(self):
self.normalProp = 100 super().__init__()
self._protectedProp = print(self._protectedProp)
200
self.__privateProp =
300
obj = Derived()
print(obj.__privateProp) # error
Accessors and mutators
• With encapsulation private data from outside the object is
accessed using Accessor and Mutator methods
• Also known as getter and setter methods
• Accessor methods are used to access the states of an object
− i.e, the data hidden in the object can be accessed from this
method
− However, these methods cannot change the state of the
object, it can only access the hidden data
Accessors and mutators
• Mutator methods are used to mutate/modify the state of an
object
− i.e, it alters the hidden value of the data variable
Accessors and mutators
class Car: c = Car("Toyota")
def __init__(self, brand): c.get_brand()
self.__b = brand c.set_brand("Audi")

def set_brand(self, brand):


self.__b = brand

def get_brand(self):
return self.__b
Accessors and mutators
• Python offers another approach called properties
• A method which is used for getting a value is decorated with
"@property“
• The method which has to function as the setter is decorated
with "@x.setter"
Accessors and mutators
class Car: c = Car("Toyota")
def __init__(self, brand): print(c.brand)
self.__b = brand c.brand = "Audi"

@property
def brand(self):
return self.__b

@brand.setter
def brand(self, brand):
self.__b = brand
Class Diagrams
• Class diagrams describes the set of
classes in a system
• Also shows attributes and operations
of each class
• Inheritance shown with arrows
towards parent class
Class Diagrams
• Visibility of attributes/operations
− Public attributes (+): visible to all
classes
− Private/protected attributes (-): visible
only to an instance of the class in
which they are defined
• UML notation
☑ OOP

☑ Inheritance

☑ Polymorphism

☑ Encapsulation
Summary

You might also like