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.