The keywords extends and implements are used to inherit the features of an already existing parent block in newly created child block. Using extends keyword, a newly created class (subclass) can inherit the features of an existing class (superclass). Using implements keyword a newly created class can implement all the methods of an interface.
The primary difference between keywords extends and implements is that a class extends another class to inherit all its members whereas, a class implements an interface to inherit all it methods and implement them according to its requirement. There are some other differences between keywords extends and implements discussed in a comparison chart shown below.
Content: extends Vs implements
Comparison Charts
Basis for Comparison | Extends | Implements |
---|---|---|
Basic | The extends keyword is used to create a subclass using features of a super class. | The implements keyword is used for the implementation of an interface |
Methods | The extends keyword may not override all the methods of a super class. | The implements keyword has to implement all the methods of an implemented interface. |
Class | A class can extend only one super class. | A class can implement multiple interafaces. |
Interface | An interface can extend more than one interface. | An interface cannot implement another interface. |
Definition of extends keyword
Inheritance being an important concept in Object Oriented Programming, is achieved when a subclass extends an another superclass. A subclass is allowed to extend only one superclass. A class can never extend more than one super class in Java. When a class extends another class, it can override the methods defined in a superclass. Let us understand keyword extends with the help of an example.
class A { String s; A(String s1) { s=s1; } // display string void display() { System.out.println(+ s); } } class B extends A { String i; B(String s1, String i1) { super(s1); i = i1; } void dispaly() { super.dispaly(); /*display() of class A invoked.*/ System.out.println(+ i); } } class Override { public static void main(String args[]) { B ob = new B(Tech, Differences); ob.display(); /* display of class B invoked*/ } } /* out put */ Tech Differences
In above code, you can observe that class B has extended class A and has overridden the method display( ) defined in class A. In a similar way an interface can extend more than one interface at a time. As interface do not have to implement any method of the extended interface, hence, it is allowed to inherit multiple interfaces at a time.
Definition of implements keyword
Using the keyword implements, a class inherits all the methods in an interface. When a class implements an interface, that class has to implements all the methods of implemented interface. An interface can never implement another interface, as implements keyword promises implementation of the methods but an interface never implements methods in it, so it is impossible for an interface to implement another interface. Let us understand implements keyword with the help of an example.
interface strings{ void display(String s); } interface integer{ void show(int i); } class Demo implements strings, integer{ void show(int i){ System.Out.Println("integer value:" +i); } void display(String s){ System.Out.Println("string value:" +s); } } class Demo_main{ public static void main(string args[]){ Demo d = new Demo(); d.display("TechDifferences"); d.show(2); } } /* output */ TechDifferences 2
As in the code above you can see that the class demo implements two interfaces strings and integer and has implemented the methods declared in both the interfaces that are display() and show().
Key Differences Between extends and implements
- A class can inherit another class, or an interface inherits other interfaces using a keyword extends whereas, a class can implement an interfaces using a keyword implements.
- A subclass that extends a superclass may or may not override all the methods in a superclass. On the other hand, a class implementing an interface has to define/implement all the methods of that interface.
- A class can implement any number of an interface at the same time. On the other hand, a class can extend only one super class.
- An interface can extend any number of interfaces, but an interface can never implement any other interface as implementing means defining the methods and interface always have abstract methods so an interface can never implement another interface.
Conclusion
I conclude the discussion saying that implementing an interface makes the code more flexible than extending a class. As it avoids the restriction of inheriting only one super class in Java. Using implements keyword a class can inherit features of more than one interfaces.
Leave a Reply