Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple q on interface

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic