Not use "override" keyword in abstract method
Let's illustrate the difference with an example:
In this example, the MakeSound
method in the Dog
class does not override the abstract method MakeSound
from the Animal
class because it doesn't use the override
keyword. As a result, when you try to call MakeSound
on a reference of type Animal
that points to a Dog
object, it won't compile because MakeSound
is not defined in the Animal
class.
Now, let's fix this by properly overriding the method in the Dog
class:
Now, the MakeSound
method in the Dog
class correctly overrides the abstract method MakeSound
from the Animal
class. When you call MakeSound
on an Animal
reference pointing to a Dog
object, it will execute the implementation provided in the Dog
class, printing "Woof".
So, when you try to compile the code dog.MakeSound();
, it will indeed throw a compilation error, indicating that there is no suitable method MakeSound
available for the Animal
reference.
Last updated