Abstract class and Interface

In the previous post, we have seen Polymorphism which is one of the concept of Object Oriented Programming. Before moving to explanation of Abstract class and Interface , let’s understand the Abstraction first.

What is Abstraction in java?

Abstraction is nothing but hiding the internal details and showing only the functionality to the user.As an example you can think of a TV. We watch our favorite channels but we are not bothered about its internal working. Its functionality is only visible to us.

So, we can say that abstraction shows us what it does instead of how it does. Abstraction can be achieved in the following ways mentioned below.

  1. Abstract Classes (Partial Abstraction or say 0 to 100%)
  2. Interface (Full Abstraction or say 100%)

Now, we have understood what is abstraction. It is time to move on to understand Abstract classes and interfaces.

First, let’s see what is Abstract Classes.

Abstract Classes:

Abstract class has the following features.

  • The class is declared with abstract keyword.
  • Abstract class can not be instantiated. e.g. A a = new A();// not possible
  • It can have one or more abstract method. A abstract method is which has empty body.
  • Another class has to implement all the abstract method of abstract class by using extends keyword.

Example of Abstract class:

Example of Abstract Method:

Example to understand the Abstraction using Abstract class and method:

There is an abstract class called Machine which has an abstract method. Another class Desktop is extending parent class Machine.

Output: Starts a Desktop

Abstract Class which has constructor, abstract  and non- abstract methods:

Note: If there is any abstract method in the class , then class has to be declared as abstract.
Output:
Machine constructor
Starts a Desktop
Machine makes noice

When to use abstract class?

  • Abstract classes or method are declared as abstract when two or more subclasses are expected to fulfill a similar role through different implementation.
  • When there is a requirement to declare a class which hold both abstract and non-abstract methods.

Another way to achieve the abstraction id via Interface.

Interfaces:

Interfaces have the following features.

  • Interfaces are used to achieve fully abstraction i.e. 100 % abstraction.
  • Interfaces have method body without any implementation.
  • Multiple inheritance is possible through Interfaces.
  • Interfaces can not be instantiated.
  • Interfaces also represents IS-A relationship.
  • It can have constant, static methods and nested types.
  • Interfaces can be implemented by the classes using implements keyword and extended by another interface using extends keyword.
  • By default, it is assumed to add public and abstract keywords before the interface method and public, static and final keywords before data members.

Let’s understand these points in details.

Example of interface which is having abstract methods and those abstract methods are implemented by child class.

Output:
Starts a Desktop
Desktop making noice

Compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members:

Interface in JavaInterfaces can be implemented by the classes using implements keyword and extended by another interface using extends keyword:

Interface helps to achieve multiple inheritance as shown in the below figure.

Interface extends Interface

A class can implement multiple interface as shown in the below example. That’s why achieves multiple inheritance.

Output:
Starts a Desktop
Keep desktop on the table
Note: Multiple inheritance is supported by classes in java, but it can be achieved through interfaces.

Example of interface extends another interface:

Output:
Starts a Desktop
Keep desktop on the table

Interface can be nested:

One interface can be placed in another interface known as nested interface.

Marker or Tagged Interface:

An interface which does not have any thing like method, data member etc. known as marker interfacee.g. Serializable, Cloneable etc.

Ask Question
If you have any question, you can go to menu ‘Features -> Q&A forum-> Ask Question’.Select the desired category and post your question.
Print Friendly, PDF & Email
Polymorphism in Java
Encapsulation in java
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

1 Response

  1. August 15, 2015

    […] Webdriver is tool or framework etc. But in simple term , we can say WebDriver is  nothing but an interface. Selenium WebDriver ,also known as Selenium 2.0 has been designed to overcome of the limitation of […]

Leave a Reply

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