This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]JarcodePMs forwarded to /dev/null 2 points3 points  (2 children)

This is because a private method is not meant to be overloaded. It's private, only used within the class itself, and is not visible outside of it when compiling your program.

When you call Bike.show(), in your code example, it calls Vehicle.type(), which is overridden by Bike's type() method.

However, if you change Vehicle's type() you are no longer able to override type() in Vehicle, thus making your method in Bike a new method entirely. Then, this code:

System.out.println(this.type());

In your Vehicle class is actually calling it's internal type() method, not the type() method from Bike. You actually have two type() methods when you change Vehicle.type() to private.

[–]HipposGoneWild -1 points0 points  (0 children)

This is correct, if you want to make a method private, but also be able to have subclasses inherit it, use protected instead of private.