• 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

query on method over riding

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a basic question on method overriding.
I have used 2 classes in different packages for illustrating this scenario.

Parent.java
-----------


Child.java
----------


Since the child is in a different package, the protected method hello() in child is not visible to the Parent. But when I run the program, the hello() method in child is getting executed.
Does this means that, once a child class over rides the method, the child method would be always called irrespective of the access modifier?

[edit]Add code tags. CR[/edit]
[ September 28, 2008: Message edited by: Campbell Ritchie ]
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The protected access modifier can be accessed by all classes in the same package as well as by the subclass,even if its in a different package.Moreover, in case of overridden methods, at runtime, the call to the method depends on the object type and not the refernce type.

So in this case, the parent reference variable has a reference type Parent and object type Child. So, it will invoke method in the Child class( as it is the object type).
[ September 28, 2008: Message edited by: Abhi vijay ]
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Question by dipu menon ,


ANY UPDATE please ?
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhi's explanation is good.

Take a look here to check a table on access modifiers.
 
dipu menon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abi,
Thanks for your prompt reply. The explanation you have given about the protected access modifier is absolutely right. That happens incase of inheritance.
In my case,the main method is creating an object of child and referencing with the parent variable. The hello()method of child is not invoked by inheritance. Let me re-write the code to make it more readable. I am introducing a class called RunClass to create the Child object. The RunClass and Parent are in the same package and the Child is in a different package.

RunClass.java
-------------


Parent.java
-----------

Child.java
-----------


Just glance through the modified code. The RunClass and Parent are in the same package. So a call to the "protected parent.hello()" inside RunClass would be OK for the compiler.
During runtime, the JVM sees that the parent reference variable is referencing a Child object, but the hello() method of the Child class should not be accessible to RunClass, which is in a different package.
I think you got my point.

[edit]Add code tags. CR[/edit]
[ September 28, 2008: Message edited by: Campbell Ritchie ]
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think hello() method of the Child class should not be accessible to RunClass, which is in a different package???
 
dipu menon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because its protected.
If you insert the following code inside RunClass, You'll get compilation error.

Child child = new Child();
child.hello();


This is because, the hello() is protected and only subclasses of the Child
would have access to it.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dipu menon:
This is because, the hello() is protected and only subclasses of the Child
would have access to it.

No, it is because the RunClass class is in a different package.

The keyword "protected" allows access from the same package, or from subclasses of the class where "protected" is used.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cambpell,

but [RunClass not extends Child ]
------------------------------
RunClass parent = new Child();
parent.hello();
---------------------------
in this case how it is running because hello() is in protected which is in Child Class ,different package .more over it is not extends Child .

please clarify my doubt. i think Abhi gives the explanation for overridding and protected access specifier...
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


for the compiler it checks the Parent reference and it's in the same package with RunClass so, that is allowed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic