Abstract class is also a class which is declared as abstract and this cannot be instantiated like normal java class even though it can be sub classed.

That means we cannot create new objects using abstract class but we can create sub classes of abstract class.
Abstract method is just a method without an implementation of the method. Example is below.

abstract void call();

And if any class includes abstract method then that class must be an abstract method.

Public abstract class PhoneBooth{
            abstract void call();

Moreover if a class needs to be implemented by an interface then that class should implement all methods in interface. But if that class does not need to implement all methods in interface then that class should be declared as an Abstract class.
Further information an abstract class can have methods with or without implementations. But we cannot have them in interfaces. All methods should be declared without any implementations in interfaces. Go through interfaces article before you read different between abstract class and interface.
When to use Abstract Classes

  1. If you need to share code with other classes which are related to a class.
  2. When you feel there are set of classes which are having same functionalities and then can be derive from similar kind of root class. Then better to create abstract class and extend so called classes from that abstract class.
  3. When you feel that object behavior is not final and it can be customized. Then you have to user non-static or non-final fields.

Compare interface with Abstract Class

Abstract Class Interface
Can have methods with or without implementations. Cannot have method implementations
Can declare fields as non-static and final. All fields are by default public, static and final
Can declare  public, protected and private concrete method All methods are by default public
Can be extended by only one class but can be implemented by multiple interfaces.

Leave a Reply

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

WP Facebook Auto Publish Powered By :