0% found this document useful (0 votes)
108 views

Object Oriented Programming CS F213: BITS Pilani

This document discusses the basic concepts of object-oriented programming including class, object, abstraction, encapsulation, inheritance, and polymorphism. It provides examples of classes like Student and Circle and how objects are instances of classes. Abstraction hides unnecessary details and shows only relevant data. Encapsulation binds data to code that manipulates it. Inheritance allows new classes to extend existing classes so subclasses inherit properties. Polymorphism allows the same method to have different meanings depending on the data type.

Uploaded by

KHUSHBOO KUMARI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Object Oriented Programming CS F213: BITS Pilani

This document discusses the basic concepts of object-oriented programming including class, object, abstraction, encapsulation, inheritance, and polymorphism. It provides examples of classes like Student and Circle and how objects are instances of classes. Abstraction hides unnecessary details and shows only relevant data. Encapsulation binds data to code that manipulates it. Inheritance allows new classes to extend existing classes so subclasses inherit properties. Polymorphism allows the same method to have different meanings depending on the data type.

Uploaded by

KHUSHBOO KUMARI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Object Oriented Programming

CS F213
J. Jennifer Ranjani
email: [email protected]
Chamber: 6121 B, NAB
BITS Pilani Consultation: Appointment by e-mail
Pilani Campus
OOP Basics
BITS Pilani
Pilani Campus
Basic OOP concepts

• Class
• Object
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism

BITS Pilani, Pilani Campus


Abstract Data Type (ADT)

• A structure that contains both data and the actions to be


performed on that data.

• 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 centre


accountBalance age radius
studentId
withdraw() area()
deposit() getName()
getId() circumference()
determineBalance()

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/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


Class/Object Example
class Student{
int id;
String name;
}
class TestStudent{
public static void main(String args[]){
//Creating object
Student s1=new Student();
//Initializing object
s1.id=253;
s1.name="Sathish";
//Printing data
System.out.println(s1.id+" "+s1.name);
}}

BITS Pilani, Pilani Campus


Data Abstraction

• Abstraction is a process where you show only “relevant”


data and “hide” unnecessary details of an object from the
user.
• It allows the creation of user defined data types, having
the properties of built in data types and more.
– Example: A car in itself is a well-defined object, which is
composed of several other smaller objects like a gearing system,
steering mechanism, engine, which are again have their own
subsystems. But for humans car is a one single object, which
can be managed by the help of its subsystems, even if their inner
details are unknown.

BITS Pilani, Pilani Campus


Abstraction - Example

class Student{
int id; Creates a data
String name;
type Student
}
// Class Student
Student s1;

BITS Pilani, Pilani Campus


Encapsulation

• Encapsulation is:
– Binding the data with the code that manipulates it.
– It keeps the data and the code safe from external interference

• All information (attributes and methods) in an object


oriented system are stored within the object/class.

• Information can be manipulated through operations


performed on the object/class – interface to the class.
Implementation is hidden from the user.

• Object support Information Hiding – Some attributes and


methods can be hidden from the user.
BITS Pilani, Pilani Campus
Encapsulation Example
class Student{
private int rollno;
String name;
void insertRecord(int r, String n){
rollno=r;
name=n;
}
void displayInformation(){System.out.println(rollno+" "+name);}
}
class TestStudent{
public static void main(String args[]){
Student s1=new Student();
s1.insertRecord(111,"Karan");
s1.displayInformation();
}
}
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
• 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

You might also like