View RSS Feed

Java Object

Java method Overriding

Rate this Entry
by , 04-26-2012 at 05:42 PM (813 Views)
Example that has been given below shows the method overriding, in Java. In java, method overriding is a subclass method that overrides the super class method. Extend keywords are used by the subclass for extension of the super class. In the example, Class B is present in the subclass. Class A is present in super class. Overriding method of superclass & subclass has similar signatures. Methods of the superclass are being modified by using overriding.
Java Code:
class A {
int i;
A(int a, int b) {
i = a+b;
}
void add() {
System.out.println("Sum of a and b is: " + i);
}
}
class B extends A {
int j;
B(int a, int b, int c) {
super(a, b);
j = a+b+c;
}
void add() {
super.add();
System.out.println("Sum of a, b and c is: " + j);
}
}
class MethodOverriding {
public static void main(String args[]) {
B b = new B(10, 20, 30);
b.add();
}
}

Submit "Java method Overriding" to Facebook Submit "Java method Overriding" to Digg Submit "Java method Overriding" to del.icio.us Submit "Java method Overriding" to StumbleUpon Submit "Java method Overriding" to Google

Comments