Method overriding occurs when the names and the type signatures of the two methods are identical.
If not, the two methods are overloaded.
For example, consider this modified version of the preceding example:
class Base {
int i;
Base(int a) {
i = a;
}
void show() {
System.out.println("i: " + i);
}
}
class SubClass extends Base {
int k;
SubClass(int a, int c) {
super(a);
k = c;
}
void show(String msg) {
System.out.println(msg + k);
}
}
public class Main {
public static void main(String args[]) {
SubClass subOb = new SubClass(1, 2);
subOb.show("This is k: ");
subOb.show();
}
}
The output produced by this program is shown here:
This is k: 2
i: 1
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |