• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor question

 
RAGU KANNAN
Ranch Hand
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome explanation...

Thanks, Raghu.K
 
Aum Tao
Ranch Hand
Posts: 210
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic