Java Java Data Structures Organizing Data Interfaces


what is overriding? and how to use it

overriding is writing a special functionality to your subclass, Example if Vehicle is Superclass, Cycle is Subclass, then method drive can be overidden in Cycle class.

1 Answer

Mohammad Laif
Mohammad Laif
Pro Student 22,287 Points

If you have parent class contains printDateMethod(), and you create a new class extended that parent class. So you end up with child class and parent class.

Now using printDateMethod() in the parent class output -> "12-31-2017".

But in the child class you want to change that output to -> "31-12-2017", therefore you can change printDateMethod() code inside your child class, but you need to write "@Override" above it, to notify the compiler (so he did not freak out, wondering why the child behave different than his parent).

that @Override called annotation, and there are more of them!

So overriding simply is replacing the parent code with new code in the child.