Skip to main content

Posts

Showing posts from August, 2013

What will be the Output of this Program?

What will be the Output of this Program? class P    {        int z=2;     P()     {        System.out.println("Default");     }     P(int x)     {        this();        System.out.println("one argument");     }     P(int m, int n)     {          this(z);          System.out.println("Argumented");     }         public static void main(String arr[])        {         P p = new P(1, 2);               }   }

Brain_Teaser Questions

1. Can we define a class within a constructor? 2. What will happen if we define a static nested class in a constructor? Given:  If Class A (class A has only one parametrized constructor) is the super class of B (Class B has two parametrized constructors). 3. Can we invoke the parametrized constructor of class A and the parametrized constructor of class B from the other constructor of class B. 4. What will happen if we create the object of class B:     B b1 = new B(5);     assume that there is a constructor B(int x) in class B.

Casting the Objects

class A   {       // body of class A   } class B extends A    {       // body of class B    } We can cast the subclass object to a super class reference variable. i.e. Super class reference variable can hold the subclass object... A a = new B(); But Subclass reference variable can not hold the super class object. WHY? B b = new A();                     // Compile time error