• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constructor question

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guru's,

The answer for the following question is B and C, I couldn't understand the answer E.
Pls explain to me.

Thanks, Raghu/K
-------------------------------------------------

class A{
public A(){}
public A(int i) { this();}
}
class B extends A{
public boolean B(String msg){return false;}
}
class C extends B{
private C() { super() }
public C(String msg) {this();}
public C(int i){}
}
---------------------------------------
A. The code will fail to compile.
B. The constructor in A that takes an int as an argument will never be called as a result of constructing
an object of class B of C.
C. Class C define three constructors.
D. Objects of class B cannot be constructed.
E. At most one of the construcotrs of each class is called as a result of constucting an object of class.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class can have overloaded constructors, as illustrated here in classes A and C. A constructor can call another constructor of the same class using the keyword this along with the appropriate arguments. (Note: You don't want to call another constructor by name, because then you would be creating another object.)

In this example, the class A constructor taking an int argument calls the overloaded no-args constructor.

Similarly, the class C constructor taking a String argument calls the overloaded no-args constructor.

So option E ("at most one of the constructors of each class is called...") is false, because calling A(int i) results in A() also being called, and calling C(String msg) results in C() also being called.
 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome explanation...

Thanks, Raghu.K
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, remember that this() and super() should be the first statement in your constructor. However, only one of them can occur in your constructor definition.
 
Do you pee on your compost? Does this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic