Java - Hiding Overrides and the Final Modifier

I could not find a question like mine, so I hope this is not a duplicate.

Again on how to redefine and hide. I think - but I could be wrong - I understood both.

The following code behaves as expected, both methods were hidden. method1, because it is a private method and private methods cannot be overridden only by the hidden methods of method2, because static and static methods cannot be overridden, they can only be hidden.

public class Child extends Parent { public void method1(){System.out.println("child");} public static void method2(){ System.out.println("static child");} } class Parent{ private void method1(){ System.out.println("parent");} public static void method2(){ System.out.println("static parent");} public static void main(String[] args){ Parent p = new Child(); p.method1(); //prints out "parent" p.method2(); //prints out "static parent" } } 

If I read the specs, it says:

http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.3.3

A method can be declared final to prevent subclasses from being overridden or to hide it.

If I change method1 in the parent class to "final"

 private final void method1(){ System.out.println("parent");} 

Everything works perfectly. edit start: I was expecting a compiler error to indicate that the final methods cannot be hidden, but this did not happen. : edit end

Question # 1: does this mean that only hidden methods can be hidden? In the book I am reading (OCA study guide, Jeanne Boyarski and Scott Selikoff p. 252), they clearly say that the hidden method is hidden.

Then I changed method2 in the parent class to

 public final static void method2(){ System.out.println("static parent");} 

Now the compiler complains, the error says: β€œThe child cannot override method2 ()”, which is quite confusing because I thought I was trying to hide the method.

Question No. 2: Should it be "The child cannot hide method2 ()"?

edit start: I am well aware that there is no redefinition here, but as the mentioned specifications mention, the final version of the modifier prohibits overriding or hiding methods, so I put it in the header. : edit end

+5
source share
2 answers

Question 1

Question # 1: does this mean that hidden methods can be hidden?

Parent.method1() not visible and not inherited by Child simply by virtue of private . Thus, Child.method1() did not overlap or hide Parent.method1() , it simply created a new method in Child that had the same names, parameters, and return type.

See http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8.3 :

Note that a private method cannot be hidden or overridden in the technical sense of these terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the type of the return type or cast of such a method carry any relation to the method of the private method in the superclass.

Question 2

Question No. 2: Should it be "The child cannot hide method2 ()"?

Yes you are right. This should be a "hide." In JLS ( http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8.2 ),

If class C declares or inherits the static method m, then it is said that m hides any method m ', where signature m is the submission (Β§8.4.2) of signature m' in superclasses and superinterfaces C that would otherwise be available for code in C.

Hiding are static methods for static methods. Overrides are instance methods for instance methods. The two cannot be mixed: the static method cannot override or hide the instance method, and the instance method cannot override or hide the static method.

BTW, my Eclipse compiler gives a similar error message: "Unable to override final method from parent"

+8
source

Well, I'm completely new to java, but I will try to answer.

The difference is that you use different access level modifiers: you use private in method1() of the Parent class and publish to method1() in the Child class. In fact, you are not hiding the method, since it is not the same method. A private modifier indicates that a member can only be accessed in its class, so you use the new method when you declare method1() in the Child class. Although the child inherits all methods from the parent (as it expands), private methods are not inherited. In the case of method2() , since it is declared public , it is inherited by the Child class and can be hidden.

More on this (taken from oracle lessons ):

Private members in the superclass

A subclass does not inherit private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, they can also be used by the subclass.

A nested class has access to all private members of its enclosing class β€” both fields and methods. Consequently, an open or protected nested class inherited by a subclass has indirect access to all private members of the superclass.

Edition: Question 2:

You are hiding a static method, not the last. Only hidden ones can be hidden, like here:

 class SuperClass { static void display() { System.out.println("Super"); } } class SubClass extends SuperClass { static void display() { System.out.println("Sub"); } } public class Test { public static void main(String[] args) { // Prints "Super" in console SuperClass sup = new SubClass(); sup.display(); // Prints "Sub" in console SubClass sub = new SubClass(); sub.display(); } } 

You use the final keyword in a method declaration to indicate that a method cannot be overridden by subclasses. So, if you change it, you redefine it, and therefore the compiler says:

overridden method is static,final (note final ).

The compiler complains about this because you no longer hide it. When you declared it final , you will override it. This will give you the same error if you do not use the static modifier in the Child class, as you will try to override what is no longer static . Hiding is used only when the static method hides another static method. If you try:

  • the non-static method β€œhides” the static: this is an override.
  • The final method hides the static: this is an override.

In these cases, you are no longer trying to hide (because hiding is only used statically), but you are trying to override.

+2
source

Source: https://habr.com/ru/post/1242209/


All Articles