my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can one access private methods of a superclass with a sub class reference? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "can one access private methods of a superclass with a sub class reference?" Watch "can one access private methods of a superclass with a sub class reference?" New topic
Author

can one access private methods of a superclass with a sub class reference?

Saurabh Chaubey
Ranch Hand

Joined: Oct 16, 2005
Posts: 101
In the following code why is the O/p "hello in base"? is it not true that we cant access private methods of a superclass with a sub class reference.

Plase clear my doubt.

Thanks in advance.

Regards,
saurabh
[ October 20, 2005: Message edited by: Barry Gaunt ]
Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185

In the following code why is the O/p "hello in base"?


Hi,

In class B, the 'private void hello()' signature makes the method become impossible for inheritance (since it is private).
And Hence, the signature 'void hello()' in class Sub is not an overidden version of the 'hello()' method of Base class. It is treated as a new method in Sub.

Base ref=new Sub();
ref.method();

ref.method(), first looks for method() in class Base. It is there and there is no corresponding version in Sub(since it is declared new method). And hence the output.

is it not true that we cant access private methods of a superclass with a sub class reference.


I dont understand your second question (in connection with this program).
[ October 20, 2005: Message edited by: Mani vannan ]

Manivannan
akshatacc mathur
Greenhorn

Joined: Oct 09, 2005
Posts: 1
I suppose that the a reference finds the method first in the corresponding Object type if it does not find it there then only it goes to Reference Class to find the corresponding method i also want to add that PRIVATE METHOD IS NOT ACCESSIBLE OUTSIDE THE CLASS
Please correct if i am wrong
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,


We cant access private methods of a superclass with a sub class reference.


Not directly....as you can see below...


but using a public method in Base class that can access the private method in Base..

The access of a private member is limited to the class...in which it is defined..









Hope you got it..
Saurabh Chaubey
Ranch Hand

Joined: Oct 16, 2005
Posts: 101
Thanks A kumar. i got it now
gurusamy sengodan
Ranch Hand

Joined: Oct 13, 2005
Posts: 41
Kumar& S Chaubey
Not like that,
We can't access private methods of a superclass with a sub class reference.

See "ref" is the Superclass Reference But it contains Subclass Object.
So by using Superclass Refernce only we can access the Private Method.
And one more thing i want to tell you.
Here we are calling the Member method() right
At first it searches the any method()which is present in Subclass
If it is not in subclass then it goes to Superclass.

class Base{
void method()
{
hello();
}
private void hello(){
System.out.println("hello in base");
}
}
class Sub extends Base{
void hello()
{
System.out.println("hello in Sub");
}
void method()
{
System.out.println("method in Sub");
}
}
class over1
{
public static void main(String k[])
{
Base ref=new Sub();
ref.method();//ans is "method in Sub"

}
}
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,
See "ref" is the Superclass Reference But it contains Subclass Object. So by using Superclass Refernce only we can access the Private Method.


Can you tell me how we can access Private Method using Superclass Refernce only ..


Except of course if we were to have object created in the same class where we have defined the method as private....


And the code ....you have shown..is supportive of method overriding..

Saurabh Chaubey
Ranch Hand

Joined: Oct 16, 2005
Posts: 101
Hi gurusamy sengodan
well the code given by you illustrates overriding, but my doubt was that i have a private method hello() in class Base and a public method hello() in sub class Class Sub. ref is a reference of superclass BUT it contains an object of subclass so why the method hello() of sub class is not called from method() in the class Base.

Regards,
Saurabh
Joseph Clark
Ranch Hand

Joined: Sep 10, 2005
Posts: 48
To answer the question, NO NO NO NO NO! "private" means not inherited.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,


well the code given by you illustrates overriding

but my doubt was that i have a private method hello() in class Base and a public method hello() in sub class Class Sub. ref is a reference of superclass BUT it contains an object of subclass so why the method hello() of sub class is not called from method() in the class Base.



As you have mentioned that it is an example of overriding...then where is the question of the base class method() being invoked..

it is method() of Sub that is called...

Srinivasa Kadiyala
Ranch Hand

Joined: Jun 10, 2004
Posts: 237
I hope Chaubey is watching this topic:

You have created a super class ref pointing to a sub class object.

The sub class knows about super class, but the super class may not know all in a sub class.

The statement ref.method() is calling the superclass method() as it is not overridden in the subclass.

Now the super class method() calls hello().

The trick comes now.....polymorphism. It has three types....shadowing, overriding and overloading.
1) overloaded

2) overridden.
2) shadowed. The shadowed method is the private method in the superclass.


Srini
Srinivasa Kadiyala
Ranch Hand

Joined: Jun 10, 2004
Posts: 237
when I was writting it got posted by mistake. Hereunder is the complete answer.

1) Overloaded

There is no overloaded method in the subclass for hello(). So this dos not apply.

2) Overriding

This is not possible as we can override the private methods. You make the method in super class 'public' then the sub class has overridden the super class method and the sub class hell0() will be called.

3) Shadowing

This is what happenned here. The hello() method in the super class is in shdow. The reference type is of super class then it picks up the shadowed hello() method in super class.

Hope this clarifies.

Pl reply you are clear or not
Srinivasa Kadiyala
Ranch Hand

Joined: Jun 10, 2004
Posts: 237
Pl ad my correction for case 2: Overriding

This is not possible as we can not override the private methods.
Srinivasa Kadiyala
Ranch Hand

Joined: Jun 10, 2004
Posts: 237
Just add some more info:
overriding allows a subclass to re-define a method it inherits from it's superclass.

You have not re-defined the method in your subclass. hello() in a sub class is in fact a new method as you can't inherit the private method.(Already explained above).

<<< rules for your info >>>>>

the access modifier for the overriding method may not be more restrictive than the access modifier of the superclass method

* if the superclass method is public, the overriding method must be public
* if the superclass method is protected, the overriding method may be protected or public
* if the superclass method is package, the overriding method may be packagage, protected, or public
* if the superclass methods is private, it is not inherited and overriding is not an issue

<<<< end here >>>>>

secondly, if you have worked earlier with c++ that may confuse you a little bit as latebinding( dynamic binding) of java is different to c++
( both are oop languages but implemented the spec differently)
Saurabh Chaubey
Ranch Hand

Joined: Oct 16, 2005
Posts: 101
Thanks Puthriah Sarma
I got it now.

Regards,
Saurabh
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: can one access private methods of a superclass with a sub class reference?
 
Similar Threads
I am really confused regarding overriding
overridding
static & non-static
private final method
Abstract classes