Multiple Inheritance in Java
- Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit properties of more than one parent class.
- The problem occurs when there exists methods with the same signature in both the superclasses and subclass.
- On calling the method, the compiler cannot determine which class method to be called and even on calling which class method gets priority.
class Mother {
void relation() {
System.out.println("I am mother of this child...");
}
}
class Father {
void relation() {
System.out.println("I am father of this child...");
}
}
class MultipleInheritanceWithClass extends Mother,Father{ // Trying to achieve Multiple Inheritance
public static void main(String[] arr){
Mother obj1 = new Mother();
obj1.relation();
Father obj2 = new Father();
obj2.relation();
}
}
Output :
MultipleInheritanceWithClass.java:11: error: '{' expected
class Main extends Mother, Father{ // Multiple Inheritance
^
1 error
In case both the implemented interfaces contain default methods with the same method signature, the implementing class should explicitly specify which default method is to be used, or it should override the default method.
super keyword is used to call the default method of the interface.
interface Mother {
default void relation() { // Default method
System.out.println("I am mother of this child...");
}
}
interface Father {
static void relation() { // Static method
System.out.println("I am father of this child...");
}
}
class MultipleInheritanceWithInterface implements Mother, Father {
public void relation() { // Overriding the default relation method
Mother.super.relation(); // Calling a non-static method using super keyword
Father.relation(); // Calling a static method
}
public static void main(String[] arr) {
MultipleInheritanceWithInterface obj = new MultipleInheritanceWithInterface();
obj.relation();
}
}
Output :
I am mother of this child...
I am father of this child...
Multiple inheritance can also be achieved using interfaces having public methods by default. Also a class can implement multiple interfaces and extend a class at the same time.
interface Camera {
void pic();
void video();
}
interface Songs {
void gaana();
void spotify();
}
class Device implements Camera, Songs { // Multiple Inheritance
public void pic() {
System.out.println("Taking your pic...");
}
public void video() {
System.out.println("Starting your video recording...");
}
public void gaana() {
System.out.println("Playing songs on Gaana...");
}
public void spotify() {
System.out.println("Playing songs on Spotify...");
}
}
abstract class Phone {
void call() {
System.out.println("Calling...");
}
void ring() {
System.out.println("Ringing...");
}
abstract void game();
}
class SmartPhone extends Phone implements Camera, Songs { // Multiple inheritance and extending a class at same time
void game() {
System.out.println("Here are some games...\n1. Classic Snake\n2. Space Defense\n3. Bounce Classic");
}
public void pic() {
System.out.println("Taking your pic...");
}
public void video() {
System.out.println("Starting your video recording...");
}
public void gaana() {
System.out.println("Playing songs on Gaana...");
}
public void spotify() {
System.out.println("Playing songs on Spotify...");
}
}
class MultipleInheritance {
public static void main(String[] arr) {
System.out.println("MY SMARTPHONE ->");
SmartPhone sp = new SmartPhone();
sp.call();
sp.ring();
sp.game();
sp.pic();
sp.video();
sp.gaana();
sp.spotify();
System.out.println("\nMY DEVICE ->");
Device dev = new Device();
dev.pic();
dev.video();
dev.gaana();
dev.spotify();
}
}
Output :
MY SMARTPHONE ->
Calling...
Ringing...
Here are some games...
1. Classic Snake
2. Space Defense
3. Bounce Classic
Taking your pic...
Starting your video recording...
Playing songs on Gaana...
Playing songs on Spotify...
MY DEVICE ->
Taking your pic...
Starting your video recording...
Playing songs on Gaana...
Playing songs on Spotify...
Inheritance in interface
- Just like, a class can extend another class, an interface can also extend other interface.
- We can also extend one or more interfaces to a single interface, but we can't extend two or more classes in a single class.
interface GrandParent {
void grandParent();
void grandParentAge(int x);
}
interface Parent extends GrandParent { // Inheritance in interface
void parent();
void parentAge(int y);
}
class Child implements Parent {
public void grandParent() {
System.out.println("This is a method declared inside the GrandParent interface.");
}
public void grandParentAge(int x) {
System.out.println("This is a method declared inside the GrandParent interface with value " + x);
}
public void parent() {
System.out.println("This is a method declared inside the Parent interface.");
}
public void parentAge(int y) {
System.out.println("This is a method declared inside the GrandParent interface with value " + y);
}
void child() {
System.out.println("This is a method declared inside the Child class.");
}
void childAge(int z) {
System.out.println("This is a method declared inside the Child class with value " + z);
}
}
class InheritanceInInterface {
public static void main(String[] arr) {
Child obj = new Child();
obj.grandParent();
obj.grandParentAge(30);
obj.parent();
obj.parentAge(20);
obj.child();
obj.childAge(10);
}
}
Output :
This is a method declared inside the GrandParent interface.
This is a method declared inside the GrandParent interface with value 30
This is a method declared inside the Parent interface.
This is a method declared inside the GrandParent interface with value 20
This is a method declared inside the Child class.
This is a method declared inside the Child class with value 10
Comments
Post a Comment