Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# declaration for a class used to represent points in xy-coord. plane

Daniel Lucas
Ranch Hand
Posts: 48
This is question #4 on the collegeboard AP Comp Sci A practice review multiple choice questions from the AP Central website:

Consider the following declaration for a class that will be used to represent points in the xy-coordinate plane.

public class Point
{
private int myX; // coordinates
private int myY;

public Point ( )
{
myX = 0;
myY = 0;
}

public Point(int a, int b)
{
myX = a;
myY = b;
}

// ... other methods not shown
}

The following incomplete class declaration is intended to extend the above class so that points acn be named.

public class NamedPoint extends Point
{
private String myName;

// constructors go here

// ... other methods not shown
}

Consider the following proposed constructors for this class.

I. public NamedPoint()
{
myName = "";
}

II. public NamedPoint(int d1, int d2, String name)
{
myX = d1;
myY = d2;
myName = name;
}

III. public NamedPoint(int d1, int d2, String name)
{
super(d1, d2);
myName = name;
}

Which of these constructors would be legal for the NamedPoint class?

(A) I only
(B) II only
(C) III only
(D) I and III
(E) II and III

My analysis:

OK well I'm not familiar with anything that represents points in the xy-coordinate plane, but that doesn't seem to affect my ability to answer this question.

So we have a public class called Point. It has private integer coordinates declared as myX and myY, respectively for the X and Y coordinates. (SIDE NOTE: Why do they declare int myX and myY as private?)

Next we have a public method called Point. It initializes myX and myY to 0.

Next we have the Point method which we're going to make look like int a, int b. We're declaring myX = a and myY = b.

Some other methods apparently are needed to make this run in a compiler.

Now we have an incomplete class declaration which extends the Point class so that points can be named.

We have a public class called NamedPoint that extends Point. (SIDE NOTE: What is the purpose of extending a class? What does it really do? I've been doing it all year and never really understood it.)

Now we declare a private String called myName.

Some constructors and methods are not shown. Looking below, it looks like the purpose of this question is to see if I know what constructos should go below here to make this program work.

OK so I need to figure out which of the three constructos would work.

The first one doesn't seem to do anything.

The second one declares myX and myY to equal d1 and d2, respectively. Then it declares myName = name. Also, it makes NamedPoint look like coordinate X, coordinate Y, name). This seems right to me.

The third one is the same except it does something with super and does not declare what d1 and d2 are equal to. I know in an inheritance hierarchy that super refers to the parent class of a class. But I don't think that has anything to do with this problem, and it makes no sense to me using it in this way. I don't think III works.

So that means II only, C. Is it right? *checks back at answer* No, the answer is D. So that means it's the opposite of what I thought. Wonderful! So apparently I don't know what the heck I'm talking about!

HELP anyone?

Thinking about it some more I think I misread the question. It says which of these constructors would be LEGAL for the NamedPoint class. Apparently I and III are legal, but why? Why isn't II legal?

Rusty Shackleford
Ranch Hand
Posts: 490
A private variable is only accesible in that class. NamedPoint is not part of the Point class, therefore it can not directly access the integers in Point. You asked why are they private, that is the heart of the issue here. If they were protected, code II would work, but still be wrong IMO. Always use super to set variables in the parent class in the child constructor.

More often then not class level variables should be either private or protected. Making them public is something rarely done ebcasue it breaks encapsulation and one of the major points of OOP. Normally those two variables would likely be protected, but they do not have to be.

This exercise is designed to test your knowlege of visibility modifiers and a bit on inheritance, so go over them again.

Daniel Lucas
Ranch Hand
Posts: 48
That makes perfect sense. Thank you! These are all things I've covered when reading the chapters. Not all of the concepts have really clicked yet, however.