Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Objects and reference variables of different types



//for example only
class A
{}
class B extends A
{
public static void main (String [] args)
{
  A a1 = new A();
  A a2 = new B();  //my doubt lies here
  System.out.println(a1);
  System.out.println(a2);
}
}

When it is declared as "A a1 = new A();", object is created to class A and 'a1' refers to the newly created object.

But, when it is declared as "A a2 = new B();", what does it imply? I am confused why should a class B object be referred by reference variable of A ?

I request to help me making this concept clear, please.