Unit I
Object-Oriented Programming
Overview
OOPs or Object-Oriented Programming is a programming approach that centers on organizing a program
around its data and well-defined interfaces, with the aim of making code more closely aligned with the
real world. This is achieved by using objects in a programming language.
The main purpose of OOPs programming is to implement ideas and solve real-world problems
using classes, objects, inheritance, polymorphism, and abstraction. Some of the most
popular OOP languages include Java, C++, and Python.
OOPs can be defined as:
A modular approach where data and functions can be combined into a single unit known as an object. It
emphasizes data and security and provides the reusability of code. Using OOP, we can resemble our code
in the real world.
List of Java OOPs Concepts
The following are the concepts in OOPs :
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
What is Class in Java OOPs?
A Class is a collection of similar types of objects. It is commonly known as the 'blueprint of an object'. It
is a template that describes the kinds of state and behavior that the object of its type support.
Class is a user-defined data type. Also, it doesn't occupy any memory.
, It is composed of name, attributes, and methods. Access
Modifiers(public, private, default, protected) are used for having limited or public access to that
class so that it can be used by other programs in Java.
We create individual objects using class.
In our example, we can create a class known as "Plane" which will be its name. It consists of the
attributes/fields and methods that each plane will have.
We can have the following attributes :
plane_number
total_seats
airline_class
pilot_name
Our "Plane" class can contain the following methods to use the above attributes
seatAvailability()- this checks how many seats are available or vacant on that plane.
selectClass()- lets you choose the class in which you want to travel- eg. business class or
economy class.
In short, the "Plane" class is a blueprint that represents a plane in a flight management system and we
have to create it to represent a real-world entity.
Syntax:
public class Plane {
//attributes
//methods
}
But before diving deeper into the sea of OOPs, let us get acquainted with some terminologies.
Member Functions- The functions are written inside a class are known as member functions.
Instance variables- Variables that are declared within the scope of the class.
What is an Object in Java OOPs?
As we have discussed above, Objects are the building blocks of OOP. A Java program is mostly a
collection of objects talking to other objects by invoking each other’s method.
,At a run time, when JVM encounters the “new” keyword, it will use the appropriate class o make an
object which is an instance of that class. That object will have its state and access to all of the behavior
defined by the class.
Objects are real-world entities which have a certain state and behavior.
Objects are commonly known as an 'instance of a class'.
In our example, we created the class "Plane". So now, we will create objects from the same class.
Each object will have the attributes and methods defined in the class. If we want to create an
object of class "Plane" , we use the following syntax.
Syntax:
Plane P1 = new Plane();
So this P1 object will contain its characteristics like its number, total seats, plane class(business,
economy, etc.), and pilot. Creating another object of class "Plane" will give it all the attributes that were
defined in that class.
Objects interact with each other and share information. This technique is termed Message
Passing.
Objects are allocated memory space and address too.
The following figure illustrates the relationship between objects and classes.
The figure shows that the class Plane acts as a template to create objects which contain the attributes of
the class(Plane).
Four Pillars of OOPs in Java
The following are the basic principles of OOP in Java. They are known as the principles as the entire OOP
lays its foundation on these principles.
Inheritance
Polymorphism
Abstraction
Encapsulation
An easy way to remember OOPs principle is by remembering the sentence- I ate A
PIE.(Abstraction, Polymorphism, Inheritance, Encapsulation)
What is Inheritance in Java OOPs?
, Inheritance is a mechanism of deriving one class from another. It allows the derived class to inherit
features from a parent class, the features include (fields and methods). The parent class is also known as
the superclass or base whereas the derived class is known as a subclass or child class.
Inheritance is a property of OOPs in which member functions and data members of one class can
be used by another class.
Reusability of code can be achieved because of inheritance. If we want to create a class that
already has some common methods which we want to define within another class, we can use
the already existing class as a base class or a parent class and then inherit from it.
Hence the code can be reused as we don't need to write the same piece of code again and can
use it in the derived class or child class.
Base class- a class from which member functions and data members are used by another class.
Derived class- a class that uses the properties of the base class and can also add its properties.
In our example, we created a class "Plane" which contained attributes like plane_number, total_seats,
airline_class, and pilot_name. All the planes are of the sub-type of class "Plane" and they have common
attributes mentioned above.
So in addition planes have their distinct attributes like luxury_arrangements, food, and air hostess. So
instead of defining the common properties again, we can only define the distinct properties in the new
class and inherit the common properties from the base class.
Types of Inheritance :
Single
Multilevel
Hybrid
Hierarchical
The above image shows various inheritances and their syntax.
What is Polymorphism in Java OOPs?
As the name suggests, it means many forms. It is the ability to create a function, variable, or an
object that has more than 1 form. The concept of polymorphism is expressed by 'one interface, many
methods'. It is one of the Principles of OOPs.
Two types of polymorphism in java :
Compile-time : It is also known as static or early binding. The binding is performed during
compilation time.
Run time : It is also known as dynamic or late binding. The binding occurs during runtime,
depending on the type of object.
The two types of polymorphism are achieved in java by :