Abstraction and Interface in Java

Abstraction in Java

Abstraction is a method to show only functionality or essential things to the user and hiding the internal part or implementation details. For example, when you send SMS to someone, you just type the message and send it but you do not know how the message got delivered to the recipient this is abstraction where internal details are hidden from you. 

Ways to achieve Abstraction

There are two ways to achieve abstraction in java- 

  1. An abstract class (0 to 100% abstraction can be achieved)
  2. Interface (100% abstraction can be achieved)

Abstract Class and Abstract Method

 “Abstract” keyword declares a abstract class.

  • An abstract class can have abstract methods(methods without body) as well as concrete methods (regular methods with the body). But, if a class has at least one abstract method, then the class must be declared abstract. 
  • A normal or non-abstract class cannot have abstract methods. 
  • An abstract class can be a subclass but cannot be instantiated. 
  • An abstract class is always used by inheriting it from another class. 
  • All of the methods of the inherited abstract class must be implemented in the derived class. 

If you want to declare a method in your class but you want the implementation of the method to be determined in the child classes you can declare that class as abstract. An abstract method must be declared by using an abstract keyword before the method name while declaring and it does not contain body but contains a method signature.

How to declare – using a semicolon (;) at the end.

Abstract class Example

1)In this example, we are printing the current time using abstraction

Abstract class Example

 

Output:

Abstract class Output

2)An abstract class with the constructor

An abstract class with the constructor

Output:

An abstract class with the constructor output

3)An abstract class without any abstract method

An abstract class without any abstract method

Output:

An abstract class without any abstract method output

4)An abstract class with a final method

An abstract class with a final method

Output:

An abstract class with a final method output

Interface

The interface in Java is similar to the class which means it can have methods and variables but the methods declared in the interface are by default abstract which means it can have method signatures but nobody. 

  • Interfaces act as a blueprint of the class focus only on what a class must do instead of how to do.
  • An Interface specifies a set of methods that the class has to implement.
  • It is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors which a class implements.
  • If you do not want to provide method bodies for all functions specified in the interface you are implementing, then you must declare that class as abstract.

You have to declare an interface, using interface keyword. It provides total abstraction which means all fields are public, static and final by default and all the programmers declare methods with an empty body and are public. A class that implement interface must implement all the methods declared in the interface. To implement an interface you must use  “implements” keyword.

Why do we use interface?

  • It provides total abstraction.
  • Since multiple inheritances can’t be accomplished in Java in case of class, but we can achieve it using interface.
  • Loose coupling can also be achieved. 
  • Interfaces implement abstraction. So you would wonder why to use interfaces when you have abstract classes?

The reason is, variables in an interface are final, public and static and your abstract classes may contain non-final variables.

// A simple interface

Player

{

   final int id = 10;

   int move();

}

To implement an interface we use keyword: implement

    To implement an interface

Output:

To implement an interface output

Implementing Multiple Interfaces

Implementing Multiple Interfaces

 

Output:

Implementing Multiple Interfaces output

 

Difference between abstract and interface

 

Abstract Interface
Type of methods Abstract class can have both abstract and concrete methods. An interface can have only abstract methods.
Final Variables An abstract class may contain non-final variables. Variables declared in a Java interface are by default final.
Type of variables Abstract class can declare variables as final, non-final, static and non-static. An interface has only static and final variables.
Implementation Abstract class can provide the implementation of an interface. An interface can’t provide the implementation of an abstract class.
Multiple inheritances An abstract class can extend another Java class and implement multiple Java interfaces. An interface can extend another Java interface only
Adding functionality While adding a new method to an abstract class we have the option of providing the default implementation which helps all the existing code work properly. While adding a new method to an Interface we have to check all the implementations of the interface and define an implementation for the new method.
Fields and Constants An abstract class can have fields and constraints defined No fields can be defined in interfaces

Hope you enjoyed reading the blog post. Let us know if you have any suggestions for the published content on the site. Feel free to contact us if you wish to register yourself in our skill upgradation courses.

For more such content subscribe to our YouTube channel – https://www.youtube.com/channel/UCcWR1IezEQynJnAETgK8g2w?sub_confirmation=1 and follow us on facebook – https://www.facebook.com/pristinetechschool

2 thoughts on “Abstraction and Interface in Java

Leave a Reply

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