Difference between private and final methods

15

Studying final methods and classes in the book "Programming in Java 6 ed." of Deitel I came across the following proposition:

  

The methods declared private are implicitly final, because it is impossible to overwrite them in a subclass (although the subclass can declare a new method with the same signature as the private method in the superclass) . >

I did not understand this last part in italics. Is declaring a method with the same signature not the same as overwriting?

    
asked by anonymous 30.11.2015 / 12:22

5 answers

5

I'll be direct,

  

(although the subclass can declare a new method with the same   signature of the private method in the superclass)

I do not understand this last part in italics. Is declaring a method with the same signature not the same as overwriting?

No, because within the super class calls of the private methods that are made inside the superclass will still continue pointing to the private method of the superclass itself, even if there is an equal in subclass.

Super Class

public class SuperClasse {
    public int processar(int x, int y){
         return somar(x,y);
     }

     private int somar(int x, int y){
          return x+y;
     }
}

Daughter Class

public class SubClasse extends SuperClasse {
    private int somar(int x, int y){
         return x*y;
     }
}

Main

public static void main(String[] args) {
    SubClasse sub = new SubClasse();
    int resultado = sub.processar(10, 5);
    System.out.println(resultado+"");
}

Take the test and see the output.

How do we deduce that there was no overwriting? because if it had been overwritten the result would be 50 . As there was not, the processing made the sum. Want to know how to overwrite? transform sum methods to public . Turn around and see the result again.

    
30.11.2015 / 14:01
13

It is not the same if the method is declared as private . This modifier indicates that the method should only exist within the class. It is something internal to her and no one else should have access, not even the types derived from it.

If the method only exists in it, it can not be seen or accessed by the derived types, any method that has the same signature is a new method. The overwrite would only occur if the method existed for the derived type. And even if it was not final . I assume you understand that final also prevents overwriting.

What you may not know is that it does not prevent an identical subscription method. Only in this case will the overlap occur. In most cases it is not what you want.

This has to do with polymorphism.

When the method is public and virtual, where one expects a more generic type can use the more specific type and the method of this type is what will be called.

When the method is not virtual, the method that will be called is always of the generic type. Polymorphism does not occur. Then you think that the method with the same signature in the derived type will be called, but it will not. It can only be called directly.

class A {
    public metodo() { System.out.println("A"); }
}
class B extends A {
    @override public metodo() { System.out.println("B"); }
}
class Principal {
    public static main() {
        B x = new B();
        teste(x); // imprime B porque B é derivado de A e o método é virtual
        A y = new A();
        teste(y); // imprime A
    }
    public static teste(A x) {
        x.metodo();
    }
}

No polymorphism:

class A {
    private metodo() { System.out.println("A"); }
}
class B extends A {
    public metodo() { System.out.println("B"); }
}
class Principal {
    public static main() {
        B x = new B();
        testeB(x); //Imprime B
        teste(x); //por ser privado, é final, e o compilador não tenta acessar o tipo derivado
        // não vai funcionar, teste() só consegue acessar o método de A, que é privado
        A y = new A();
        teste(y); //não vai funcionar
    }
    public static teste(A x) {
        x.metodo(); //não compila, o método não existe, métodos privados não são acessíveis fora da classe
    }
    public static testeB(B x) {
        x.metodo();
    }
}

Note that here the methods have the same signature but one exists privately and another publicly exists. They are not confused. You can not overwrite something you can not even see. If it is private the compiler will ignore the method.

Sem polimorfismo mas tudo público

class A {
    final public metodo() { System.out.println("A"); }
}
class B extends A {
    public metodo() { System.out.println("B"); }
}
class Principal {
    public static main() {
        B x = new B();
        testeB(x); //Imprime B, óbvio, não tem como errar
        teste(x); //Imprime A, não há polimorfismo, ele só entende o que há em A
        A y = new A();
        teste(y); //imprime A
    }
    public static teste(A x) {
        x.metodo();
    }
    public static testeB(B x) {
        x.metodo();
    }
}
    
30.11.2015 / 12:35
9

The keyword final means that the method defined with it can not be overridden in a subclass.

Private methods are only accessible within the defined class, therefore it is not possible to access / override a private method in a subclass.

  

Declaring a method with the same signature is not the same as   overwrite?

This depends on where the method is declared.

When there are two or more methods with the same signatures (parameters of different types) in the same class this is called overload or overloading .

When a method has an equal signature and is inherited from a super class, it is called superscript or override , ie the subclass can change / override the behavior / implementation of the method provided by the superclass, provided that this method does not has been marked as final .

    
30.11.2015 / 12:33
7
  

Declaring a method with the same signature is not the same as overwriting?

If you declare methods with the same signature but in completely different classes, this is not to overwrite, they are only two methods with the same signature but have no relation to each other.

Overwrite can only occur in an inheritance or extension relationship between classes and interfaces, ie a class can override a method of the superclass or the interface it implements, as long as this method is visible to the subclass, otherwise , it is the same as saying that there are two methods with the signature table that do not relate, like the example of the distinct classes.

    
30.11.2015 / 12:42
3

Private indicates that the property or method is not inherited by child classes and only the owner class can access the property or method.

Final indicates that the child class can inherit and access (access inherited properties and methods, not parent class properties or methods), but can not modify or override. That's it .

What goes beyond this is merely didactic.

    
08.12.2015 / 14:00