aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding ??" Watch "overriding ??" New topic
Author

overriding ??

srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252

can anyone explain me how come the method1 of parent class is called ???

thanks & regards

srikanth

( tags added)
[ September 02, 2005: Message edited by: Barry Gaunt ]

Thanks & Regards<br /> <br />-Srikanth
Swapna James
Ranch Hand

Joined: Jul 26, 2005
Posts: 56
Hi Srikanth,

The Child class does not have a method2. SO when p.method2 is called the inherited method2 from the parent will be called.

If you modify the code like this-


Then the Child method2 will be called.

Hope you are clear.

Swapna.


SCJP 1.4
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
hi swapna ...

thats true ..since there is no method2 in the child class its parent class method method2 is called ..after that its there method1...my doubt is why is it calling parent class method1.....inspite of instance being that of child class...

thanks & regards

srikanth .....
Swapna James
Ranch Hand

Joined: Jul 26, 2005
Posts: 56
Hi Srikanth,

Because Child does not have method2 Parent method2 is called->p.method2();

Parent method 2 calls Parent method1 here->this.method1();

So when "this" is called the parent method1 is called.

Hope it is clear. And the rest of you guys ... correct me if I am wrong.

Swapna.
Renu R.
Greenhorn

Joined: Sep 02, 2005
Posts: 7
I dont know the reason why but I coud see that if you change the access modifier of the parent class method1 to public from private then the child class's method1 is called.I suppose somebody can help us out in this
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"NewToRanch Forum" Please read our JavaRanch Naming Policy and change your displayed name to comply with it. We require a displayed name in the format <first name><space><family name>, preferably your real names.

As it stands your displayed name will mean your account will be disabled in the near future.

Thanks
-Barry

(NR)


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Because the method Parent.method1() is private, the method Child.method1() does not override it. That is, there is no polymorphism involved for method1(). Therefore, in the method Parent.method2() there is only the method Parent.method1() available for it to call.
[ September 02, 2005: Message edited by: Barry Gaunt ]
Swapna James
Ranch Hand

Joined: Jul 26, 2005
Posts: 56
I guess I answered before experimenting properly.

I am also confused .....

Can anybody help?
raghu babu
Ranch Hand

Joined: Jul 05, 2005
Posts: 60
Here is the explanation for this :

If m1 is a private member of the class T where the invocation expression occurs, then the implementation in class T is selected at run-time regardless of the run-time type of the object.

This is excerpt is from Dan Chisholm's site.

Here m1 is method1()
T is Parent class.

Based on this explanation, at compile time, method2 in parent class calls, priavate method method1. As the above statement clearly explains,
parent's method1 is what called at runtime.

Hope this helps.
sameer inamdar
Ranch Hand

Joined: Jul 04, 2005
Posts: 40
JVM complains if I try to override the private method.


Dream!!! Set Vision !! Transform Vision to Mission by Actions on Deadlines!.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by sameer inamdar:
JVM complains if I try to override the private method.


And what error does the JVM give you? What version of the JVM are you using?
Roy Simon
Ranch Hand

Joined: Aug 26, 2005
Posts: 62
i dont see the reason as to why someone will make a super class method private and have the same method in the subclass(making a method private--no polymorphism) so there is no overriding !!!
so if i say
super_class obj = new sub_class();
//method - private in super_class
//method - public in sub_class
//here the compiler intervenes saying unknown method method()
//method private in super_class
obj.method();

making the method abstract would make more sense

regards
simon
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Simon Roy James:
i dont see the reason as to why someone will make a super class method private and have the same method in the subclass(making a method private--no polymorphism) so there is no overriding !!!


That's the point Roy. Part of studying for the SCJP certification is to learn to recognize these cases and to reason why the code does not work as expected or stated.
[ September 04, 2005: Message edited by: Barry Gaunt ]
sameer inamdar
Ranch Hand

Joined: Jul 04, 2005
Posts: 40
I am using ver 1.4.2
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by sameer inamdar:
I am using ver 1.4.2


And what is the error message you get from your JVM?
swathi mims
Greenhorn

Joined: Sep 04, 2005
Posts: 4
I think you can even try using ((superclassname this).methodname) to access that particular method.Try this it might work for sure.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: overriding ??