A note on Abstraction in Java – Day 13

Published by Maneet Srivastav on

Abstraction is a process of hiding the implementation details and showing only functionality to the user. Another way, it shows only important things to the user and hides the internal details for example sending SMS, you just type the text and send the message. You don’t know the internal processing about the message delivery.

Abstraction lets you focus on what the object does instead of how it does it.

Ways to Achieve Abstraction

There are two ways to achieve abstraction in java

  1. Abstract class (0 to 100%) : A class that is declared with abstract keyword, is known as abstract class in java. It can have abstract and non-abstract methods (method with body). It needs to be extended and its method implemented. It cannot be instantiated.
  2. Interface (100%) : There can be only abstract methods in the java interface not method body. It is used to achieve abstraction and multiple inheritance in Java.

Interface in Java

An interface in java is a blueprint of a class. It has static constants and abstract methods. The interface in java is a mechanism to achieve abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve abstraction and multiple inheritance in Java.

Java Interface also represents IS-A relationship. It cannot be instantiated just like abstract class. 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.

Understanding the Relationship Between Classes and Interfaces

As shown below, a class extends another class, an interface extends another interface but a class implements an 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.

Abstract class Interface
1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can have default and static methods also.
2) Abstract class doesn’t support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
4) Abstract class can provide the implementation of interface. Interface can’t provide the implementation of abstract class.
5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
6) Example:
public abstract class Shape{
public abstract void draw();
}
Example:
public interface Drawable{
void draw();
}

Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves fully abstraction (100%).

 

CONNECT WITH THE AUTHOR ON LINKEDIN.

VISIT OUR WEBSITE BRIGHTERBEES FOR INFORMATION ABOUT JAVA.

TO GET FULL BLOG SERIES OF JAVA click here.


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY CONNECT WITH US