Materi Pertemuan 2 - OOP 2

You might also like

You are on page 1of 19

#2

Review OOP Basic


Object Oriented Programming 2

Puguh Jayadi, S.Kom., M.Kom.


https://www.linkedin.com/in/puguhjayadi

PGRI Madiun University


Course Outline
1. Definition
2. Advantage
3. Basic Concept
4. Discuss

PGRI Madiun University 2


Definition
• Object Oriented Programming (OOP) is a programming
paradigm that focuses on the concept of objects and
classes.

• It models real-world objects and their interactions to


design and develop software applications.

• It enables developers to create reusable and


maintainable code, improve code organization, and
promote better software design.

• The popular programming languages that support OOP


are Java, Python, C++, PHP, Dart and more.

PGRI Madiun University 3


Advantage
• Reusability: generated code can be reused

• Extensibility: programmers can create new methods or change existing ones as desired without having to
create code from scratch,

• Maintainability: code that has been made is easier to manage if the application is made on a large scale
which allows for errors in its development, this can be overcome with OOP because OOP programming
already uses the concept of modularity

PGRI Madiun University 4


OOP Paradigm
Human Class

Attribute: id, name, specialist


Human Table
id name address
001 Abi Madiun
002 Ani Magetan Object
003 Adi Ngawi

Database from MySQL, Oracle, Postgree


What’s the Method ?
Walk, Breathe, Eat

Universitas PGRI Madiun 5


Basic Concept: Class, Object
Class: blueprint or template for creating objects

Example: Human, Car, Product

• Adi
Object: is an instance or detail of a class • Ani
• It has a unique value according to the class property and can • Abi
perform methods from the class.
Human Class Object
Example:
• Adi, Ani, Abi is object form Human Class
• Avanza, Inova, Supra is object from Car Class,
• Table, Chair, Door from Product Class

PGRI Madiun University 6


Implementation in Python class_object.py
class Human: # create object
mhs1 = Human("Abu", "Madiun", "Islam")
# public attribute mhs2 = Human("Ani", "Madiun", "Katholik")
name = None
# protected attribute # print object
_address = None print(mhs1.name)
# protected attribute
__religion = None

# constructor of class
def __init__(self, name, address, religion):

# attribute
self.name = name
self.address = address
self.religion = religion

PGRI Madiun University 7


Basic Concept: Attribute, Method
Attribute: a property or characteristic of an object.
• It describes a feature or aspect of the object that defines its data.
Example:
• Human must have a name, address
• Car must have name, wheels, an engine, a steering • Adi
• Products must have name, sizes, prices, manufacturers • Ani
• Abi
Human Class Object
Method: a specific behavior or operation that can be performed by
the object.
 name
 address
Example:  walk
• Human Class can walk, breathe, eat  breathe
Attribute
 eat
• Car Class can start, brake, increase speed
Method
• Product Class can be made, disassembled, sold

PGRI Madiun University 8


Implementation in Python method.py
class Human: # create object
mhs1 = Human("Abu", "Madiun", "Islam")
# public attribute mhs2 = Human("Ani", "Madiun", "Katholik")
name = None
# protected attribute # call method
_address = None mhs1.walk()
# protected attribute
__religion = None

# constructor of class
def __init__(self, name, address, religion):

# attribute
self.name = name
self.address = address
self.religion = religion

def walk(self):
print("I'm "+self.name+", I can walk")

PGRI Madiun University 9


Basic Concept: Inheritance
Inheritance: a way to create a class by inheriting from another class, so that the derived class can have the
same capabilities as the referenced class

Example:
• Human is Parent Class
• Lecturer is Child Class from Human Class • Adi
• Ani
• Lecturer Class is inheritance from the Human Class • Abi
Lecturer Class Object

 name  name  walk


 address  address  breathe
Human Class  nidn  eat
 walk  specialist  teach
Attribute
 breathe Attribute  research
 eat  serve
Method Method

PGRI Madiun University 10


Implementation in Python inheritance.py
# super class Human # sub class Lecturer
class Human: class Lecturer(Human):

# public attribute # constructor


name = None pass

# protected attribute def research(self):


_address = None print(self.name+" as a lecturer must do research")

# protected attribute # create object


__religion = None lec1 = Lecturer("Abu", "Madiun", "Islam")

# constructor of class # print object with attribute


def __init__(self, name, address, religion): print(lec1._address) # Madiun
# print(lec1.__religion) # ?
# attribute
self.name = name # call method from super class
self._address = address lec1.walk()
self.__religion = religion
# call method from sub class
def walk(self): lec1.research()
print("I'm "+self.name+", I can walk")

PGRI Madiun University 11


Basic Concept: Polymorphism
Polymorphism : A class has many forms according to their needs (type data, methods, parameters)

Example:
• Human can be Lecturer and Teacher
• Lecturer has walk method and Teacher has walk method

Lecturer Class

 name
 address
Human Class
Attribute  walk
 breathe
 eat Teacher Class
Method

PGRI Madiun University 12


Implementation in Python polymorphism.py
# super class Human # create object from Human
class Human(): human1 = Human()

def display_info(self): # call human method


print("I'm Human") human1.display_info()

# sub class Lecturer # create object from Lecturer


class Lecturer(Human): human1 = Lecturer()
pass
# call human method
def display_info(self): human1.display_info()
print("I'm Lecturer")

# sub class Teacher # create object from Teacher


class Teacher(Human): human1 = Teacher()
pass
# call human method
def display_info(self): human1.display_info()
print("I'm Teacher")

PGRI Madiun University 13


Basic Concept: Encapsulation
Encapsulation : A way so that the attributes or methods of a class cannot be accessed from outside. This is
relate with access modifier (public, private, protected)

Example:
• Human must have a name, address
• Actually humans also have an age attribute,
• it's just that people don't really like being asked about age because it's a private matter

 name
 address
 age Human Class
Attribute  walk
 breathe
 eat
Method

PGRI Madiun University 14


Implementation in Python encapsulation.py
# super class Human # create object
class Human: lec1 = Lecturer()

# constructor of class # setter


def __init__(self): lec1.set_religion("Islam")

# attribute # getter
self.name = None print(lec1.get_religion())
self._address = None
self.__religion = None

def set_religion(self, x):


self.__religion = x

def get_religion(self):
return self.__religion

# sub class Lecturer


class Lecturer(Human):
pass

PGRI Madiun University 15


Basic Concept: Abstraction
Abstraction : allows us to hide core details and only show the needed information.

Example:
• Every human has a soul
• And every soul has a heart and how he believes

 name
 address
 age Human Class
Attribute  walk
Soul Class  breathe
 eat
 heart  believe
Method
Attribute Method

PGRI Madiun University 16


Implementation in Python abstraction.py
# Abstract Base Classes
from abc import ABC, abstractmethod # create object
lec1 = Lecturer()
# super class Human
class Human(ABC): # call abstract method
lec1.display_info()
# decorator abstract method
@abstractmethod
def display_info(self):
pass

# sub class Lecturer


class Lecturer(Human):
pass

def display_info(self):
print("I'm Lecturer")

PGRI Madiun University 17


Discuss

Any Question?

PGRI Madiun University 18


Next Chapter
• We'll learn more about interface

Assignment

• Explain the many examples of the basic OOP concepts we have studied.

PGRI Madiun University 19

You might also like