• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic