CodesJava

Easy learning with example program codes

how to implement multiple inheritance in java?


We can implement multiple inheritance in java by following ways:

  • A class can implements multiple interfaces.
  • An interface can extends multiple interfaces.

Example

/**
 * This program is used to show multiple inheritance example.
 * @author CodesJava
 */
interface ShowAge{
          //This method is used to print age.
          void age(int age);
}
 
interface ShowName{
          //This method is used to print name.
          void name(String name);
}
 
public class InterfaceExample2 implements ShowAge, ShowName{
                /**
                 * This method is used to print age.
                 * @author CodesJava
                 */
                @Override
                public void age(int age) {
                        System.out.println("Age = " + age);
                }
 
                /**
                 * This method is used to print name.
                 * @author CodesJava
                 */
                @Override
                public void name(String name) {
                       System.out.println("Name = " + name); 
                }                               
 
                public static void main(String args[]){
                        //object creation
                        InterfaceExample2 obj = new InterfaceExample2();
 
                        //method call
                        obj.name("jai");
                        obj.age(26);
                }
}

Output

Name = jai
Age = 26

Java nterview questions on inheritance



Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap