1.1 Study Materials Object Oriented Programming Abstraction, Objects, Classes, Encapsulation, Inheritance, Polymorphism
1.1 Study Materials Object Oriented Programming Abstraction, Objects, Classes, Encapsulation, Inheritance, Polymorphism
20CSPC301
OBJECT ORIENTED PROGRAMMING
(Common to CSE, IT, CSBS)
UNIT No. I
UNIT No.
1
OOP INTRODUCTION
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT, CSBS)
Object
⮚ Object means a real word entity or physical as well as logical entity such
as pen, chair, table etc.
⮚ Any entity that has state and behavior is known as an object.
Class
⮚ Car, bike, truck these all belong to vehicle class. These Objects also have
different states and behaviors. For Example cars have state - color, name, model,
speed, Mileage. as we say behaviors - distance travelled.
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT, CSBS)
Inheritance
Inheritance can be defined as the procedure or mechanism of acquiring all
the properties and behavior of one class to another, i.e., acquiring the properties and
behavior of a child class from the parent class.
Polymorphism
⮚ The object of a class can be created by using the new keyword in Java
Programming language.
⮚ Car, bike, truck these all belong to vehicle class. These Objects also have
different states and behaviors. For Example cars have state - color, name, model,
speed, Mileage. as we say behaviors - distance travelled.
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT, CSBS)
⮚ State
⮚ Behavior
⮚ Identity
Class
A class contains:
⮚ Car, bike, truck these all belong to vehicle class. These Objects also have
different states and behaviors. For Example cars have state - color, name, model,
speed, Mileage. as we say behaviors - distance travelled.
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT, CSBS)
⮚ Data Member
⮚ Method
⮚ Constructor
⮚ Block
⮚ Class and Interface
Vehicle class
⮚ Car, bike, truck these all belong to vehicle class. These Objects also have
different states and behaviors. For Example cars have state - color, name, model,
speed, Mileage. as we say behaviors - distance travelled.
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT, CSBS)
Class is a container
1 which collection of object is an instance of class
⮚ Car, bike, truck these all belong to vehicle class. These Objects also have
different states and behaviors. For Example cars have state - color, name, model,
speed, Mileage. as we say behaviors - distance travelled.
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT, CSBS)
class Class_Name
{
data member 1;
data member 2;
method 2;
method N;
Class Employee
{
int eid; // data member (or instance variable) String ename; // data
⮚ Car, bike, truck these all belong to vehicle class. These Objects also have
different states and behaviors. For Example cars have state - color, name, model,
speed, Mileage. as we say behaviors - distance travelled.
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT, CSBS)
ename="Hitesh";
System.out.println("Name: "+e.ename);
}
}
Output:
Name: Hitesh
Abstraction
⮚ Abstraction is the concept of exposing only the required essential characteristics
and behavior with respect to a context.
⮚ Hiding of data is known as data abstraction/Information Hiding. In object
oriented programming language this is implemented automatically while writing the code in
the form of class and object.
Abstraction shows only important things to the user and hides the internal details, for
example, when we ride a bike, we only know about how to ride bikes, but did not know
about how it works?
Encapsulation
⮚ Encapsulation is a process of wrapping of data and methods in a single unit is called
encapsulation.
⮚ Encapsulation is achieved in java language by class concept.
⮚ Combining state and behavior in a single container is known as encapsulation. In java
language encapsulation can be achieved using class keyword, state represents
declaration of variables on attributes and behavior represents operations in terms of
method.
⮚ Car, bike, truck these all belong to vehicle class. These Objects also have
different states and behaviors. For Example cars have state - color, name, model,
speed, Mileage. as we say behaviors - distance travelled.
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT, CSBS)
Advantage of Encapsulation
⮚ The main advantage of using encapsulation is to secure the data from other methods,
when we make a data private then these data only use within the class, but these data
are not accessible outside the class.
Real life example of Encapsulation in Java
⮚ The common example of encapsulation is capsule. In capsule all medicine are
encapsulated inside capsule.
⮚ Car, bike, truck these all belong to vehicle class. These Objects also have
different states and behaviors. For Example cars have state - color, name, model,
speed, Mileage. as we say behaviors - distance travelled.
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT, CSBS)
Inheritance
⮚ The process of obtaining the data members and methods from one class to another
class is known as inheritance. It is one of the fundamental features of object-oriented
programming.
⮚ Types of inheritance in java: single, multilevel and hierarchical inheritance. Multiple
and hybrid inheritance is supported through interface only.
Important points
⮚ In the inheritance the class which is given data members and methods is known as
⮚ Car, bike, truck these all belong to vehicle class. These Objects also have
different states and behaviors. For Example cars have state - color, name, model,
speed, Mileage. as we say behaviors - distance travelled.
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT, CSBS)
⮚ Car, bike, truck these all belong to vehicle class. These Objects also have
different states and behaviors. For Example cars have state - color, name, model,
speed, Mileage. as we say behaviors - distance travelled.