Frage im Vorstellungsgespräch bei CHRISTUS Health

Explain the difference between an abstract class and an interface in Java.

Antwort im Vorstellungsgespräch

Anonym

3. Juni 2024

An abstract class can have both abstract methods (methods without a body) and concrete methods (methods with a body), whereas an interface can only have abstract methods (though from Java 8 onwards, interfaces can also have default and static methods with a body). An abstract class can have instance variables, constructors, and can provide implementations for some methods, while an interface cannot have instance variables or constructors and is meant purely for defining a contract that implementing classes must follow. A class can extend only one abstract class but can implement multiple interfaces.