This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Saurabh Chaubey
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
akshatacc mathur
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks A kumar. i got it now
 
gurusamy sengodan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer the question, NO NO NO NO NO! "private" means not inherited.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pl ad my correction for case 2: Overriding

This is not possible as we can not override the private methods.
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Puthriah Sarma
I got it now.

Regards,
Saurabh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic