aspose file tools*
The moose likes Beginning Java and the fly likes Need Help With This java Problem(Review for a test) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need Help With This java Problem(Review for a test)" Watch "Need Help With This java Problem(Review for a test)" New topic
Author

Need Help With This java Problem(Review for a test)

Jame Brown
Greenhorn

Joined: Mar 18, 2006
Posts: 4
I am reviewing for a test and have am not sure about a question(sure more to come later)

For the question(s) below, consider the following class definition:

public class AClass
{
protected int x;
protected int y;

public AClass(int a, int b)
{
x = a;
y = b;
}

public int addEm( )
{
return x + y;
}

public void changeEm( )
{
x++;
y--;
}

public String toString( )
{
return "" + x + " " + y;
}
}

4) Consider that you want to extend AClass to BClass. BClass will have a third int instance data, z. Which of the following would best define BClass' constructor?
A) public BClass(int a, int b, int c)
{
super(a, b, c);
}
B) public BClass(int a, int b, int c)
{
x = a;
y = b;
z = c;
}
C) public BClass(int a, int b, int c)
{
z = c;
}
D) public BClass(int a, int b, int c)
{
super(a, b);
z = c;
}
E) public BClass(int a, int b, int c)
{
super( );
}

I think it is D but I could be wrong(probably am lol) I think this because of inheritance I figure since a,b don't change from A(well I think they don't) so why not call them from the superclass and then add the 3rd one.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Yes, D is correct. In fact, all of the other options will fail.

A default "no-args" constructor is automatically provided by the compiler only if no other constructors are provided. In this case, AClass has a constructor that takes two ints, so it does not have a no-args constructor.

If the first line of a constructor is not an explicit call to this (an overloaded constructor for the same class) or an explicit call to super (a superclass constructor), then there is an implicit call to super with no-args. So options B and C both contain an implicit call to a no-args super, which does not exist. Option E contains an explicit call to this non-existant constructor.

Option A tries to call a super constructor with 3 int arguments. This constructor also does not exist.

But even if AClass had a no-args constructor, there is another important reason for the approach in option D.

BClass extends ("is-a") AClass. So in order to construct an instance of BClass, we first need a complete instance of AClass to use as a "foundation." Otherwise, a BClass constructor runs the risk of trying to access a member that's not properly initialized. This is why there is a call to super -- implicitly, if not explicitly -- as the first line of any constructor (other than the base class Object).

In this case, because variables x and y are declared in AClass, and AClass has a constructor to initialize these, this is where these variables should be initialized. This way, when the body of BClass's constructor executes, it has a properly initialized instance of AClass to work with.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Help With This java Problem(Review for a test)