Extend (extend
) is a class concept and broader. You extend a class by probably putting new members (not just attributes) into 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 method concept and more specific. You overwrite an existing method in an inherited class, i.e., you make the implementation contained in the current class method to be used in place of the existing implementation in the inherited class. Some languages do this implicitly and others require the explicit desire for superscription, since otherwise the superscript could happen by accident, without the desire of the programmer.
See the example in fictitious 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 the 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 well for C#. Does what I said and gives a Warning which is probably not what you want.
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
Java has made the override even if you can’t.
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
But the answer still holds. Is that now I know that Java does the override
implicit where it should not. keep this in mind when programming in Java.
When you put in some external information make sure we can easily access it. Otherwise it is irrelevant to say where you took it. It might even look like spam, even if you don’t mean to. Remember that most courses on the internet, paid or free, teach only the basics and often the wrong way. In general, none is worth it. but for an introduction. This if you have a critical sense. If you trust the courses, you are screwed.
– Maniero
Got it, thanks for your feedback @bigown. I’ll edit my question.
– Duds