Home » Core Java » Abstraction , Abstract Class and Interface in Java

Abstraction , Abstract Class and Interface in Java

Abstraction in Java  is the process which is used to hide certain details and only show the essential features of the object.Abstraction is like what object does rather than how the object does.Suppose an abstract class has a method which takes amount and return a amount  after adding tax ,vat etc.So,the user does not know here how the final amount is calculated,the user only know that he/she will get final amount.

How to achieve Abstraction :
Abstraction is achieved in 2 ways.
1 : Abstract class
2 : Interface

Abstract Class :
1 : Abstract class is a class which is declared with abstract keyword.
2 : It may or may not have abstract methods
3 : Abstract classes cannot be instantiated because they are incomplete.JVM treat a class as incomplete, if it has keyword abstract in its declaration.One will get compile time exception ,while trying to create object of abstract class.
4 : Abstract class can be sub-classed.
5 : We can not make abstract class as final.If a class is made final ,then we can not extend the class and we can not override the methods of the class.But,abstract class is used by extending it in sub class.So final and abstract are opposite of each other and can not be used together.
6 : Abstract class can have constructor in Java and this constructor will be called during constructor chaining by sub class. When sub class extends abstract class and create object ,at that time even if you don’t provide any super class constructor, compiler will add default no argument constructor in abstract class as the  first statement in any constructor implicitly calls super(), default super class constructor in Java.Constructor in abstract class can be used to initialize common variables.
7 : Abstract class can have static methods.But this method may not be useful as static method can not be overridden.So sub class can not use it.
8 : Abstract class can have both concrete and abstract methods.We should use abstract class when we know implementation of some methods and not sure of  implementation of other methods in the class.

Example : Consider an abstract class ShapeSize with abstract method perimeter() and concrete method area().The sub class Circle extends the super class ShapeSize and overrides perimeter() method.In the main class we created object of subclass and access both methods.

Program : 


Interface :

1 : Interface in java is declared using keyword interface.
2 : Interface means 100% abstraction.
3 : Interface can not have any concrete method.
4 : All methods declared inside Java Interfaces are implicitly public and abstract
5 : All variables declared inside interface is implicitly public c final variable or  constants. 
6 : In Java its possible for an interface or class  to extend multiple interfaces.
7 : We should use interface when we are not sure of  implementation of  methods in different classes which are     going to implement the interface.

Example : Consider an interface Shape with abstract method area() and variable pi .The sub class Circle implements the interface Shape and overrides area() method.In the main class we created object of subclass and access area() method.

Marker  interface :
 1 : Marker interface in Java is an interface with no field or methods.
 2 : These are used to provide necessary information to the JVM so that JVM may perform some useful operation.
 3 : For example: Serializable, Cloneable etc. 

Leave a comment

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

seventeen + seventeen =