Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding ??

 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Swapna James
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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)
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I answered before experimenting properly.

I am also confused .....

Can anybody help?
 
raghu babu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JVM complains if I try to override the private method.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using ver 1.4.2
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can even try using ((superclassname this).methodname) to access that particular method.Try this it might work for sure.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic