using System;
public class DrawingObject
{
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing object.");
}
}
using System;
public class Line : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
However in java this is not the case, it is not optional.
However using the final keyword in your code you can tell the compiler that what methods are not allowed to be overridden.
Java compilers depending on their implementations use various techniques for performance optimization, function inlining being one of them i.e.
if a method is not overridden and it is short then the complier can optimize the method call be means od inlining.
e.getName() can be replaced with e.name
This is a major improvement if you think in terms of CPU. CPU's have prefecthing stratery builtin to speed up the FETCH-DECODE-EXECUTE cycle and it hates branching. So code inlining is definately is a speedup.
Fortunately JIT compilers in VM knows exactly which class overrides a given class and they can check whether any class overrides a given method, and hence decide for inlining.
In cases where you are sure that a methods needs not to be overridden, you can avoid this lookup activity/overload by declaring a method as final.
To sum up it makes a lot of sense if you declare all you methods as final unless you have a good reason that you want polymorphism.