Professional Documents
Culture Documents
Compilation of Notes
Compilation of Notes
- 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
__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.
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.
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.
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
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
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
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.
Interfaces in Java
- Similar to class but it is not class
Java
- Specify the data type before you declare a value
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.
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.
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)
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.):
(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.