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:

abstract class AbstractExample {}

Example of Abstract Method:

abstract void run();

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.

abstract class Machine {
	abstract void turnsOn();
}


class Desktop extends Machine{
	void turnsOn(){           //Implementing the abstract method 
		System.out.println("Starts a Desktop");
	}

	public static void main(String[] args) {
		Machine D = new Desktop();
		D.turnsOn();
	}
}
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.
abstract class Machine {
	Machine(){   // Constructor
		System.out.println("Machine constructor");
	}
	abstract void turnsOn();  // Abstract method
	
	void makesNoice(){        //Non-abstract method
		System.out.println("Machine makes noice");
	}
}


class Desktop extends Machine{
	void turnsOn(){           //Implementing the abstract method 
		System.out.println("Starts a Desktop");
	}

	public static void main(String[] args) {
		Machine D = new Desktop();
		D.turnsOn();
		D.makesNoice();
	}
}
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.

interface Machine {
	void turnsOn();  // Abstract method
	void makesNoice();
}

class Desktop implements Machine{
	
	// All abstract method have to be implemented
	public void turnsOn(){           //Implementing the abstract method 
		System.out.println("Starts a Desktop");
	}
	
	public void makesNoice() {   //Implementing the abstract method 
		System.out.println("Desktop making noice");
	}

	public static void main(String[] args) {
		Machine D = new Desktop();
		D.turnsOn();
		D.makesNoice();
	}
}
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.

interface Machine {
	void turnsOn();  // Abstract method
}

interface Table{
	void keepOnTable();
}

class Desktop implements Machine,Table{
	
	// All abstract method have to be implemented
	public void turnsOn(){           //Implementing the abstract method of Machine interface
		System.out.println("Starts a Desktop");
	}
	
	public void keepOnTable() {   //Implementing the abstract method of Table interface
		System.out.println("Keep desktop on the table");	
	}


	public static void main(String[] args) {
		Desktop D = new Desktop();
		D.turnsOn();
		D.keepOnTable();
	}
}
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:

package com.test;

interface Machine {
	void turnsOn();  // Abstract method
}

interface Table extends Machine{    // Interface extends interface
	void keepOnTable();
}

class Desktop implements Table{   // Only table interface is implemented , it takes method of Machine as well
	
	// All abstract method have to be implemented
	public void turnsOn(){           //Implementing the abstract method of Machine interface
		System.out.println("Starts a Desktop");
	}
	
	public void keepOnTable() {   //Implementing the abstract method of Table interface
		System.out.println("Keep desktop on the table");	
	}

	public static void main(String[] args) {
		Desktop D = new Desktop();
		D.turnsOn();
		D.keepOnTable();
	}
}

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.

interface Machine {
	void turnsOn();  // Abstract method
	interface Table{
		void keepOnTable();
	}
}

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.

//marker interface
public interface Serializable{  
}
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.

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

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 *