0

Please clearly tell me what is overriding ?

15th Feb 2018, 7:10 AM
suresh
suresh - avatar
2 Answers
+ 4
A method of a superclass is overriden in the subclass when the subclass creates its own implementation of the method. Example (Java): public class SuperClass { public void methodToOverride(){ System.out.println("Original implementation"); } } public class SubClass extends SuperClass { @Override public void methodToOverride() { System.out.println("New Implementation for SubClass"); } } public class Main { public static void main(String[] args){ SubClass a = new SubClass(); a.methodToOverride(); //output is New Implementation for SubClass } }
15th Feb 2018, 7:26 AM
David Akhihiero
David Akhihiero - avatar
+ 1
Overriding in general terms means changing the behaviour of a predefined function and changing it for a certain context. This is useful in a number of ways, such as defining different behaviour for subclasses or allowing custom classes to use the +, -, *, / and other operators. Not to be confused with overloading, which is making two functions with the same name but different parameters.
15th Feb 2018, 8:51 AM
spcan
spcan - avatar