Object Oriented Programming
CS F213
BITS Pilani
Pilani Campus
OOP Basics
BITS Pilani
Pilani Campus
Basic OOP concepts
• Class
• Object
• Encapsulation
• Inheritance
• Polymorphism
BITS Pilani, Pilani Campus
BITS Pilani, Pilani Campus
Abstract Data Type (ADT)
• A structure that contains both data and the actions to be
performed on that data.
• Abstract Datatype is a special kind of datatype, whose
behavior is defined by a set of values (data) and set of
operations (actions)
• Class is an implementation of an Abstract Data Type.
BITS Pilani, Pilani Campus
Examples
Person Objects
Abstract Person Class
Into Attributes: Name, Age, Sex
Operations: Speak(), Listen(), Walk()
Vehicle Objects
Abstract Vehicle Class
Into Attributes: Name, Model, Color
Operations: Start(), Stop(), Accelerate()
Polygon Objects
Polygon Class
Abstract Attributes: Vertices, Border,
Into Color, FillColor
Operations: Draw(), Erase(), Move()
Figure 1.12: Objects and classes
BITS Pilani, Pilani Campus
Class
• Class is a set of attributes and operations that are
performed on the attributes.
• A blueprint from which individual objects can be
created.
• A class defines all the properties common to the object
- attributes and methods.
Student Circle
Account
accountName Name: String centre
accountBalance Age: int radius
studentId: String
withdraw() area()
deposit() getName(): String
circumference()
determineBalance() getId(): String
BITS Pilani, Pilani Campus
Objects
• Instance of the class
• Entity that has state and behavior
• Each object has an address and takes up memory
• It can communicate without knowing other object’s code
or data
BITS Pilani, Pilani Campus
Classes vs. Objects
BITS Pilani, Pilani Campus
Classes/Objects
John and Jill are
:John
Student objects of class
Student
:Jill
:circleA circleA and circleB
Circle are
:circleB objects of class
Circle
BITS Pilani, Pilani Campus
Object
• Objects have state and classes don’t.
John is an object (instance) of class Student.
name = “John”, age = 20, studentId = 1236
Jill is an object (instance) of class Student.
name = “Jill”, age = 22, studentId = 2345
circleA is an object (instance) of class Circle.
centre = (20,10), radius = 25
circleB is an object (instance) of class Circle.
centre = (0,0), radius = 10
BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
THREE OOP PRINCIPLES
Encapsulation
• Encapsulation is:
– Binding the data with the code
that manipulates it.
– It keeps the data and the code
safe from external interference
• Information can be manipulated through operations
performed on the object. Implementation is hidden from the
user.
• Object support Information Hiding/Abstraction – Some
attributes and methods can be hidden from the user.
BITS Pilani, Pilani Campus
Inheritance
BITS Pilani, Pilani Campus
Inheritance
• New data types (classes) can be defined as extensions
to previously defined types.
• Parent Class (Super Class) – Child Class (Sub Class)
• Subclass inherits properties from the parent class.
Parent
Inherited
capability
Child
BITS Pilani, Pilani Campus
Inheritance - Example
• Example
– Define Person to be a class
• A Person has attributes, such as name, age, height, gender
– Define student to be a subclass of Person
• A student has all attributes of Person, plus attributes of
his/her own ( student no, course_enrolled)
• A student inherits all attributes of Person
– Define lecturer to be a subclass of Person
• Lecturer has all attributes of Person, plus attributes of
his/her own ( staff_id, subjectID1, subjectID2)
BITS Pilani, Pilani Campus
Inheritance - Example
• Circle Class can be a subclass (inherited from ) of a
parent class - Shape
Shape
Circle Rectangle
BITS Pilani, Pilani Campus
Uses of Inheritance - Reuse
• If multiple classes have common attributes/methods,
these methods can be moved to a common class -
parent class.
• This allows reuse since the implementation is not
repeated.
BITS Pilani, Pilani Campus
Reuse-Example
Circle Rectangle
centre
centre height
radius width
area() area()
circumference() circumference()
move(newCentre) move(newCentre)
move(newCentre){
move(newCentre){
centre = newCentre;
centre = newCentre;
}
}
BITS Pilani, Pilani Campus
Reuse-Example
Shape
centre move(newCentre){
centre = newCentre
}
move(newCentre)
Circle Rectangle
height
radius width
area() area()
circumference() circumference()
BITS Pilani, Pilani Campus
Polymorphism
BITS Pilani, Pilani Campus
Polymorphism
• Polymorphic which means “many forms” has Greek
roots.
– Poly – many
– Morphos - forms.
• In OO paradigm polymorphism has many forms.
• Allow a single object, method, operator associated with
different meaning depending on the type of data passed
to it.
BITS Pilani, Pilani Campus
Polymorphism – Method Overloading
• Multiple methods can be defined with the same name,
different input arguments.
Method 1 - initialize(int a)
Method 2 - initialize(int a, int b)
• Appropriate method will be called based on the input
arguments.
initialize(2) Method 1 will be called.
initialize(2,4) Method 2 will be called.
BITS Pilani, Pilani Campus
Go to menti.com and
use code: 75 70 63 8
BITS Pilani, Pilani Campus
Query: Difference between
abstraction and encapsulation
Abstraction Encapsulation
• Hides unwanted • Treats data and method
information i.e. helps us as an entity and hides it
to ignore how certain from outside i.e. Access
operations are to these attributes and
implemented. method are restricted.
• Java implements • Java implements
abstraction using abstract encapsulation using
classes and interfaces. access modifiers.
NOTE: you will understand these difference more clearly when
inheritance, interfaces and access modifiers are dealt.
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956