wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes plz explain and tell out put Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "plz explain and tell out put " Watch "plz explain and tell out put " New topic
Author

plz explain and tell out put

Akhilesh Yadav
Ranch Hand

Joined: Apr 04, 2006
Posts: 46
public class A {
public A(){
super();
System.out.println("instantiate a parent");
}
}

public class B extends A{
public void child (){
System.out.println("instantiate a child");
}
}
public class C {
public static void main(String [] args){
A a = new B();
}
}

wht would be the output :-
a) intantiate a parent
b)intantiate a child
c) both a and b
Roy Simon
Ranch Hand

Joined: Aug 26, 2005
Posts: 62
hi,
the output would be option a followed by option b
the reason is that before a subclass object is created its super class instance is initiated first (its like every sublass object has a super class object inside it)

Regards
Simon
Chandrasekhar Mangipudi
Ranch Hand

Joined: Jan 29, 2006
Posts: 118
Instantiate a parent is the right answer.Here we are creating object for B class and before the execution of the B class Constructor it calls super class constructor of A class and executes it without complation error.


Thanks & Regards,
ChandraSekharMangipudi
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Hi,

public class A
{
public A() // Class A's constructor
{
super();
System.out.println("instantiate a parent");
}
}

public class B extends A // Class B with no constructor
{
public B()
{
super();
}

public void child () // Not a constructor
{
System.out.println("instantiate a child");
}
}

public class C
{
public static void main(String [] args)
{
A a = new B();
}
}

1)In Java whenever you do not provide a constructor for a class compiler inserts the default contructor with a call to super class constructor in your class.

2)Now, due to inheritance whenever you create a subclass's object it first invoke its super class's contructor and after the superclass object creation completes then subclass constructor is executed.

In your program 1) & 2) both are valid as you don't have a constructor for a subclass B which is extending from class A. So when you write

A a= new B();

compiler invokes its default constructor in which there is (no print system)just a call to super, so it will print

instantiate a parent

nothing more.

Since public void child(){System.out.println("instantiate a child");}
never gets executed, you will never see this message printed on the screen.

Hope this helps you.
Asha


SCJP 1.5
Sandeep Singh Kunwar
Ranch Hand

Joined: Feb 27, 2006
Posts: 52
I think 'Asha Pathik' has explained everything in this regard.


OfCourse Learner...<br />SCJP 5, SCWCD1.4<br />Blogs: <a href="http://sandeepkunwar.blogspot.com" target="_blank" rel="nofollow">http://sandeepkunwar.blogspot.com</a>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: plz explain and tell out put
 
Similar Threads
Order of Execution(Interesting)-:)
init blocks example
Static and Inheritance - strange java...why is this happening ?
Problem -- Virtual Method Invocation
Overriding/overloading