Implements
and Extends are two keywords that provide a mechanism to inherit attributes and
behavior to a class in Java programming language, they are used for two
different purposes. Implements keyword is used for a class to implement a
certain interface, while Extends keyword is used for a subclass to extend from
a super class. When a class implements an interface, that class needs to implement
all the methods defined in the interface, but when a subclass extends a super
class, it may or may not override the methods included in the parent class.
Finally, another key difference between Implements and Extends is that, a class
can implement multiple interfaces but it can only extend from one super class
in Java. In general, usage of Implements
(interfaces) is considered more favorable compared to the usage of Extends
(inheritance), for several reasons like higher flexibility and the ability to
minimize coupling. Therefore in practice, programming to an interface is
preferred over extending from base classes.
benefits of inheritance
Inheritance can also make application code more flexible to change because classes that inherit from a common superclass can be used interchangeably. If the return type of a method is superclass
Example,
then the application can be adapted to return any class that is descended from Example.
No comments:
Post a Comment