Web technology

web developers learning site

Typecasting of object in java programming

class A{
int a = 5;
void test()
{
System.out.print(“A”);
}
}
class J extends A{
int a = 10;
void test(){
System.out.println(“J”);
}
}

public class M extends J  {

int a = 15;
void test()
{
System.out.println(“M”);
}
public static void main(String args[])
{
J obj = new M();
M k = (M)obj;
A a = (A)k;

System.out.println(obj.a +”  “+k.a + ”  “+a.a);
k.test();
a.test();
}
}

 

Output ::

10  15  5
M
M

 

Single Post Navigation

Leave a comment