04 Java Abstraction
04 Java Abstraction
PROGRAMMING
SUBJECT DESCRIPTION: JAVA ENTERPRISE EDITION PROGRAMMING
SUBJECT CODE: CS220
A method which is declared as abstract and does not have implementation is known
as an abstract method.
In this example, Shape is the abstract class, and its implementation is provided by
the Rectangle and Circle classes.
Mostly, we don't know about the implementation class (which is hidden to the end
user), and an object of the implementation class is provided by the factory
method.
In this example, if you create the instance of Rectangle class, draw() method of
Rectangle class will be invoked.
Another example of Abstract class in java
Abstract class having constructor, data member
and methods
An abstract class can have a data member, abstract method, method body (non-
abstract method), constructor, and even main() method.
There are mainly three reasons to use interface. They are given below.
• It is used to achieve abstraction.
• By interface, we can support the functionality of multiple inheritance.
• It can be used to achieve loose coupling.
How to declare an interface?
An interface is declared by using the interface keyword. It provides total abstraction;
means all the methods in an interface are declared with the empty body, and all the
fields are public, static and final by default. A class that implements an interface
must implement all the methods declared in the interface.
The relationship between classes and interfaces
As shown in the figure given below, a class extends another class, an interface
extends another interface, but a class implements an interface.
Java Interface Example
In this example, the Printable interface has only one method, and its
implementation is provided in the A6 class.
Java Interface Example: Drawable
In this example, the Drawable interface has only one method. Its implementation is
provided by Rectangle and Circle classes. In a real scenario, an interface is defined
by someone else, but its implementation is provided by different implementation
providers. Moreover, it is used by someone else. The implementation part is hidden
by the user who uses the interface.
Java Interface Example: Bank
Let's see another example of java interface which provides the implementation of
Bank interface.
Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple
interfaces, it is known as multiple inheritance.
Interface inheritance
A class implements an interface, but one interface extends another interface.
Java 8 Default Method in Interface
Since Java 8, we can have method body in interface. But we need to make it default
method. Let's see an example:
Java 8 Static Method in Interface
Since Java 8, we can have static method in interface. Let's see an example:
Nested Interface in Java
Note: An interface can have another interface which is known as a nested interface.
We will learn it in detail in the nested classes chapter. For example:
Difference between abstract class and interface
Abstract class and interface both are used to achieve abstraction where we can
declare the abstract methods. Abstract class and interface both can't be
instantiated.
But there are many differences between abstract class and interface that are given
below.
Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves fully abstraction (100%).
Example of abstract class and interface in Java
Let's see a simple example where we are using interface and abstract class both.