• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

simple q on interface

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic