Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Object Oriented Programming

- Method of structuring a program by bundling related properties and behaviors into individual objects
- Programming paradigm that provides a means of structuring programs so that properties and behaviors are
bundled into individual objects
- Built around well objects.
- Approach to problem solving computations carried out by objects.

Procedural
- relies on procedure, contains a series of computational steps to be carried out.
- List of instructions that executes one after the other

Python Classes/Objects

- A class is like an object constructor, or a “blueprint” for creating object.


- To create a class, use the keyword class:

__init__() function
- Executed when the class is being initiated.
- Assign values to object properties, or other operations that are necessary to do when the object is being
created.
- Called automatically every time the class is being used to create a new object.

__str__() function
- Controls what should be returned when the class object is represented as a string.
- If it is not set, the string representation of the object is returned.

Object Methods
- Object contains methods.
- Methods in objects are functions that belong to the object.
- is a program component that knows how to perform specific actions.

2 characteristics
o Attributes (ex. Name, title, experience)
o Behaviors (ex. Working, on-leave, underperformed)

Self parameter
- Is a reference to the current instance of the class, and is used to access variables that belongs to the class.
- It does not have to be named self, you can call it whatever you like, but it has to be the first parameter of any
function.

Class – blueprint for how something should be defined


Instance – object built from class contains real data

Attributes created in __init__() are called instance attributes

Instantiating – creating a new object from a class


Memory Address – indicates the address/location where the object is stored in your computer’s memory.

Instance method
- defined inside a class
- First parameter is self

Accessor method
- Used to access the state of the object
- The data hidden in the object can be accessed it cannot change the state

Mutator
- Modify the state of the object

Class Variable
- Inside class/class header, outside instance method before the constructor __init__()
Instance Variable
- Value of variable varies from object to object. Every object has its own copy of the instance attribute.

Class header – class student:


Constructor __init__ tapos ang parameter/argument kay ang sa sulod sa init nga tapad sa self
Instance variable ang sa baba ng init
Self is referring to the object

OOP Concepts:
• Encapsulation
• Inheritance
• Polymorphism
• Abstraction

Inheritance
- Allows us to define a class that inherits all the methods and properties from another class.
- One class takes on the attributes and methods or one another

Abstraction
- Simplifying complex reality by modelling classes

Polymorphism
- is a technique to make different things by using only one function
- Process of using an operator or function in different ways

Encapsulation
- Is a way to secure or to hide some part of your class attributes/methods
- prevents data from direct modification
- Hides the implementation details

Types of Inheritance
▪ Single
▪ Multilevel
▪ Heararchal
▪ Multiple
▪ Hybrid

Python Inheritance

Parent class
- Is the class being inherited from, also called base class.

Child class
- Is the class that inherits from another class, also called derived class.

Any class can be a parent class, so the syntax is the same as creating any other class
To create a class that inherits the functionality from another class, send the parent class as parameter when creating
child class.

Note:
• Use the pass keyword when you do not want to add any other properties or any methods to the class.
• The __init__() function is called automatically every time the class is being used to create a new object.
• When you add the __init__() function, the child class will not longer inherit the parent’s __init__() function.
The child’s __init__() function overrides the inheritance of the parent’s __init__() function.
• To keep the inheritance of the parent’s init function, add a call to the parent’s init function.
• If you add a method in the child class with the same name as a function in the parent class, the inheritance
of the parent method will be overridden.
super() function
- Will make the child class inherit all the methods and property from its parent. By using super, you do not
have to use the name of the parent element, it will automatically inherit the methods and properties from its
parents.

Three types of Encapsulation in OOP:

Public – when your object can be reached from any point of code
Protected – when you have an access to certain attribute/method only inside package (group of files)
Private – when only members of the class can reach thus attribute/method
- Implemented by double (__) before the name

There are two ways to make polymorphism in our code:


➢ Overriding – when a child of class inherits its methods and extends its functionality.
For example: we will override “function” that basically returns doubled value now will return squared
value.
➢ Overloading – when you have a method in your class with some set a parameters. In case of usage/filling
different parameters you will have different results.

Basic Terminologies:
Object – It’s the instance of class/ it’s the working entity of a class.
Class – It is a template or blue print about the capability of what an object can do.
Method – the behaviors of a class. It tells what a method.
Instance – an specific realization of any object

The creation of instance is called instantiation.


Mainclass(m) = new Mainclass():
➢ The “m” – usually called variable but as it’s actually holding object reference then it’s called instance of
class.
➢ The “new Mainclass();” – new object has been created in memory and it’s reference has been returned.

OOP use attributes:


✓ Class variable
o Inside class/class header, outside instance method
o The constructor is the “__init__”
✓ Abstraction
o Holding unnecessary details
o Can have multiple implementation
o Building complex system by splitting the complexity level by level

Advantages of abstraction:
1. We can separate the things that can be grouped to another type
2. Frequently changing properties and methods can be grouped to a separate type
3. Simplifies the representation of the domain models

Two types of Abstraction


o Control Abstraction – simplified and unnecessary execution details are removed. Without it, a
programmer would need to specify all the register/binary-level steps
o Data Abstraction – refers to defining the behavior of data structure. The way to create complex data
types and exposing only meaningful operation to interact with data types.

Summary
Abstraction are ideas, not specific events.
To abstract something is to move away from its implementation
Abstraction can be used to reorganize code
OOP is entirely dependent on the abstraction
Abstraction is the practice of breaking large problem into smaller components
Encapsulation in abstraction
- Encapsulation is about packaging of the class whereas abstraction is more about what the class does for
you.

• Private – accessible within the class


• Public – accessible anywhere from outside the class
• Protected – accessible within the class and its sub-classes

Interfaces in Java
- Similar to class but it is not class

Abstract method in Java


- Abstract class can be a parent

Java
- Specify the data type before you declare a value

➢ Object class – parent of all classes


➢ No constructors – super class and implicit/explicit

Dynamic binding/polymorphism

✓ Immutable object – limitable/you can’t change the object. Don’t ptovide the mutator methods to declare the
data field private
✓ Abstraction – separate implementation from the use
✓ Encapsulation – details are hidden, almost similar

Composition/Aggregation
- Has a relationship
- A class/object

An aggregation is a form of association where the relation of Association can be considered the containing class
'owning' the contained class. The lifetime of that relationship cannot be defined. 'Owning' can be determined as a
single-direction Association.

Method Overloading

def sum(a,b):
return a + b
➢ Mao ni siya sa python.

def sum(a,b,c):
return a + b + c
➢ In other programming language, pede ni siya.

Additional Info from the Internet:


Method Overloading is defining two or more methods with the same name but different parameters. Python
does not support method overloading. Method Overriding is redefining a parent class method in the derived
class. Overriding r equires inheritance for implementation.
In Java, with method overloading, multiple methods can have the same name with different parameters:
Multiple methods can have the same name as long as the number and/or type of parameters are different.
Overloading and overriding both fall under the concept of polymorphism, which is one of the essential
features of OOP.

OVERLOADING OVERRIDING
It is performed at compile time. It is performed at runtime.
It is carried out within a class. It is carried out with two classes having an IS-A relationship
between them.
Parameters do not remain the same in The parameter must remain the same in case of overriding.
case of overloading.
You can perform overloading on static You cannot perform overriding on static methods.
methods.
Overloaded methods use static binding. Overridden methods use dynamic binding.

Abstract Classes and Methods


❖ Data abstraction is the process of hiding certain details and showing only essential information to the user.
❖ Abstraction can be achieved with either abstract classes or interfaces (which you will learn more about in the
next chapter).
❖ The abstract keyword is a non-access modifier, used for classes and methods:
❖ Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited
from another class).
❖ Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided
by the subclass (inherited from).

Why And When To Use Abstract Classes and Methods?


❖ To achieve security - hide certain details and only show the important details of an object.
❖ Note: Abstraction can also be achieved with Interfaces, which you will learn more about in the next chapter.

Interfaces
❖ Another way to achieve abstraction in Java, is with interfaces.
❖ An interface is a completely "abstract class" that is used to group related methods with empty bodies:

Notes on Interfaces:
❖ Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to
create an "Animal" object in the MyMainClass)
❖ Interface methods do not have a body - the body is provided by the "implement" class
❖ On implementation of an interface, you must override all of its methods
❖ Interface methods are by default abstract and public
❖ Interface attributes are by default public, static and final
❖ An interface cannot contain a constructor (as it cannot be used to create objects)

Why And When To Use Interfaces?


1) To achieve security - hide certain details and only show the important details of an object (interface).
2) Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be
achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple
interfaces, separate them with a comma (see example below).

Java Polymorphism
- Polymorphism means "many forms", and it occurs when we have many classes that are related to each
other by inheritance.
- Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another
class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single
action in different ways.
- For example, think of a superclass called Animal that has a method called animalSound(). Subclasses of
Animals could be Pigs, Cats, Dogs, Birds - And they also have their own implementation of an animal sound
(the pig oinks, and the cat meows, etc.):

Why And When To Use "Inheritance" and "Polymorphism"?


- It is useful for code reusability: reuse attributes and methods of an existing class when you create a new
class.

(Good luck sa exams! Kung kinsa man nakadawat sakong notes nga gicompile from other people, from my
screenshots and from what I got from the internet, unta makapasar kas OOP! Kung ganahan kag send sakong gcash
09959016165, ganahan pako mag compile sa other subjects tas detailed! Salamat HAHAHAHAH loveyou)
Additional Information if you’re not satisfied with the info above:

➢ Abstraction in python is defined as a process of handling complexity by hiding unnecessary information from
the user.
➢ Polymorphism is taken from the Greek words Poly (many) and morphism (forms)

A class is a technique to group functions and data members and put them in a container so that they can be
accessed later by using a dot (.) operator. Objects are the basic runtime entities of object-oriented programming. It
defines the instance of a class. Objects get their variables and functions from classes, and the class we will be
creating are the templates made to create the object.

Object-Oriented Terminologies
• class: Classes are a user-defined data type that is used to encapsulate data and associated functions
together. It also helps in binding data together into a single unit.
• Data Member: A variable or memory location name that holds value to does a specific task within a program.
• Member Function: They are the functions, usually a block of a code snippet that is written to re-use it.
• Instance variable: A variable that is defined inside a method of a class.
• Function Overloading: This technique is used to assign multiple tasks to a single function, and the tasks are
performed based on the number of arguments or the type of argument the function has.
• Operator Overloading: It is the technique of assigning multiple functions/tasks to a particular operator.
• Inheritance: It is the process of acquiring the properties of one class to another, i.e., one class can acquire
the properties of another.
• Instantiation: It is the technique of creating an instance of a class.

You might also like