112. Abstract Classes

When superclass just defines the structure of the methods without providing complete implementation of every method and the subclass overrides the abstract methods in superclass and implements them, then the superclass is called as abstract class.

superclass specifies 'abstract' type before the methods who's structure is defined and implementation is not provided. We call these methods as abstract methods.

For example: -  abstract methodname( );

The class that has at least one abstract method need to be specified as 'abstract' before the class name

abstract className
    //Body of the class

Implementation for the abstract methods in the superclass will be provided in the sub classes which overrides the abstract methods of superclass.

Lets implement this on Eclipse IDE:

1. Create superclass 'Superclass' which contains abstract method whose structure is provided but implementation is not provided as shown below and save:

2. Create subclass 'Subclass' which overrides the abstract method in 'Superclass' and provides the implementation for the overridden method in subclass as shown below and save:

3. Create another class 'AbstractDemo' to create an object for calling the abstract methods implementation in subclass as shown below:

4. Save and Run the 'AbstractDemo' class file
5. Observe that the output is displayed in the console as shown below:

Download this project:

Click here to download the project containing the 'Superclass', 'Subclass' and 'AbstractDemo' class files used in this post (You can download the project and import into Eclipse IDE on your machine)

Please comment below to feedback or ask questions.

Prevent Overriding using 'final' keyword will be explained in the next post.


man said...

What is the difference between abstract classes and interfaces?
what is the purpose of Interfaces?

Arun Motoori said...

We can achieve abstraction (i.e. hiding the complex details and showing the required details) using abstract classes and interfaces. (Abstraction example - Car Class can be abstracted by showing only the car body, speed, cost, mileage and hiding the things like engine and all the machinery used).

Coming to the difference between abstract classes and interfaces:

First difference - Both are used for abstraction, but abstract class can have both abstract and non-abstract methods, where as interface must have only abstract methods).

Second difference - Abstract class is a class while interface is a interface, means by extending abstract class you can not extend another class because Java does not support multiple inheritance but you can implement multiple inheritance via implementing interfaces in Java. i.e.

class A extends B,C is not possible as only one class can be inherited by a Class i.e. only class A or Class B can be extended at once not both. So multiple inheritance is not possible.

To achieve multiple inheritance we have to use interfaces, i.e.
class A implements B,C. So multiple inheritance is possible with interfaces as class A can implement interface B and interface C at the same time.

Usage of interface in Java:

1. Used to achieve abstraction in Java
2. Define the contract for the sub-classes to implement

Regarding second usage, I would like to provide a practical example:

Selenium is an interface and the Class like FirefoxDriver, ChromeDriver, InternetExplorerDriver, SafariDriver and AndroidDriver which implement the Selenium interface has their own implementation for the all the abstract methods in Selenium interface. So all the Classes are implementing the same interface to have the same methods across all the browsers but the methods implemented (coding of method body) will be different for FirefoxDriver (to make the automation work for Firefox Browser) and ChromeDriver (methods implemented differently to make the automation work on Chrome Browser)

I hope this answered your question.

Arun Motoori said...

Simple example for why we use interface: Interface is created to design the structure of any project. Suppose we create structure for all the activities of a bank software in an interface and use this structure for various banking softwares of different banks like HDFC, HSBC etc.

Lets say we have created a structure by creating Selenium interface using various variables and methods, all the Class implementing the interface has to follow the same structure of Selenium interface by writing the code for methods defined in the interface.

man said...

Thank you Arun

Ramit Mathur said...

Wonderful Explanation in such a simple words!!! Incredible :)