Object Type Casting


class A {
int x = 10;

int get() {
return x;
}
}

class B extends A {
int x = 20;

int get() {
return x;
}
}

public class Mains {
public static void main(String[] args) {
B b = new B();
System.out.println(b.get()); //20
System.out.println(b.x); //20

A a = new B();
System.out.println(a.get()); //20
System.out.println(a.x); //10

A a1 = new A();
System.out.println(a1.get()); //10
System.out.println(a1.x); //10
}
}

Comments

Popular posts from this blog

Android - Using KeyStore to encrypt and decrypt the data

Stack and Queue

Java Reflection API