Latest :

List Of Difference Between Abstract Class And Interface | Tutorial

Differences between Abstarct class and Interface complete tutorial with suitable examples and sample outputs. In case if you need more information about the difference between Abstract class and Interface leave a comment here.

An abstract class can have concrete methods, but an interface cannot have.

Example 1:

Example 2:

An abstract class can consist both abstract methods and concrete methods but the interface does not consist of any concrete methods

An Abstract class can have a constructor, but an interface cannot.

Variables in an abstract class can be normal or final, but in an interface, the variables should always be final. So they must be initialized when declared.

Example:

Variables in an abstract class can be public, protected, normal or private. But variables in an interface are always public. If we don’t specify any, then the system will treat it as public only by default.

Example:

Variables in an abstract class can be instant or static, but in an interface, they are static by default.

Example:

An abstract class can implement an interface, but an interface cannot implement any other interface. An abstract class can implement multiple interfaces.

Example:

An abstract class cannot extend multiple abstract classes. An interface can extend multiple interfaces. An abstract class cannot extend multiple classes also. It means multiple inheritances are not allowed with abstract classes but allowed with interfaces.

Example:

When an abstract class extends another abstract class, the abstract class can implement the methods of the parent abstract class or leave it without implementing. But an interface can never implement anything.

When we want to implement some mechanism and leave the remaining to child classes then abstract classes are preferred. When we want all the implementation in the child classes only then interfaces are used.

Similarities

  • Both can have abstract methods.
  • The object cannot be created for both.
  • Reference can be created for both.
  • Generally, the reference is used to refer to the object of a child class.

When a class in extending an abstract class, that class should override all methods of the abstract class. Similarly, when a class is implementing an interface, that class should override all methods of the interface.

In both the cases, if the class does not override all the methods, then the class should be declared as abstract.

An abstract class can be extended by any number of classes and an interface can be implemented by any number of classes.

x

Check Also

Convert String To Date In Java – JavaTutoring

Convert String to Date In Java – Here we cover the different ways to convert ...