• Register

Inheritance is an significant pillar OOP. It is the tool in Java by which one class is permissible to receive the features (fields and functions) of a new class. There are 2 chief keywords, “extends” and “implements” which are used in Java for inheritance.

Extends: The extends keyword is used to specify that the class which is being well-defined is resulting from the base class using inheritance. So fundamentally, extends keyword is used to extend the features of the parental class to the child.

Extends vs Implements in Java

Example:


class 1 { 
    public void methodOne1() 
    { 
  
        // Some features
    } 
} 
  
class Two2 extends One1 { 
  
    public static void main(String args[]) 
    { 
        Two2 t1 = new Two2(); 
  
        // invoke the function one
        // of the above class 
        T1.methodOne1(); 
    } 
} 

Implements: In Java, the implements keyword is used to instrument an interface. An interface is a superior type of class which implements a comprehensive abstraction and only comprise abstract functions.

Example:

// Describing an interf
interface One1 { 
    public void methodOne1(); 
} 
  
// Describing 2 interface 
interface Two2 { 
    public void methodTwo2(); 
} 
  
// Imple the two2 interfaces 
class Three implements One1, Two2 { 
    public void methodOne1() 
    { 
  
        // Imple of the function
    } 
  
    public void methodTwo2() 
    { 
  
        // Imple of the function
    } 
} 

Reminder: A class can inherit a class and can implement any quantity of interf instantaneously.

Example:

// Describing the interface 
interface One1 { 
  
    // Abstract function 
    void methodOne1(); 
} 
  
// Describing a class 
class Two2 { 
  
    // Describing a function 
    public void methodTwo2() 
    { 
    } 
} 
  
class Three extends Two2 implements One1 { 
  
    public void methodOne1() 
    { 
  
        // Implementation of the method 
    } 
} 

 

 

 

 

 

 

2,760 points
7 4