Java: Polymorphism don’t work for variables

02. августа 2016 JAVA, Programming 0

Example:

public class Test
{
    public static void main(String[] args)
    {
        class A{
            private int x;
            public int getX_A(){return this.x;}
        }

        class B extends A{
            public int x;
            public int getX_B(){return this.x;}
        }

        B b= new B();
        A a = b;
        a.x = 5;

        if(a == b){System.out.println("Object equals");}
        else {System.out.println("Object not equals");}
        
        if(a.getX_A() == b.getX_B()){System.out.println("Variables equals");}
        else {System.out.println("Variables not equals");}
    }
}

Добавить комментарий

Ваш адрес email не будет опубликован.