user994165
user994165

Reputation: 9516

Overriding a parent method

I have a class A private method 1

class B extends A private method 1 (Same name)

When I instantiate B and method 1 gets called from class A. I made them both protected and that solved it. Is there a way to keep both private and still get class B's method 1 to be called?

Upvotes: 0

Views: 65

Answers (3)

Óscar López
Óscar López

Reputation: 236170

Take a look at this explanation of the visibility modifiers from the java tutorials, it'll be clear after that

Upvotes: 2

Lycha
Lycha

Reputation: 10187

private methods are only accessible from that specific class so you can't override them. You can have method with same name and arguments in a subclass and there will be no errors, but it will not override it.

Upvotes: 0

pimaster
pimaster

Reputation: 1967

No. Private is designed for that class and that class alone.

Protected is designed for any subclesses to know about, use and override if required.

Upvotes: 0

Related Questions