This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance Doubt?" Watch "Inheritance Doubt?" New topic
Author

Inheritance Doubt?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9398
    
    2

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


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
toukhir mujawar
Ranch Hand

Joined: Oct 11, 2006
Posts: 70
just define the method test() as public

that must be sufficient....
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9398
    
    2

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

Joined: Oct 26, 2006
Posts: 9


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 ]

Thanks,<br />Naresh
Charith Fernando
Ranch Hand

Joined: Sep 12, 2005
Posts: 67
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


Charith I Fernando<br />SCJP5, SCWCD, SCBCD, BSc(Hons) IS<br />+94 773 263 222 (mobile)
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9398
    
    2

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

Joined: Aug 25, 2006
Posts: 1274
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 ]

all events occur in real time
 
Don't get me started about those stupid light bulbs.
 
subject: Inheritance Doubt?