java继承覆盖,Java继承(方法覆盖和重载)

Besides that Java inheritance is a fundamental feature of the language, I have some questions.

Here is the source for my testing example:

class MyClass{

public void say(String t){

System.out.println("Hello MyClass "+t);

}

public void print(MyClass t){

System.out.println("MyClass is printed");

}

public void anotherPrint(int i){

System.out.println("MyClass is printed again");

}

}

class MyClass2 extends MyClass{

public void say(String t){

System.out.println("Hello MyClass2 "+t);

}

public void print(MyClass2 t){

System.out.println("MyClass2 is printed");

}

public void anotherPrint(double i){

System.out.println("MyClass2 is printed again");

}

}

public class HelloWorld{

public static void main(String []args){

MyClass klass = new MyClass2();

klass.say("h"); //Question 1 (Prints: "Hello MyClass2 h")

klass.print(new MyClass2()); //Question 2 (Prints: "MyClass is printed")

klass.print(new MyClass()); //Question 3 (Prints: "MyClass is printed")

klass.anotherPrint(1); //Question 4 (Prints: "MyClass is printed again")

klass.anotherPrint(1.0); //Question 5 (Throws Exception!)

}

}

I have the following questions:

1. The klass object is instance of MyClass. Why does it execute the method from the MyClass2 class?

2,3 . At question 1 klass calls the method of the Class2 class. Here I used a parameter that fits to each one of the overridden and overloaded (simultaneously) methods. Why klass object always calls the method from the MyClass class?

4. It is normal. No question at all.

5. It is right to throw an exception. The klass object does not have this method with double parameter. But, why it is not called the method from the MyClass2 class, just like it happened at Question 1?

解决方案

1. The klass object is instance of MyClass.

No it is a reference variable of type MyClass but referring to an object of MyClass2.

2. Why does it execute the method from the MyClass2 class?

Since you are invoking say() on an object of MyClass2, it executes the say() of MyClass2. Expected behavior.

This is called the run time polymorphism in Java. This provides the ability to override functionality already available in the class hierarchy tree. At run time, which version of the method will be invoked is based on the type of actual object stored in that reference variable and not on the type of the reference variable.

3. At question 1 klass calls the method of the Class2 class. Here I used a parameter that fits to each one of the overridden and overloaded (simultaneously) methods. Why klass object always calls the method from the MyClass class?

That is not overridden method. An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.An overriding method can also return a subtype of the type returned by the overridden method. This is called a covariant return type.Your method signatures are different. Hence invoking klass.print() where klass is a MyClass reference will always refer to the print() of MyClass.

4. It is right to throw an exception. The klass object does not have this method with double parameter. But, why it is not called the method from the MyClass2 class, just like it happened at Question 1?

Because at compile time, the compiler validates if you can call a method based on the reference type. Here the reference type is MyClass and since MyClass doesn't define anotherPrint(double), the compiler complains.It is a compile time check.In question 1, compiler verified klass.say("hi") and it saw that there exists a method in MyClass which can be invoked this way. At that time, it was not concerned whether klass reference variable will refer to a MyClass object or MyClass2 object at runtime. Hence it worked.

You can refer to these Oracle's tutorials here.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值