• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple inheritance

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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")
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess it should be "extends" first and then "implements" if any
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
checking out!!!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic