Aug 19, 2020 5 min read
Abstract Class:
An abstract class is a class that is only partially implemented by the programmer. It may contain one or more abstract methods. An abstract method is simply a function definition that serves to tell the programmer that the method must be implemented in a child class.
Notes on Abstract class:
Abstract classes that declare all their methods as abstract are not interfaces with different names. One can implement multiple interfaces, but not extend multiple classes (or abstract classes).
interface:
An interface is similar to an abstract class; indeed interfaces occupy the same namespace as classes and abstract classes. For that reason, you cannot define an interface with the same name as a class. An interface is a fully abstract class; none of its methods are implemented and instead of a class subclassing from it, it is said to implement that interface.
Rules of Interfaces:
Note on Interfaces:
Abstract class | Interface |
---|---|
It can have constants, members, method stubs (methods without a body), methods | It can only have constants and methods stubs. |
Methods and members can have public or protected visibility | Methods of interface should only be public not any other visibility |
The concept of multiple inheritances not supported | An interface can extend or a class can implement multiple other interfaces. |
Child class must implement all the abstract method of parent class when extend keyword is used. | No need of implementing methods from parent interface when interface is extending another interface |