Interface vs Abstract
Contents
Interface
- only abstract methods allowed (methods with no body)
- only constant class variables (public static final)
- allows for multiple inheritance by implementing mutliple interfaces
Abstract
- atleast one abstract method ; can have many concrete methods -> code reuse
- child class has to implement all of its abstract methods
- easier to add common methods instead of adding to all implementations
- can only inherit one abstract class
- when access modifiers are needed
Important Reasons For Using Interfaces
Info
Interfaces are used to achieve abstraction.
Designed to support dynamic method resolution at run time
It helps you to achieve loose coupling.
Allows you to separate the definition of a method from the inheritance hierarchy
Designed to support dynamic method resolution at run time
It helps you to achieve loose coupling.
Allows you to separate the definition of a method from the inheritance hierarchy
Important Reasons For Using Abstract Class
Info
Abstract classes offer default functionality for the subclasses.
Provides a template for future specific classes
Helps you to define a common interface for its subclasses
Abstract class allows code reusability.
Provides a template for future specific classes
Helps you to define a common interface for its subclasses
Abstract class allows code reusability.