Abstraction In Java

Abstraction is nothing but a process of hiding the implementation and showing only the functionality. When we are sending an SMS, first we have to type the message and have to click the send button to send the SMS but we don’t know the internal processing of sending the SMS. Abstraction can be achieved in the following way.

  • Abstract class (0 to 100%)
  • Interface (100%)

Abstract class

A class that declared an Abstract keyword know as an abstract class. The abstract class gives 0 to 100% of abstraction. An Abstract class is something which is incomplete and you cannot create an instance of it. If you want to create an instance then we need to make it complete or concrete by extending it. An abstract class cannot be instantiated but they can be sub classed. Abstract classes are used to provide a template or design for concrete subclasses down the inheritance tree.

An abstract method is a method that is declared without an implementation, like this

abstract void printName(String firstName);

If class contain any abstract method then the class must be declared as abstract or else it shows compile time error.

Syntax :

  • An abstract class may contain an abstract method or concrete method.
  • If any class contain one abstract method then the class must be declared as abstract
  • If class declare as abstract then it cannot be instantiated.
  • If you want to instantiate then we need inherit it from another class and have to give the body to all the abstract method. Then we can instantiate sub class.
  • After inheriting if you do not give the body of the abstract method then the sub class must also declare as abstract.

Time for action :

As above we can see an abstract class vehicle that contains two abstract methods. It’s not possible to instantiate class Vehicle.

As above class, Volvo is inherited to Vehicle class and given the body to all abstract method. Now we can instantiate the class Vehicle.

Disadvantages of using abstract class are not able use multiple inheritance. It means if class extended any abstract class then we cannot extend any other class. Multiple inheritance problems are solved in the interface.

Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.

When to use,abstract class or interface?
  • An abstract class may contain the abstract method or concrete method.
    • When you want partial pieces for your design (for reusability).
    • An abstract class is good if you think you will plan on using inheritance since it provides a common base class implementation to derived classes.
    • If you want to declare a non-public member because, in an interface, all method is public.
    • If you think to add a new method in future then abstract class is the better choice.
    • You want to share code among several closely related classes
  • Consider using Interface
    • The interface is good for when API will not change for a while.
    • The interface is good for multiple inheritance.
    • You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
    • Interface consumes very little CPU, because it’s not a class, just a bunch of names, and therefore there is no expensive look-up to do.

Summary:

  • An abstract is a keyword in java.
  • You cannot create an instance of Abstract class.
  • If class contain any abstract method then the class must be abstract.
  • You cannot declare keyword as abstract only method and class would be abstract.
  • If a class extends an abstract class or interface then it has to provide an implementation of all abstract methods or else class declares as abstract.
  • An abstract method doesn’t have a body.

Related Posts:-

1 Comment

Leave a Reply

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

Show Buttons
Hide Buttons