In
object oriented programming, method overriding is a language feature that
allows a subclass to provide a specific implementation of a method that is
already provided by one of its super classes. The implementation in the subclass
overrides (replaces) the implementation in the super class. Hence, overridden
method is always executed from the object whose object is stored in referenced
variable. Super class method is called overridden method, and subclass method
is called overriding method. It is also known as covariant return type with a method having return type.
Example –
class
Example
{
void
add(int a,int b)
{
System.out.println("(int,int) in Example : "+(a+b));
}
void
sub(int a,int b)
{
System.out.println("(int,int) in Example : "+(a-b));
}
}
public class
Overridding extends Example{
void
add(int a, int b)
{
System.out.println("add(int,int) in Overridding");
System.out.println("the addition of "+a+" and
"+b+" is = "+(a+b));
}
public static void
main(String[] args) {
Overridding
o = new
Overridding();
o.add(10, 20);
o.sub(10, 20);
}
}
Output –
add(int,int) in Overridding
the addition of 10 and 20 is = 30
(int,int) in Example : -10
No comments:
Post a Comment