Abstraction in Java

Abstraction in Java

If you have started to learn Java then I believe you must have somewhere come across term called object-oriented programming or OOPs concept. Now there are four pillars in Oops i.e., Abstraction, polymorphism, encapsulation and inheritance. In this article we will discuss about one of the four pillars of Oops i.e., Abstraction.

Abstraction basically is the art of hiding implementation details from user and provide the user what they want. Let’s try to understand with real world example. Most of us are quite fond of owning a car. When we go to place order for the car we are really not interested to understand very fine details of implementation of each and every component insider the car engine, Gear box etc., we leave those technical details and implementation for manufacturing engineers and mechanics to understand we are simply interested in the car so does the manufacturing company. They are interested to exactly provide us what we want and hide the fine implementation details from us. Likewise, there are tons of real-world examples where abstraction is in play whether smartphone you are using or smart television you are watching all have implemented abstraction in one way or the other.

Coming back to Java programming or any object-oriented programming to be more precise same principle follows code’s implementation details are hidden and only the necessary functionality is provided to the user. There are two ways to achieve abstraction in java: –

  1. By using interfaces
  2. By using abstract classes

Interfaces- Consider a television remote which only contains functionality to operate a television and it doesn’t serve any other purpose besides operating the television. You won’t be able to operate a refrigerator with a television remote. Here remote acts as an interface between you and the television. It contains all the necessary functionalities which you require while hiding the implementation details from you. In java Interfaces are similar to classes except they contain empty methods and can contain variables also. By empty methods it means that they don’t provide any implementation details and its for the classes or clients to provide the necessary implementation details for that method (or methods) when they implement the interface.

Syntax :-

public interface XYZ {

public void method ();

}

Example :

public interface TelevisionRemote {
public void turnOnTelevision();

public void turnOffTelevision();

}

A java class can just use the implements keyword to implement the interface and provide the implementation of the methods of the interface.

Example: –

public class Television implements TelevisionRemote{

   @Override

   public void turnOnTelevision(){

   //method implementation details

   }

   @Override

   public  void turnOffTelevision(){

   //method implementation details

   }

}

Interfaces provide contract specification or sets of rules for the classes implementing them. They set rules for classes and tell them what to do and not to do. In case the class does not provide implementation for all the methods of the interface then the class must be declared abstract. We will cover abstract classes later. They provide total abstraction which means that all the methods are empty and field variables are public static and final by default. Interfaces serve several features: –

1.They provide total abstraction.

2.They help to achieve what we call multiple inheritance as java doesn’t support multiple inheritance, but you can implement several interfaces in one class and thus it helps to achieve multiple inheritance.

3.They help to achieve loose coupling in design patterns implementation.

Abstract classes

Abstract classes are just like normal java class except they use keyword abstract in front of class declaration and method declaration.

Syntax: –

public abstract class XYZ {

public abstract methodName();

}

For example :

public abstract class Automobile {

   public abstract void engine();

   public void  gearBoxGearOne(){

     //method implementation

   }

}

Abstract classes are created using abstract keyword and they may have or may not have method implementation. If a method is declared abstract then its implementation has to be provided by the class extending the abstract class. We can have abstract class without abstract method as well as they can contain final methods also. A class that extends the abstract class is a child class for that abstract class and has to provide implementation for the abstract method declared in the abstract class.

Example :-

public class Car extends Automobile{

   @Override

   public void engine(){

     //Method implementation

   }

}

Now question must be arising why we have interfaces and abstract classes. There are few key differences worth noticing : –

1.Interfaces are implicitly abstract and cannot have implementations. Abstract classes can have method implementations.

2.Variables of interfaces are final by default. Abstract classes may or may not have final variable.

3.Interface methods are public whereas abstract classes can provide all types of access modifiers for its members i.e., public, protected, private.

4.Interface can extend interface only while classes can implement multiple interfaces and can extend one class only.

Thus, both abstract classes and interfaces are used to achieve abstraction and both have their own importance while designing a java solution but most preferable choice for most developers is to use interfaces as they provide complete abstraction. I hope this article helps to clear your doubts regarding abstraction.