How do I instantiate an object with fields from the parent class?
I am trying to get an understanding of object oriented programming in Java
and I have this problem.
Say for example, I have a a parent class like this:
public class Shape {
private int location;
private Color color;
// methods such as getLocation() and getColor()
public Shape(int initialLocation, Color initialColor) {
location = initialLocation;
color = initialColor;
}
}
How do I make my child class so that I can construct, say, a rectangle
with an initial location and an initial color in a main method? Do I
create a constructor in the Rectangle class? I can't because location and
color are private fields. Do I create accessor methods for location and
color and just set the location and color after instantiation? I guess,
but is there a way to do this without accessors?
public class Rectangle extends Shape {
public Rectangle(int initialLocation, Color initialColor) {
super();
}
}
I just can't wrap my head around this fundamental concept. Any help?
No comments:
Post a Comment