File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance Doubt?

 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

Assume the following,

class C {}
class B extends C{}
class A exnetds B{}

Now the question is How can a method in a class A invoke the test() method defined in class C (without creating a new instance of class C).

Regards,
Jothi Shankar Kumar. S
 
toukhir mujawar
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just define the method test() as public

that must be sufficient....
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Above,

Your quote,

just define the method test() as public

that must be sufficient....


By default, the method without any modifier has a default access which means it is accesible in that particular package by any class defined in the same package. So your answer of marking the method public is not the one I expected. I came across this question from a website,

# Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How can a method in a class A invoke the test() method defined in class C (without creating a new instance of class C). Select the one correct answer.

1. test();
2. super.test();
3. super.super.test();
4. ::test();
5. C.test();
6. It is not possible to invoke test() method defined in C from a method in A.

The answer given was 6. But I feel it is 1. By the way what "Also all the three classes implement the method test()" means from the question above?Did they mean Overriding??

I tried it using the code as below,



and I got the o/p, I'm from the top class. So answer option 1 is correct in the above mentioned question. Anyone corrections on this?

Regards,
Jothi Shankar Kumar. S
[ October 27, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
Naresh Devasani
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above code, try overriding the test() method in class B and A with a different prints. Then your option will be wrong.


[HENRY: Formatted Code]
[ October 27, 2006: Message edited by: Henry Wong ]
 
Charith Fernando
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but the question asks you "How to call a method in C from the Class A"

this is not possible because you can only refer to you're immediate superclass

if you want to go further up in the hierarchy then you need to go to Class B and change the code..



run the above code...

and try calling the test() of the Class C if you can with the given options.. you cant... if you want to then call class B's test method by calling super.test() and then from class b's test method call class c's test() method by saying super.test() again
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I got it now, consider the code below,



If I comment the test() method in class B, then the call super.test() in Main class will check for the method first in B, if the method is not available, it will check in it's superclass which is A. Ok I understand it now.

Thanks guys,

Regards,
Jothi Shankar Kumar. S
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey cowboys,

what are you talking all about?


The question was if (and if yes, how) we can call a method OF a sub-sub-class FROM a super-super class.
And this without an instance of the sub-sub-class.

Cannot work.

If a test() method existed in class C, class A wouldn't even know it without an instance of class C.

super() ?

you must be kidding, they wouldn't give a keyword the name "super" if you used it to invoke a method of a subclass, would they?


Stampdede!


Yours,
Bu.
[ October 27, 2006: Message edited by: Burkhard Hassel ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic