Extend x Overwritten, what's the difference?

13

Researching on Object Oriented concepts, I came across the following comparative, Extend (Inheritance) versus Overwritten.

I found the following statement for this comparison:

Extend:

When we include new attributes, methods in a child class.

Overwrite

If we redefine existing (inherited) methods in a child class

This definition was not clear in my vision. What is the difference between Extend and Override in Object Orientation?

    
asked by anonymous 26.09.2015 / 22:55

4 answers

9

Extend ( extend ) is a concept of the class and broader. You extend a class by probably putting new members (not just attributes) inside it or doing something else in existing methods. Whenever we create a class inheriting from another we intend to extend the class in some way.

Overwrite ( override ) is a concept of the method and more specific. You override an existing method in an inherited class, that is, you make the implementation contained in the method of the current class to be used instead of the existing implementation in the inherited class. Some languages do this implicitly and others require that the desire to be overwritten is explicit, since otherwise the overwriting could happen by accident, without the desire of the programmer.

See the example in fictional language (the rules of each language may vary):

class A {
    metodo1() { print "A"; }
    metodo2() { print "A"; }
}

class B extends A {
    override metodo1() { print "B"; }
    metodo2() { print "B"; }
}

A a = new A().metodo1(); //imprime A
A b = new B().metodo1(); //imprime B, note que o tipo é A, mas a implementação é B
A c = new B().metodo2(); //imprime A, o tipo é A e o método não foi sobrescrito

Read more about override and your optionality in Java .

An addendum: I did a test with Java and C #. I knew how the second behaved but was not sure of the first.

The above example works fine for C # . Do what I said and give a warning that probably is not what you want.

Java made the override even though it could not .

But the answer still counts. It's just that now I know that Java does% implicit% as far as it should not. keep this in mind when programming in Java.

    
26.09.2015 / 23:27
4

When you use inheritance, the class you receive automatically has the same methods and attributes.

In some cases you need to change the method of the class you are extending. The example below demonstrates this difference. The person has only two attributes, so to return the data I can return only name and cpf. In the case of the Official besides name cpf, it will be necessary to return the salary so it will be necessary to rewrite the method. So we use "@Override" to indicate that the "getData" method should have a specific behavior in that class.

class Pessoa {

    protected String nome;
    protected String cpf;

    public Pessoa(String nome, String cpf){
        this.nome = nome;
        this.cpf = cpf;
    }

    public String getCpf(){
        return cpf;
    }

    public void setCpf(String cpf){
        this.cpf = cpf
    }

    public String getNome(){
        return nome;
    }

    public void setNome(String nome){
        this.nome = nome;
    }

    public String getDados(){
        return cpf + "-" + nome;
    }
}

class Funcionario extends Pessoa /* Estende a classe Pessoa */{

    protected double salario;

    public Funcionario(String nome, String cpf, double salario){
        super(nome, cpf); /* Chama o método construtor da classe Pessoa */
        this.salario = salario;
    }

    public double getSalario(){
        return salario;
    }

    public void setSalario(double salario){
        this.salario = salario;
    }

    @Override /* Sobrescreve o método da classe pessoa, dessa forma irá rodar o método da classe Funcionário */
    public String getDados(){
        return cpf + "-" + nome + "-" + salario;
    }
}

class HelloWorld {
    public static void main(String[] args) {
        System.out.println(new Pessoa("Joao Maria", "987.456.777-45").getDados()); // Display the string.
        System.out.println(new Funcionario("Joao Carlos", "456.222.444-33", 1256.00).getDados());
    }
}
    
26.09.2015 / 23:37
4
  

Extend

When you extend a class (abstract or not), you are making the new class have all the methods and attributes of the class you are extending. If you extend the class below

class Animal{
    String raca;

    public String getRaca(){
        return raca;
    }
}

In a class named Gato , your new class will have the raca attribute and the getRaca() method already implemented.

  

Overwrite

However, in your class Gato , you can override the getRaca method, causing it to have another behavior, or even that it has some code added. See the example

class Gato extends Animal{
    public string getRaca(){
        return "Raça qualquer"; //Aqui eu estou sobrescrevendo o comportamento do método
    }
}

class Cachorro extends Animal{
    public String getRaca(){
        String base = super.getRaca(); //Aqui estou usando o método da "classe pai"
        return "Cachorro: " + base; //Aqui o retorno da classe filha
    }
}
    
26.09.2015 / 23:47
3

Examples:

Extend:

class Pessoa {

    String nome;

    setNome(String nome) { this.nome = nome }
    getNome() { return this.nome }  
}

class Funcionario extends Pessoa {

    /* Método getPis() ou setPis(String pis) são novos métodos da 
     *  classe Funcionário sendo que a mesma é filha da classe Pessoa      
     */

    String pis;

    setPis(String pis){ this.pis = pis }
    getPis() { return this.pis }  
}

Overwrite:

class Pessoa {

    String nome;

    setNome(String nome) { this.nome = nome }
    getNome() { return this.nome }  
}

class Funcionario extends Pessoa {

    String pis;

    setPis(String pis){ this.pis = pis }
    getPis() { return this.pis } 

    /* Aqui estamos sobrescrevendo um método que existe na classe
     * pai da classe Pessoa passando um valor padrão para o atributo nome
     * diferente do método setNome da classe pai
     */ 

    @Override
    setNome(String nome) { this.nome = "Sou tratado de forma diferente" }
}
    
26.09.2015 / 23:29