Multiple Inheritance in Java ?
- In Java we can achieve multiple inheritance by using interfaces.
- A class implements multiple interfaces or an interface extends multiple interfaces is called multiple Inheritance.
Program which a class multiple interfaces:
interface First {
void method_one();
public void duplicate_method();
}
interface Two {
void method_two();
void duplicate_method();
}
public class multipleInheritance implements First, Two {
@Override
public void method_two() {
System.out.println("method_two implementation");
}
@Override
public void method_one() {
System.out.println("method_one implementation");
}
@Override
public void duplicate_method() {
System.out.println("duplicate_method implementation");
}
public static void main(String[] args) {
multipleInheritance mi = new multipleInheritance();
mi.method_one();
mi.method_two();
mi.duplicate_method();
}
}
Output:
Program which an interfaces extends multiple interfaces:
multiple interfaces |
interface interfaceOne {
public void method1();
void duplicate_method();
}
interface interfaceTwo {
public void method2();
void duplicate_method();
}
interface testInterface extends interfaceOne, interfaceTwo {
public void method3();
void duplicate_method();
}
public class multipleInterfaces implements testInterface {
@Override
public void method1() {
System.out.println("method1 implementation");
}
@Override
public void method2() {
System.out.println("method2 implementation");
}
@Override
public void method3() {
System.out.println("method3 implementation");
}
@Override
public void duplicate_method() {
System.out.println("duplicate_method");
}
public static void main(String[] args) {
multipleInterfaces mi=new multipleInterfaces();
mi.method1();
mi.method2();
mi.method3();
mi.duplicate_method();
}
}
Output:Please comment below to feedback or ask questions.
No comments:
Post a Comment
Please comment below to feedback or ask questions.