aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes simple q on interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "simple q on interface" Watch "simple q on interface" New topic
Author

simple q on interface

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
class Building{
Building(){
System.out.println("Building");
}
}

class House extends Building{
private int iBedroomCount;
public int getBedroomCount(){
return iBedroomCount;
}
public void setBedroomCount(int iBedRoomCount){
this.iBedroomCount=iBedroomCount;
}

}


public class Terrace extends House{
public static void main(String argv[]){
new Terrace();
}
Terrace(){
Building b = new House();
}
}

Options are
1.Compilation error, class Building is lacking a no args constructor
2.Compilation and output of "Building" twice at runtime
3.Compilation, but no output at runtime

Can anyone predict the o/p and expalin why he has choosen that answer in brief


Thanks<br />Dinesh
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
Dinesh posted:
class Building{
Building(){
System.out.println("Building");
}
}

class House extends Building{
private int iBedroomCount;
public int getBedroomCount(){
return iBedroomCount;
}
public void setBedroomCount(int iBedRoomCount){
this.iBedroomCount=iBedroomCount;
}

}


public class Terrace extends House{
public static void main(String argv[]){
new Terrace();
}
Terrace(){
Building b = new House();
}
}

Options are
1.Compilation error, class Building is lacking a no args constructor
2.Compilation and output of "Building" twice at runtime
3.Compilation, but no output at runtime

Can anyone predict the o/p and expalin why he has choosen that answer in brief


The answer is 2.

Lets see how..

Start from main.
It instantiates Terrace.
the Terrace calls ite super class's (House) no arg constructor.
Since it does not have one, a default is provided by java.
Now it calls its super classes (Building) constructor and there it
prints once.

Now the control is returned in Terrace's constructor.
Now it again instantiates Building and now it prints 2nd time.

Hope i have cleared your concept.

Cheers
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
the reply he is 2. One remembers that the inherited constructors are overloaded and nao, and when you do not create one, the compiler creates automaticamente(construtor standard). He does not have no breaking in the code, because the constructors are one calling the other until arriving class main - Object. E the arguments of the constructors are compatible.


Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Hi Amol,

Thanks for expalining.

I more doubt i have
Building b = new House();
Is the type of casting called upcasting or downcasting.

Can you expalin the difference between two.

nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
I don't think it is casting.

Building b=new Horse();//this is not at all casting this what i feel.
Horse=(Horse)b;//this is casting

//downcasting
Object a=new Horse();
Horse=(Horse)a;//same as with building

//upcasting
Horse h=new Horse();
Object Ob=h

anybody correct if i am wrong
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by dinesh tahiliani:
Building b = new House();
This is upcasting. But since the compiler is able to prove the correctness, it's done automatically.
 
Don't get me started about those stupid light bulbs.
 
subject: simple q on interface