Professional Documents
Culture Documents
(Advanced)
What is Polymorphism?
The derivation of the word Polymorphism is from two different Greek words- poly and morphs. “Poly”
means numerous, and “Morphs” means forms. So, polymorphism means innumerable forms. Polymorphism,
therefore, is one of the most significant features of Object-Oriented Programming.
Method Overloading: This allows us to have more than one method having the same name, if the parameters of
methods are different in number, sequence and data types of parameters.
Runtime Polymorphism
It is also known as Dynamic Method Dispatch. Dynamic polymorphism is a process in which a call to an
overridden method is resolved at runtime, that's why it is called runtime polymorphism.
Method overriding: Declaring a method in sub class which is already present in parent class is known as
method overriding. Overriding is done so that a child class can give its own implementation to a method which
is already provided by the parent class.
Encapsulation
Encapsulation in Java is a process of wrapping code and data together into a single unit
It is the mechanism that binds together code and the data it manipulates.
It is a protective shield that prevents the data from being accessed by the code outside this shield.
Advantages of Encapsulation
To achieve a lesser degree of encapsulation in Java, you can use modifiers like “protected” or “public”. With
encapsulation, developers can change one part of the code easily without affecting other.
1) Getter and Setter in Java are two conventional methods used to retrieve and update values of a
variable. They are mainly used to create, modify, delete and view the variable values. The setter method
is used for updating values and the getter method is used for reading or retrieving the values. They are
also known as an accessor and mutator.
In above example all the three data members (or data fields) are private(see: Access Modifiers in Java) which
cannot be accessed directly. These fields can be accessed via public methods only.
Fields empName, ssn and empAge are made hidden data fields using encapsulation technique of OOPs.
Abstraction in Java
An Abstraction is a process of exposing all the necessary details and hiding the rest. In Java, Data
Abstraction is defined as the process of reducing the object to its essence so that only the necessary
characteristics are exposed to the users.
Abstraction defines an object in terms of its properties (attributes), behavior (methods), and interfaces (means of
communicating with other objects).
Here, you can see that an Owner is interested in details like Car description, service history, etc; Garage
Personnel are interested in details like License, work description, bill, owner, etc; and Registration
Office interested in details like vehicle identification number, current owner, license plate, etc.
It means each application identifies the details that are important to it.
Abstraction can be seen as the technique of filtering out the unnecessary details of an object so that there remain
only the useful characteristics that define it. Abstraction focuses on the perceived behavior of the entity. It
provides an external view of the entity.
package com.techvidvan.abstraction;
//parent class
abstract class BaseClass
{
//abstract method
abstract public void show1();
//concrete method
public void show2()
{
System.out.println("Concrete method of parent class");
}
}
//child class
class ChildClass extends BaseClass
{
// Must Override this method while extending the Parent class
public void show1()
{
System.out.println("Overriding the abstract method of the parent class");
}
//Overriding concrete method is not compulsory
public voidshow2()
{
System.out.println("Overriding concrete method of the parent class");
}
}
public class AbstractionDemo
{
public static void main(String[] args)
{
/* we can't create object of the parent class hence we are creating object of the child class */
ChildClass obj = new ChildClass();
obj.show1();
obj.show 2();
}
}
1. Data abstraction
Data abstraction is the most common type of abstraction in which we create complex data types such
as HashMap or HashSet, and hide its implementation details from the users and display only the meaningful
operations to interact with the data type.
The benefit of this approach solves the performance issues and improves the implementation over time. Any
changes which occur while improving the performance, do not reflect on the code present on the client-side.
2. Control abstraction
Control abstraction is the process of determining all such statements which are similar and repeat over many
times and expose them as a single unit of work. We normally use this type of abstraction is when we want to
create a function to perform any given task.