Abstract and interface class

Please refer to What is the AbstractClass? Why it is used over ConcreteClass, Interface? Why it is used however can not create an instance of abstract class?  for why abstract classes are used(using example).

If any doubts further feel free to comment.

Answer to comment:

Exactly. Abstract classes only say what to do not how to do. Each class that extends abstract class must implement the unimplemented(methods whose body is not provided in abstract class) methods of abstract class.

  • Methods of Interfaces cannot have implementations. In other words, they can have only abstract or non-concrete methods. Abstract classes can have both abstract methods and concrete methods.
  • Interfaces can contain only final variables(by default all variables are final). There may be final variables in abstract classes.
  • Methods of interface are public by default. Methods of abstract class can have modifiers such as public, private..
  • Interface cannot provide any code at all. Abstract class can provide complete code, default code, and/or stubs that have to be overwritten.
  • Interface can extend another interface only but cannot extend any type of class. Abstract class can extend another java class, implement multiple java interfaces and extend only one abstract class.
  • Interface is absolutely abstract and cannot be instantiated. Abstract class cannot be instantiated but can be invoked if main exists.

Comments are closed.