Why is the output different in the two cases?

前端 未结 3 1935
慢半拍i
慢半拍i 2021-01-22 06:10

Why is the output different in the below case even when, the variable has been overridden?

public class A {
    int a = 500;

    void get() {
        System.out         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-22 06:35

    When doing ob.a, you get the variable int a from your ob object, which is object of the class B.

    However, when you do ob.get();, you are calling the get()-method from class A, because there is no get() in B, which - as you wrote - uses this.a, which would be the int a of class A in that case.

提交回复
热议问题