Java – Method override

class A
class A
{
  int i=10;
  void show()
  {
    System.out.println("class A");
  }
}

class B extends A
{
  int i=5;
  public void show()
  {
    System.out.println("class B");
  }
}
class M
{
  public static void main(String s[])
  {
    A a=new B();
    a.show();
    System.out.println(a.i);
  }
}


OUTPUT= class B
        10

If a class a method is overridden by a class B method, why is the variable 'I'?

Solution

Because variables are not virtual, there are only methods

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>