问题
public class Shape
{
final private void print()
{
System.out.println("in class Shape");
}
public static void main(String[] args)
{
Shape shape=new Rectangle();
shape.print();
//calling shape class function
//giving output in class shape
}
}
public class Rectangle extends Shape
{
public void print()
{
System.out.println("in class Rectangle");
//super.print();
}
}
Ques: why private function don't show polymorphic behaviour ? and we are still overriding final method? its calling base class funtion why?
回答1:
A Private function is not visible nor callable from its children; hence these are two completely different functions. There is nothing to overwrite from the perspective of the child class, because it is not aware that the parent even has a print() function.
回答2:
Making it final private void print()
is to prevent it from overriding in sub-classes.
As final
prevents overriding and private
makes the method invisible to the sub-classes so that it cant be accessed
See also :
- Java `final` method: what does it promise?
- Overriding private methods in Java
回答3:
You are not actually over-ridden the print method because of private. They are completely different.
More over you cannot override a final method.
This is the place where @override
annotation helps you better. If you try to place the annotation, then you realize the behaviour at compile time itself.
回答4:
In addition to Eriks answer from the Java Language Specification:
A class C inherits from its direct superclass all concrete methods m (both static and instance) of the superclass for which all of the following are true:
- m is a member of the direct superclass of C.
- m is public, protected, or declared with package access in the same package as C.
- No method declared in C has a signature that is a subsignature (§8.4.2) of the signature of m.
and
An instance method mC declared in or inherited by class C, overrides from C another method mA declared in class A, iff all of the following are true:
[...] One of the following is true:
- mA is public.
- mA is protected.
So your subclass doesn't inherit the private methods, hence there is nothing to override.
回答5:
Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. In other words, polymorphism allows you define one interface and have multiple implementation. This is one of the basic principles of object oriented programming.
The method overriding is an example of runtime polymorphism. You can have a method in subclass overrides the method in its super classes with the same name and signature. Java virtual machine determines the proper method to call at the runtime, not at the compile time.
But if you will think as print() is instance method and at runtime why it is not calling from Rectangle print() method.
The reason is as print() of subclass is not a overriden method as parent class method is final which cannot be overriden.
Shape shape=new Rectangle();
shape.print(); // prints in the shape class
((Rectangle)shape).print(); //prints in the rectangle class
As parent's class method is private so it is not visible for outside world and as it is final it cannot be overriden.
回答6:
In your example it is shown as private final
method so this method is not visible out side the class. So Rectangle can't see the method defined inside Shape
class.
public class A{
final private method1(){
}
final public method2(){
}
public method3(){
}
}
public class B extends A{
public method1(){
//it is legal. but it is not a override. this method can't see the method1 defined in A
}
public method2(){
//throw error. because final method can't be overriden
}
public method3(){
//legal override method
}
}
来源:https://stackoverflow.com/questions/27167661/java-override-a-private-function-dont-showing-polymorphic-behaviour