You are on page 1of 6

OBJECT ORIENTED PROGRAMMING

(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.

What is polymorphism in programming?


Polymorphism is the capability of a method to do different things based on the object that it is acting upon. In
other words, polymorphism allows you define one interface and have multiple implementations.

Types of polymorphism in java


There are two types of polymorphism in java:
1) Static Polymorphism also known as compile time polymorphism
2) Dynamic Polymorphism also known as runtime polymorphism

Compile time Polymorphism


Polymorphism that is resolved during compiler time is known as static polymorphism. Method overloading is an
example of compile time polymorphism.

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

Encapsulation simply means binding object state(fields) and behavior(methods) together.

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

1 | Compile by MISS FARYAL-ADIL


1) Data Hiding: it is a way of restricting the access of our data members by hiding the implementation
details. Encapsulation also provides a way for 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.
2) Increased Flexibility: We can make the variables of the class read-only or write-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 write-only then
we have to omit the get methods like getName(), getAge(), etc. from the above program
3) Reusability: Encapsulation also improves the re-usability and is easy to change with new requirements.
4) Testing code is easy: Encapsulated code is easy to test for unit testing.

Data Hiding in Java


Data Hiding in Java is hiding the variables of a class from other classes. It can only be accessed through the
method of their current class. It hides the implementation details from the users. But more than data hiding, it is
meant for better management or grouping of related data.

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.

Example of Encapsulation in Java


How to implement encapsulation in java:
1) Make the instance variables private so that they cannot be accessed directly from outside the class.
You can only set and get values of these variables through the methods of the class.
2) Have getter and setter methods in the class to set and get the values of the fields.
2) class EncapsulationDemo{
3) private int ssn;
4) private String empName;
5) private int empAge;
6)
7) //Getter and Setter methods
8) public int getEmpSSN(){
9) return ssn;
10) }
11)
12) public String getEmpName(){
13) return empName;
14) }
15)
16) public int getEmpAge(){
17) return empAge;
18) }
19)
20) public void setEmpAge(int newValue){
21) empAge = newValue;
22) }
23)
24) public void setEmpName(String newValue){
25) empName = newValue;

2 | Compile by MISS FARYAL-ADIL


26) }
27)
28) public void setEmpSSN(int newValue){
29) ssn = newValue;
30) }
31) }
32) public class EncapsTest{
33) public static void main(String args[]){
34) EncapsulationDemo obj = new EncapsulationDemo();
35) obj.setEmpName("Mario");
36) obj.setEmpAge(32);
37) obj.setEmpSSN(112233);
38) System.out.println("Employee Name: " + obj.getEmpName());
39) System.out.println("Employee SSN: " + obj.getEmpSSN());
40) System.out.println("Employee Age: " + obj.getEmpAge());
41) }
42) }
43) Output:
44) Employee Name: Mario
45) Employee SSN: 112233
46) Employee Age: 32

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).

3 | Compile by MISS FARYAL-ADIL


Real-life Example for Java Abstraction

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.

How to Achieve Abstraction in Java Program?


In Java, we can achieve Data Abstraction using Abstract classes and interfaces.
Interfaces allow 100% abstraction (complete abstraction). Interfaces allow you to abstract the implementation
completely.
Abstract classes allow 0 to 100% abstraction (partial to complete abstraction) because abstract classes can
contain concrete methods that have the implementation which results in a partial abstraction.

Abstract Classes in Java


 An Abstract class is a class whose objects can’t be created. An Abstract class is created through the use of
the abstract keyword. It is used to represent a concept.
 An abstract class can have abstract methods (methods without body) as well as non-abstract methods or
concrete methods (methods with the body). A non-abstract class cannot have abstract methods.
 The class has to be declared as abstract if it contains at least one abstract method.
 An abstract class does not allow you to create objects of its type. In this case, we can only use the objects
of its subclass.
 Using an abstract class, we can achieve 0 to 100% abstraction.
 There is always a default constructor in an abstract class, it can also have a parameterized constructor.
 The abstract class can also contain final and static methods.

4 | Compile by MISS FARYAL-ADIL


Abstract Methods in Java
 Abstract methods are methods with no implementation and without a method body. They do not contain
any method statement.
 An abstract method is declared with an abstract keyword.
 The declaration of an abstract method must end with a semicolon ;
 The child classes which inherit the abstract class must provide the implementation of these inherited
abstract methods.

Example of Abstract class and Abstract methods

 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();
 }
 }

5 | Compile by MISS FARYAL-ADIL


Output:
Overriding abstract method of the parent class
Overriding concrete method of the parent class

Types of Abstraction in Java


Abstraction can be of two types:

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.

6 | Compile by MISS FARYAL-ADIL

You might also like