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
// 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
- Method Overriding in Java _1(简单示例)
- Java方法重写(overriding method)
- Java - Method Overloading vs Method Overriding
- Method Overriding[叫做 覆盖 或 重写 或覆写] in Java _2(使用规则)
- Overriding vs. Overloading in Java
- 【异常形象】overriding vs. Overloading in Java
- Overriding Declarative Method
- 方法覆盖(method overriding)
- Rules for method overriding:
- java overriding
- Java中的方法重载(method overloading)和方法重写(method Overriding)的区别
- Method of Overriding and Overloading
- Overriding Java methods in Groovy for unit testing
- java 示例2——Get the class name in a static method
- Flex与Flash的交互_1(初级简单示例)
- 简单介绍java反射机制中Annotation(注解)与Method的程序示例
- Method Size Limit in Java
- Method Reference in Java 8
- PHP程序员如何突破成长瓶颈(php开发三到四年)
- python_Non-ASCII character
- ASP.NET 伪静态的实现
- 蓝牙笔和手机通过蓝牙协议的通讯 通过蓝牙笔点击杂志上印刷的电影,手机上播放点中的电影应用
- python_int类型转string
- Method Overriding in Java _1(简单示例)
- Socket 错误一览表
- C++ Primer 学习寄语
- ubuntu下拨号上网(ADSL(PPPOE))接入指南
- IBM POWERPC405 CPU lINUX下GPIO操作笔记
- Independent的技术小经验
- PowerISO v4.9
- Method Overriding[叫做 覆盖 或 重写 或覆写] in Java _2(使用规则)
- ree traversal