interface inheritance of same method

gudii9
gudii9 used Ask the Experts™
on
6. Multiple inheritance of state is not allowed:
Remember that Java does not allow a class inherits two or more classes directly. To understand why multiple inheritance is not allowed, consider the following example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class A {
    public void methodA() {
 
    }
 
    public void foo() {
 
    }
}
 
 
public class B {
    public void methodB() {
 
    }
 
    public void foo() {
 
    }
}
Suppose that we want to write a class C that extends both A and B like this:
1
2
3
4
5
public class C extends A, B {
    public void methodC() {
        foo();
    }
}
As you can see, both A and B has a method called foo(), so which foo() method the class C invokes exactly? from A or B? This case is ambiguous hence Java does not allow.
 
7. Multiple inheritance of type is allowed:
This means Java does allow multiple inheritance between interfaces. For example:
1
2
3
4
5
6
7
8
9
10
11
public interface X {
    public void methodX();
}
 
public interface Y {
    public void methodY();
}
 
public interface Z extends X, Y {
    public void methodZ();
}
This is allowed because interfaces do not have concrete methods, thus there is no ambiguity.
Likewise, we can have a class implements multiple interfaces:
1
2
3
4
5
6
7
public class Sub implements X, Y, Z {
    public void methodX() { }
 
    public void methodY() { }
 
    public void methodZ() { }
}

Open in new window


if both Interface X and Interface Y has same method called methodAB what happens?


6. Multiple inheritance of state is not allowed:
Remember that Java does not allow a class inherits two or more classes directly. To understand why multiple inheritance is not allowed, consider the following example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class A {
    public void methodA() {
 
    }
 
    public void foo() {
 
    }
}
 
 
public class B {
    public void methodB() {
 
    }
 
    public void foo() {
 
    }
}
Suppose that we want to write a class C that extends both A and B like this:
1
2
3
4
5
public class C extends A, B {
    public void methodC() {
        foo();
    }
}
As you can see, both A and B has a method called foo(), so which foo() method the class C invokes exactly? from A or B? This case is ambiguous hence Java does not allow.
 
7. Multiple inheritance of type is allowed:
This means Java does allow multiple inheritance between interfaces. For example:
1
2
3
4
5
6
7
8
9
10
11
public interface X {
    public void methodAB();
}
 
public interface Y {
    public void methodAB();
}
 
public interface Z extends X, Y {
    public void methodZ();
}
This is allowed because interfaces do not have concrete methods, thus there is no ambiguity.
Likewise, we can have a class implements multiple interfaces:
1
2
3
4
5
6
7
public class Sub implements X, Y, Z {
    public void methodX() { }
 
    public void methodY() { }
 
    public void methodZ() { }
}

Open in new window


in this case

public interface Z extends X, Y {
    public void methodAB();
}

Open in new window

in above case Z gets X method or Y method


something like below
package MultipleInheritanceType;
public interface X {
    public void methodAB();
}

package MultipleInheritanceType;
public interface Y {
    public void methodAB();
}


package MultipleInheritanceType;
public interface Z extends X, Y {
    public void methodABC();
}

package MultipleInheritanceType;
public class Sub implements X, Y, Z {

	@Override
	public void methodABC() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void methodAB() {
		// TODO Auto-generated method stub
		
	}
    /*public void methodX() { }
 
    public void methodY() { }
 
    public void methodZ() { }*/
} 

Open in new window

what is difference between inheritance of state and  type
Please advise
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2018
Commented:
Imagine this

class A {
  public int getAge() {
    return 5;
  }
}

class B {
  public int getAge() {
    return 10;
  }
}

class C extends A,B {
  @Override
  public int getAge() {
    // Does this return 5 or 10;
    return super.getAge();
  } 
}

Open in new window


This is inheritance by state. You inherit the state, i.e. you inherit the result of the methods. With interfaces you only inherit the type, there is no implementation, it is left up to you to provide the implementation.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial