Method Overriding in Java _1(简单示例)

来源:互联网 发布:java基础入门怎么样 编辑:程序博客网 时间:2024/06/14 01:25

Method Overriding in Java

By: Henry Emailed: 522 times Printed: 587 times  

FROM: http://www.java-samples.com/showtutorial.php?tutorialid=287

In a class hierarchy, when a method in a subclass has the samename and type signature as a method in its superclass, then the method in thesubclass is said tooverridethe method in the superclass. When an overriddenmethod is called from within a subclass, it will always refer to the version of that methoddefined by the subclass. The version of the method defined by the superclass will behidden.Consider the following:

// Method overriding.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " +j);
}
}

class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
System.out.println("k: " + k);
}
}

class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}

The output produced by this program is shown here:

k: 3

When show( ) is invoked on an object of type B,the version ofshow( )defined within Bis used. That is, the versio n ofshow( )inside B overridesthe version declared inA. If you wish to access the superclass version of an overriddenfunction, you can do so by usingsuper. For example, in this version ofB,the superclass version ofshow( ) is invoked within the subclass' version. This allows all instancevariables to be displayed.

class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
super.show(); // this calls A's show()
System.out.println("k: " + k);
}
}

If you substitute this version of A into the previousprogram, you will see the following output:

i and j: 1 2
k: 3

Here, super.show( ) calls the superclass version of show(). Method overriding occursonlywhen the names and the typesignatures of the two methods are identical. If they are not, then the two methods aresimply overloaded. For example, consider this modified version of the precedingexample:

// Methods with differing type signatures are overloaded – not
// overridden.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " +j);
}
}

// Create a subclass by extending class A.
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// overload show()
void show(String msg) {
System.out.println(msg + k);
}
}

class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show("This is k: "); // this calls show() in B
subOb.show(); // this calls show() in A
}
}

The output produced by this program is shown here:

This is k: 3
i and j: 1 2

The version of show( ) in B takes a stringparameter. This makes its type signature different from the one inA, which takes no parameters.Therefore, no overriding (or name hiding) takes place.


If this tutorial doesn't answer your question, and you have a specific question, just ask an expert here. Post your question to get a direct answer.


MORE:

http://docs.oracle.com/javase/tutorial/java/IandI/super.html

http://stackoverflow.com/questions/427756/overriding-a-super-class-instance-variables