1.

What is the difference between an abstract class and an interface?

Answer»

LET’s dig into the differences between an abstract class and an interface:

  • Abstract classes are classes that cannot be instantiated ie. that cannot create an object. The interface is like an abstract class because all the METHODS inside the interface are abstract methods.
  • Surprisingly, abstract classes can have both abstract and non-abstract methods but all the methods of an interface are abstract methods.
  • Since abstract classes can have both abstract and non-abstract methods, we need to use the Abstract keyword to declare abstract methods. But in the interface, there is no such need.

An abstract class has constructors while an interface ENCOMPASSES none. 

Ex.

Abstract class:

PUBLIC abstract class SHAPE{public abstract void draw();}

Interface:

public interface Paintable{void paint();}


Discussion

No Comment Found