Method Overriding happens when a method in a subclass has the same name and type signature as a method in its superclass.
When an overridden method is called within a subclass, it will refer to the method defined in the subclass.
The method defined by the superclass will be hidden.
Consider the following:
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() {
System.out.println("k: " + k);
}
}
public class Main {
public static void main(String args[]) {
SubClass subOb = new SubClass(1, 3);
subOb.show();
}
}
The output produced by this program is shown here:
k: 3
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. |