Object Oriented Programming –
CS/CE 224/272
Nadia Nasir
Habib University, Fall 2024
Topics covered in Lab
• Static variables
• Function overloading
Object-oriented programming (or OOP)
Object-oriented programming (or OOP) is a paradigm or pattern of programming
whereby the solution to a programming problem is modelled as a collection of
collaborating objects.
An object is an entity that possesses both state (or properties or attributes) and
behaviour.
An example of an object is a car.
A car has attributes (e.g., colour, size, weight, fuel capacity, number of passengers, etc.).
A car has behaviour represented by its methods (e.g., start engine, turn left/right,
accelerate, stop, turn on wipers, etc.).
A class is a special kind of object that’s used as a template for creating instances of
itself. Think of it like a cookie cutter that produces cookies (or objects).
OOP Concepts
There are four main OOP concepts. These are:
Abstraction
Encapsulation
Inheritance
Polymorphism
These are also called as four pillars of Object Oriented Programming.
Abstraction
Abstract means a concept or an Idea which is
not associated with any particular instance.
Using abstract class/Interface we express the
intent of the class rather than the actual
implementation.
• In a way, one class should not know the inner details of
another in order to use it, just knowing the interfaces should
be good enough.
Encapsulation
Encapsulation enables a class to hide the inner details of how it works from the outside
world.
With Encapsulation, a class can change the way it works internally and as long as those
internals are not visible to the rest of the system, those changes will have no effect on
how the class is interacted with.
Example: Keep the body of the car the same, but change the engine
Encapsulation
Encapsulation is the mechanism of hiding of data implementation by restricting access
to public methods.
Instance variables are kept private and accessor methods are made public to achieve
this.
Inheritance
A class can inherit the attributes and behaviour
of another class (its parent).
Inheritances expresses “is-a” and/or “has-a”
relationship between two objects.
We call the inheriting class a subclass or a child
class. The original class is often called
the parent.
Polymorphism
It means one name many forms. It is
further of two types — static and
dynamic.
It is closely related to inheritance as it
allows a child class to modify or customize
that behaviour (i.e., its methods) of
parent class for itself.
• Static polymorphism is achieved using method overloading and dynamic
polymorphism using method overriding.
• We can write a code that works on the superclass, and it will work with any
subclass type as well.