OOPS Core
OOPS Core
com
OOPs Notes
(Object Oriented Programming)
1
Introduction to Object Oriented Programming
What is Object oriented programming??
Object-Oriented Programming (OOP) is a methodology or paradigm for
designing software through the utilization of classes and objects. It
streamlines both software development and maintenance. OOPs consists of-
Page 2
Let’s now see above mentioned parts of classes and objects:
Constructor
A constructor is a unique method that is automatically called during the
creation of an object. Its primary purpose is to initialize the data members of
newly created objects. In C++, the constructor shares the same name as the
class or structure it belongs to.
Java: https://www.prepbytes.com/blog/cpp-programming/type-of-constructor-in-cpp/
Python: https://wiingy.com/learn/python/constructors-in-python/
Destructor
A destructor in object-oriented programming (OOP) is a special member
function of a class that is automatically called when an object is destroyed or
goes out of scope. Its primary purpose is to perform cleanup tasks, such as
Page 3
releasing resources (memory, file handles, database connections, etc.), that
were allocated during the object's lifetime.
Java: https://www.codingninjas.com/studio/library/java-destructor
Python: https://www.scaler.com/topics/destructor-in-python/
“This” keyword/pointer
The keyword "this" refers to the current instance of the class. It serves three
main purposes:
Page 4
Learn more about “this” pointer (examples using code):
C++: https://www.scaler.com/topics/cpp/this-pointer-in-cpp/
Java: https://www.programiz.com/java-programming/this-keyword
Python: In python “this” pointer is called “self”. The purpose served by “this” pointer in
C++/Java is served by “self” keyword in python.
https://www.programiz.com/article/python-self-why
Page 5
Key concepts of OOPs
There are 4 main concepts / components / pillars of OOPs.
Inheritance
Inheritance is a core feature of Object-Oriented Programming where a class
can acquire properties and behaviors from another class. It's a vital aspect of
OOP, enabling the creation of new classes based on existing ones.
When inheritance occurs, a new class, known as the "derived class" or "child
class," is formed from an existing class, referred to as the "base class" or
"parent class." This derived class inherits all the properties of the base class
without altering them, and it can introduce additional features unique to
Page 6
itself. These modifications in the derived class do not impact the base class,
allowing for specialization.
Access Modifiers
Also called visibility modes, dictate the accessibility of members (attributes
and methods) of a class in inheritance relationships. There are typically three
visibility modes:
1. Public:
Protected members are accessible within the class itself and by its
subclasses.
Subclasses can access protected members inherited from the
superclass, but they are not accessible outside the class hierarchy.
3. Private:
Private members are only accessible within the class where they are
defined.
Page 7
Inheritance does not grant access to private members in subclasses.
They cannot be accessed directly by subclasses.
Java: https://www.programiz.com/java-programming/access-modifiers
Python: https://www.studytonight.com/python/access-modifier-python
Types of Inheritance
● Single Inheritance: a class inherits properties and behaviors from only
one parent class, this means that a subclass can extend and specialize
Page 8
the functionalities of a single parent class, forming a hierarchical
relationship between the two classes.
● Multiple Inheritance: a class can inherit attributes and methods from
more than one parent class. This means that a subclass can inherit
functionalities from multiple superclasses, forming a more complex
inheritance hierarchy. This type of inheritance is not provided by Java.
● Multilevel Inheritance: a class inherits from another class, and then
another class inherits from that derived class. In other words, it
involves chaining together multiple levels of inheritance, forming a
hierarchical relationship among classes.
● Hierarchical Inheritance: refers to the process of creating multiple
classes that inherit from a single base class.
● Hybrid inheritance: inheritance mechanism that blends together the
features of simple inheritance, multiple inheritance, and hierarchical
inheritance.
Page 9
Inheritance Reference (For declaration and Examples using code):
C++: https://www.algbly.com/Tutorials/Cpp-programming/Cpp-inheritance-types.html
Java: https://www.javatpoint.com/types-of-inheritance-in-java
Python: https://www.boardinfinity.com/blog/types-of-inheritance-in-python/
Encapsulation
It refers to the bundling of data (attributes or properties) and methods
(functions or procedures) that operate on that data into a single unit,
typically a class. This unit restricts access to the data from outside
interference and misuse, and only exposes the necessary functionalities
through well-defined interfaces. It means that all the data and methods are
bound together and all the unnecessary details are hidden from the user.
Page 10
Encapsulation Reference (For declaration and Examples using code):
It is done using access modifiers in C++, Java and Python. Refer to Page 4
for references.
Polymorphism
Polymorphism, in simple terms, means "many forms." In object-oriented
programming, it refers to the ability of different objects to respond to the
same message or method call in different ways. This allows objects of
different classes to be treated as objects of a common superclass,
promoting flexibility and extensibility in code.
1. Compile-time polymorphism:
2. Run-time polymorphism:
Page 11
provides a specific implementation of a method that is already defined
in its superclass.
Java: https://www.scaler.com/topics/java/polymorphism-in-java/
Python:
https://www.toppr.com/guides/python-guide/tutorials/python-oops/polymorphism-in-pyth
on-with-examples/
Abstraction
We aim to abstract real-life problems by simplifying them and focusing on
essential aspects while omitting unnecessary details. By defining the
properties of these problems, including the affected data and identified
operations, we create a standardized model that can serve as a solution
Page 12
template for similar problems. This approach proves efficient as many
real-life problems share common properties, allowing for the reuse of
abstracted models to solve various instances of similar problems.
● Data Abstraction:
● Process Abstraction:
Page 13
Examples of process abstraction include interfaces, which define a contract
for implementing classes without specifying the implementation details, and
function prototypes or method signatures, which declare the input/output
parameters and return type of a function/method without providing its
implementation.
https://www.upgrad.com/blog/abstraction-in-java/
Python: https://www.scaler.com/topics/python/data-abstraction-in-python/
Page 14
Interface in OOPs
An interface in object-oriented programming (OOP) is a blueprint of a class
that defines a set of methods that must be implemented by any class that
implements the interface. It specifies a contract that classes must adhere to,
outlining the methods they must provide without specifying the
implementation details.
C++ does not provide the exact equivalent of Java interface. In C++,
overriding a virtual function can only be done in a derived class of the class
with the virtual function declaration, whereas in Java, the overrider for a
method in an interface can be declared in a base class.
https://www.javatpoint.com/interfaces-in-cpp
Java: https://www.programiz.com/java-programming/interfaces
Python: https://www.tutorialspoint.com/python/python_interfaces.htm
Abstract Classes
Abstract classes in object-oriented programming (OOP) are classes that
cannot be instantiated directly and are intended to serve as base classes for
other classes. They may contain abstract methods, which are methods
without a body, and/or concrete methods, which have an implementation.
Abstract classes provide a way to define a common interface or set of
behaviors that subclasses must implement, while also allowing for code
reuse and polymorphism.
Page 15
1. Abstract classes cannot be directly instantiated; they serve as
templates for other classes.
2. Abstract classes can have abstract methods, which must be
implemented by concrete subclasses.
3. They may include concrete methods with implementations that
subclasses can inherit or override.
4. Abstract classes can have member variables like regular classes.
5. They define common behaviors for subclasses, promoting code
organization and reuse.
https://www.baeldung.com/java-interface-vs-abstract-class
Python: https://www.scaler.com/topics/abstract-class-in-python/
Java: In java all public variables and functions are virtual by default and it does not have
any “virtual” keyword which exists in C++. Being virtual is a property of variables and
methods. You may read about this property and its presence in Java, but from an
interview point of view this is not important if Java is the focus.
Page 16
https://www.educba.com/virtual-function-in-java/
Python: In python all methods are virtual by default, here also no “virtual” keyword exists.
In Java pure virtual functions are abstract classes of Java. Same for python
we have abstract classes and methods that can be used to serve the
purpose of pure virtual functions.
Exceptions
Exceptions in object-oriented programming (OOP) are a mechanism for
handling unexpected or exceptional conditions that occur during the
execution of a program. Exceptions allow programmers to gracefully handle
errors or exceptional situations without abruptly terminating the program.
Java: https://www.educative.io/answers/what-are-different-types-of-exceptions-in-java
Python: https://www.programiz.com/python-programming/exceptions
Page 17
Exception Handling
Exception handling refers to the practice of managing potential errors or
unexpected situations in a program to prevent it from abruptly halting. It
involves identifying undesirable program states and defining appropriate
actions to ensure the program's smooth execution. The try-catch construct is
commonly utilized for handling exceptions, allowing developers to enclose
error-prone code within a try block and specify how to handle exceptions in
catch blocks. This approach enables programmers to anticipate and address
potential issues, enhancing the reliability and stability of their software
applications.
Java: https://www.digitalocean.com/community/tutorials/exception-handling-in-java
Python: https://www.programiz.com/python-programming/exception-handling
Page 18
Thank You!
Join Now
Page 19