An abstract class can have shared state or functionality. An interface is only a promise to provide the state or functionality. A good abstract class will reduce the amount of code that has to be rewritten because it‘s functionality or state can be shared. The interface has no defined information to be shared.
- Abstract class can have constants, members, defined methods and methods without body, while interface can only have constants and methods without body.
- Interface can not have methods body, while abstract class can.
- All methods in interface are defined as public, while in abstract class can be any visibility.
- Classes implementing an interface must implement all its methods, while extending an abstract class only has to implement its abstract methods.
- Child classes can define abstract methods with same or less restrictive visibility, while a class implementing an interface must define with the same visibility, which is public.
- Classes can implement mutiple interfaces but only extends one class(abstract or concrete). Interfaces can also extend several interfaces.
时间: 2024-10-25 21:04:03