Encapsulation, Poly, Abstraction in Java
Encapsulation, Poly, Abstraction in Java
Java
class Encapsulate {
// private variables declared
// set method for age to access
{
geekName = newName;
}
{
Encapsulate obj = new Encapsulate();
obj.setName("Harsh");
obj.setAge(19);
obj.setRoll(51);
// obj.geekName);
}
Output
Geek's name: Harsh
Geek's age: 19
Geek's roll: 51
In the above program, the class EncapsulateDemo is encapsulated as the variables are
declared as private. The get methods like getAge() , getName() , getRoll() are set as
public, these methods are used to access these variables. The setter methods like
setName(), setAge(), setRoll() are also declared as public and are used to set the values of
the variables.
Advantages of Encapsulation:
Data Hiding: The user will have no idea about the inner implementation of the
class. It will not be visible to the user how the class is storing values in the variables.
The user will only know that we are passing the values to a setter method and
variables are getting initialized with that value.
Increased Flexibility: We can make the variables of the class as read-only or
write-know that only depending on our requirement. If we wish to make the variables
read-only then we have to omit the setter methods like setName(), setAge(), etc. from
the above program or if we wish to make the variables as write-only then we have to
omit the get methods like getName(), getAge(), etc. from the above program
Reusability: Encapsulation also improves the re-usability and easy to change with
new requirements.
Testing code is easy: Encapsulated code is easy to test for unit testing.
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like
to contribute, you can also write an article using contribute.geeksforgeeks.org or mail
your article to [email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more
information about the topic discussed above.
Polymorphism in Java
Difficulty Level : Easy
Last Updated : 01 Dec, 2020
The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form.
Real life example of polymorphism: A person at the same time can have different
characteristic. Like a man at the same time is a father, a husband, an employee. So the
same person posses different behavior in different situations. This is called
polymorphism.
Polymorphism is considered one of the important features of Object-Oriented
Programming. Polymorphism allows us to perform a single action in different ways. In
other words, polymorphism allows you to define one interface and have multiple
implementations. The word “poly” means many and “morphs” means forms, So it means
many forms.
In Java polymorphism is mainly divided into two types:
Compile time Polymorphism
Runtime Polymorphism
1. Compile-time polymorphism: It is also known as static polymorphism. This type of
polymorphism is achieved by function overloading or operator overloading. But Java
doesn’t support the Operator Overloading.
Method Overloading: When there are multiple functions with same name but different
parameters then these functions are said to be overloaded. Functions can be overloaded
by change in number of arguments or/and change in type of arguments.
Example: By using different types of arguments
// Java program for Method overloading
class MultiplyFun {
{
return a * b;
}
{
return a * b;
}
class Main {
System.out.println(MultiplyFun.Multiply(2, 4));
System.out.println(MultiplyFun.Multiply(5.5, 6.3));
}
Output:
8
34.65
Example 2: By using different numbers of arguments
class MultiplyFun {
{
return a * b;
}
{
return a * b * c;
}
class Main {
{
System.out.println(MultiplyFun.Multiply(2, 4));
System.out.println(MultiplyFun.Multiply(2, 7, 3));
}
Output:
8
42
2. Runtime polymorphism: It is also known as Dynamic Method Dispatch. It is a process
in which a function call to the overridden method is resolved at Runtime. This type of
polymorphism is achieved by Method Overriding.
Method overriding, on the other hand, occurs when a derived class has a definition for
one of the member functions of the base class. That base function is said to
be overridden.
Example:
class Parent {
void Print()
{
System.out.println("parent class");
}
void Print()
{
System.out.println("subclass1");
}
}
void Print()
{
System.out.println("subclass2");
}
class TestPolymorphism3 {
{
Parent a;
a.Print();
}
Output:
subclass1
subclass2
Abstraction in Java
Difficulty Level : Easy
Last Updated : 17 May, 2021
Data Abstraction is the property by virtue of which only the essential details are
displayed to the user. The trivial or the non-essentials units are not displayed to
the user. Ex: A car is viewed as a car rather than its individual components.
Data Abstraction may also be defined as the process of identifying only the
required characteristics of an object ignoring the irrelevant details. The
properties and behaviors of an object differentiate it from other objects of similar
type and also help in classifying/grouping the objects.
Consider a real-life example of a man driving a car. The man only knows that
pressing the accelerators will increase the speed of car or applying brakes will
stop the car, but he does not know about how on pressing the accelerator the
speed is actually increasing, he does not know about the inner mechanism of
the car or the implementation of the accelerator, brakes, etc in the car. This is
what abstraction is.
In java, abstraction is achieved by interfaces and abstract classes. We can
achieve 100% abstraction using interfaces.
Abstract classes and Abstract methods :
// concept of Abstraction
String color;
{
this.color = color;
}
double radius;
{
super(color);
System.out.println("Circle constructor called");
this.radius = radius;
}
{
}
{
}
double length;
double width;
double width)
{
super(color);
this.length = length;
this.width = width;
}
{
}
}
public class Test {
{
System.out.println(s1.toString());
System.out.println(s2.toString());
}
Output
Shape constructor called
Circle constructor called
Shape constructor called
Rectangle constructor called
Circle color is Redand area is : 15.205308443374602
Rectangle color is Yellowand area is : 8.0
Encapsulation vs Data Abstraction
1. Encapsulation is data hiding(information hiding) while Abstraction is detail
hiding(implementation hiding).
2. While encapsulation groups together data and methods that act upon the
data, data abstraction deals with exposing the interface to the user and
hiding the details of implementation.
Advantages of Abstraction
1. It reduces the complexity of viewing the things.
2. Avoids code duplication and increases reusability.
3. Helps to increase security of an application or program as only important
details are provided to the user.