• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple inheritance  RSS feed

 
Perumal Kasi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A{
public void methA(){
System.out.println("A:methA");
}
}

interface B{
public void methA();
}

public class C extends A implements B{
public void methA(){
System.out.println("C:methA");
}
}

In the above class C,are we implementing the method methA declared in interface B or we overriding the method of class B?

Thanks,
Perumal K
 
Sanjaya Sugiarto
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct me if I'm wrong (not tested it for real yet, just my perception):
you override the methA declared in class A (if you call the methA in class C than the output would be "C:methA")
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the above class C,are we implementing the method methA declared in interface B or we overriding the method of class B?


Actually both are right. Since it implements the Interface, it should implement the method declared in the interface. Also, a super class reference type used shall be a overriding principle.

correct me if I am wrong.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, the whole game is behind this line:

public class C extends A implements B

For the above line, class C is overridding the method of class A.

If the line would be:

public class C implements B extends A

Then, class C will implement method of interface B.

How I derived this point:

Remove the methA() method from class C and change the order of extends and implements (class and interface).

But Really not sure, and good question Peru.
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class C extends A implements B

For the above line, class C is overridding the method of class A.

If the line would be:

public class C implements B extends A

Then, class C will implement method of interface B.


I dont think the order would matter here.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
I think, the whole game is behind this line:

public class C extends A implements B

For the above line, class C is overridding the method of class A.

If the line would be:

public class C implements B extends A

Then, class C will implement method of interface B.

How I derived this point:

Remove the methA() method from class C and change the order of extends and implements (class and interface).

But Really not sure, and good question Peru.


public class C implements B extends A

It's giving compilation error. But I am not sure, what it is?
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it should be "extends" first and then "implements" if any
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
I think, the whole game is behind this line:

...

How I derived this point:

Remove the methA() method from class C and change the order of extends and implements (class and interface).

No, that reasoning is wrong. Java just requires that you use "extends" before "implements". If you do it the wrong way around, the Java compiler gives you an error just because you do it the wrong way around. Not because it means something different.

Class C both override the method in class A and implements the method in interface B at the same time. There's nothing mysterious or wrong about that. And this is not multiple inheritance. Note that there is only one implementation of methA() in the superclass A, so the typical ambiguity problem that you would get with multiple inheritance ("which implementation is inherited?") doesn't happen here.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do have a mild risk that A documentation says "methA displays the distance to the sun in millifurlongs" and the doc for B says "methA displays the number of uncles a monkey has". The only choice then would be to implement methA to execute the "halt and catch fire" byte-code instruction.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
You do have a mild risk that A documentation says "methA displays the distance to the sun in millifurlongs" and the doc for B says "methA displays the number of uncles a monkey has". The only choice then would be to implement methA to execute the "halt and catch fire" byte-code instruction.


Of course were this to happen it's extremely likely there's a serious problem with your design or your methods are named very badly, or both.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, any time we extend and/or implement multiple abstractions we should ask if we're doing two things that would be better in two classes. One can get away with it a lot of times, but all it takes to ruin your day is one method name that doesn't completely reflect its semantic meaning.

Editing to add a timely link to Michael Feathers Blog about LSP and a uniform semantic grammar across an application.
[ August 23, 2006: Message edited by: Stan James ]
 
Abin francis alappat
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
checking out!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!