java编辑继承,java编译器如何在继承中选择正确的方法和变量

I am a bit confused when inheritance and type casting is mixed. I want to understand the rules which java compiler follows when choosing correct methods and variables in inheritance.

I have read something like

Variables are bound at compile time and methods are bound at run time.

The second one is from stackoverflow (@John Skeet)

Overload resolution (which method signature is called) is determined at compile-time, based on the compile-time types of both the method target and the argument expressions

The implementation of that method signature (overriding) is based on the actual type of the target object at execution time.

But the problem is that they are explanations of specific situations and do not give the general process to be followed when other factors (like exception handling) are taken into consideration.

This may be bad practice but assume that both methods and variables are overridden(hidden in case of static variables).

Now if java compiler has to choose at compile time which method/variable needs to be invoked then what algorithm will it follow? Similarly at run time what is the algorithm that java compiler will use(based on the actual type of the object whose reference is being used)?

解决方案

All method signatures and variables are verified during compile-time but the actual method calls are done / resolved during run-time.

For example :

class A {

int i=5;

public void doSomething(){

//print "in A"

}

}

class B extends A{

int i=10;

public void doSomething(){

// print "in B"

}

public static void main(String[] args){

A a = new B();

a.doSomething();

}

}

Now, when you call a.doSomething(); , during compilation, the compiler just checks whether doSomething() is defined for class A (lhs reference). It is not even bothered about whether the method is also defined for class B. Even if the method were not present in B, the program would compile fine.

Next, during runtime, the JVM dynamically decides which method to call based on type of Object (B in your case.).

So, "in B" is printed.

Now, coming back to fields. Access to fields are resolved during compile time. So, if a field doesn't exist during compile-time, then the compilation fails. The fields are called based on the reference type. So, a.i will print 5 (A's value of i) because the field was resolved during compile-time. Thus, the difference is, method calls are resolved during runtime, their signatures are needed / checked during compile-time where-as fields are checked / resolved during compile-time.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值