Monday 16 May 2022

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

Both abstract classes and interfaces are used to define abstract methods in Java, which are methods without a body. However, there are some differences between the two:

  1. Abstract classes can have both abstract and non-abstract methods, while interfaces can only have abstract methods.

  2. An abstract class can have instance variables, but an interface cannot.

  3. An abstract class can be extended using the "extends" keyword, while an interface can be implemented using the "implements" keyword.

  4. A class can only extend one abstract class, but it can implement multiple interfaces.

  5. Abstract classes are useful when you want to create a common behavior for a group of related classes, while interfaces are useful when you want to define a contract that classes must adhere to.

No comments:

Post a Comment

what is chain constructor in java

  In Java, chain constructor refers to a technique where one constructor calls another constructor within the same class. This is achieved b...