Reputation: 732
class Base {
private void SayHello(){ //PRIVATE
System.out.println("Hello from Base");
}
}
class Derived extends Base {
public void sayHello(){ //PUBLIC
System.out.println("Hello from Derived");
}
}
public class TestHello{
public static void main(String[] args) {
Derived d = new Derived();
Base b = d;
d.sayHello(); //works as expected
b.sayHello(); //Why does this not work?
}
}
I want to understand: is the private sayHello from base class visible to the derived class? or is it a redefinition? And why does the call to the derived sayHello from the base pointer does not work? I mean, if it were public (in Base), then the sayHello from the derived class would have been called. So, what I can not understand is that if it has to call the public sayHello from the derived class, then why look at the access modifier from the base class?
Also, if you can point me to some concise resource that will help me understand this in more depth, I'd really appreciate this. Thanks!
Upvotes: 1
Views: 906
Reputation: 9616
Because the line below simply assigns as variable reference of object Derived
Base b = d;
and the private
methods are private to the objects and not visible outside of the class.
Upvotes: 1
Reputation: 17483
is the private sayHello from base class visible to the derived class?
Of course, no, because it has private
access modifier.
More about access modifiers:
Controlling Access to Members of a Class
or is it a redefinition?
As you can see in the accepted answer to this question:
What's the difference between redefining a method and overriding a method?
the term "redefinition" isn't commonly used. We can talk about "overriding" and "overloading", but in your case sayHello
from the Derived
class is a kind of new method and it's not an overloaded version of sayHello
from the Base
class.
And why does the call to the derived sayHello from the base pointer does not work?
Simply because you try to call method that doesn't belong to the open class interface.
I mean, if it were public (in Base), then the sayHello from the derived class would have been called.
Of course, it's an expected polymorphic behaviour. In this case, sayHello
from the Derived
class overrides sayHello
from the Base
class, so you can call sayHello
from the Derived
class via the reference to the the Base
class.
So, what I can not understand is that if it has to call the public sayHello from the derived class, then why look at the access modifier from the base class?
Because you use reference to the Base
class and there's no sayHello
method in the interface of the Base
class.
I found a good discussion here:
Overriding private methods in Java
May be also useful for you:
Hope it'll help you.
Upvotes: 1
Reputation: 324
b.sayHello() is not working because when you call the method sayHello() the computer starts looking for the method in the base class, and when it finds the method it will see that it is a private method, so it is going to tell you that you can't use it.
On the otherhand, d.sayHello() is working because the computer will start looking for sayHello() in the subClass derived, and when the computer finds it it will let you use it because it is public.
Upvotes: 0