Difference between extends and implements keyword

 

  • Using extends key word we get properties of one structure to another structure.
  • Using implements keyword we provide bodies for function definitions inside a structure (interface).
  • In simple terms, extends means getting and implements means giving.
  • Therefore functionality of implements is suit opposite to that of extends.
  • Hence implements are against inheritance.

 

Example:
Father             Son             Child-1            Father is super class
Child-2            Son is sub class
Child-3            Child 1, 2, 3 are interfaces

Son has to extend from father and can implement to all children. This is the reason why, we can extend only one class but can implement any number of interfaces.

 

Example:

Class B extends D implements   Xyz, Mno

{

Public void functionX () {=}
Public void functionY () {=}
Public void functionM () {=}
Public void functionN () {=}

Public static void main (string args [ ])

{
B b1 = new B ();
b1. Function X ();
b1. Function Y ();
b1. Function M ();
b1. Function N ();

}
}

Defining bodies for the definitions in the interface fun M () and fun N () are the definitions of the interface Mno.

 

Example: 

If there is a common definition in two interfaces and the two interfaces are implemented by the same class.

Interface Xyz
{
Public void functionX ();
Public void functionY ();
}

Interface Ijk
{

Public void functionI ();
Public void functionX ();
}

Class Idemo1 implements xyz, Ijk
{

Public void functionX () {=}
Public void functionY () {=}
Public void functionI() {=}
Public static void main (string args [ ])
{
Idemo1 d1 = new Idemo1 ();
d1. FunctionX ();
d1. FunctionY ();
d1. FunctionI ();
}
}

In this above program, Idemo1 implements Xyz and Ijk. FunctionX () is defined both in Xyz and Ijk. But we need not define function X() two times in our class as a part of defining bodies for function definitive’s present in the interface.

 

Extends and Implements Keyword Homework Help