Activity 5.2.1.
Try creating another Employee object in the main method that passes in your name and then use the get methods to print it out. Which class constructor sets the name? Which class constructor sets the id?
public
methods from the superclass that they extend, but they cannot access the private
instance variables of the superclass directly. And subclasses do not inherit constructors from the superclass. But inherited instance variables need to be properly initialized or none of the inherited methods are likely to work properly, so how can a subclass initialize the superclassβs private
variables?
public
setter methods for those variables the subclass could use those. But that wonβt always be the case. And sometimes constructors do more complex initialization than just setting variables.
super
. When used like the name of a method, i.e. when followed with parentheses and arguments, super
provides a way to call the code in a superclass constructor, passing whatever arguments it needs. But unlike when we call a constructor with new
, a call to super
doesnβt create a new object. Instead it runs the constructorβs code in the context of the object currently being constructed. This lets the superclass constructor initialize the instance variables declared in the superclass including private
variables the subclass canβt directly access.
super
Java will automatically insert a call to super
with no arguments. (That means if the superclass does not have a no-argument constructor that the subclasses will have to explicitly call super
with the appropriate arguments for some constructor that does exist. This ensures that instances of the subclass are properly initialized.)
super(theName)
in Employee
below runs the code in the Person
constructor that takes a String
argument which presumably initializes an instance variable in the Person
class to hold the name.
public class Employee extends Person
{
public Employee(String theName)
{
super(theName); // calls Person(String) constructor
}
}
super
to call an existing constructor on the superclass with appropriate arguments.
super
calls from each subclass to its superclass ends in the no-argument constructor of java.lang.Object
. This is a special class defined in Java which is the superclass of any class that doesnβt explicitly extend
some other class and the only class with no superclass and thus no super
constructor that needs to be called.
Object
it starts unwinding, with first the Object
constructor code running, then the constructor from its subclass, and so on until finally the constructor of the actual class being constructed runs. At that point any inherited instance variables will have been initialized so the constructor can safely call inherited methods that depend on those variables.
MPoint
and NamedPoint
below, which of the constructors that follow (labeled I, II, and III) would be valid in the NamedPoint
class?
class MPoint
{
private int myX; // coordinates
private int myY;
public MPoint( )
{
myX = 0;
myY = 0;
}
public MPoint(int a, int b)
{
myX = a;
myY = b;
}
// ... other methods not shown
}
public class NamedPoint extends MPoint
{
private String myName;
// constructors go here
// ... other methods not shown
}
// 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;
}
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=class+MPoint%0A%7B%0A+++private+int+myX%3B+//+coordinates%0A+++private+int+myY%3B%0A%0A+++public+MPoint(+)%0A+++%7B%0A++++++myX+%3D+0%3B%0A++++++myY+%3D+0%3B%0A+++%7D%0A%0A+++public+MPoint(int+a,+int+b)%0A+++%7B%0A++++++myX+%3D+a%3B%0A++++++myY+%3D+b%3B%0A+++%7D%0A%0A+++//+...+other+methods+not+shown%0A%0A%7D%0A++++++%0Apublic+class+NamedPoint+extends+MPoint%0A%7B%0A+++private+String+myName%3B%0A+++%0A+++//+constructors+go+here%0A+++//+I.%0A+++public+NamedPoint()%0A+++%7B%0A++++++myName+%3D+%22%22%3B%0A+++%7D%0A+++%0A+++//+II.%0A+++//+public+NamedPoint(int+d1,+int+d2,+String+name)%0A+++//+%7B%0A+++//++++myX+%3D+d1%3B%0A+++//++++myY+%3D+d2%3B%0A+++//++++myName+%3D+name%3B%0A+++//+%7D%0A+++%0A+++//+III.%0A+++//+public+NamedPoint(int+d1,+int+d2,+String+name)%0A+++//+%7B%0A+++//++++super(d1,+d2)%3B%0A+++//++++myName+%3D+name%3B%0A+++//+%7D%0A+++%0A+++public+static+void+main(String%5B%5D+args)%0A+++%7B%0A++++++NamedPoint+nPt+%3D+new+NamedPoint()%3B%0A++++++//+NamedPoint+nPt+%3D+new+NamedPoint(3,+2,+%22home%22)%3B%0A++++++//+NamedPoint+nPt+%3D+new+NamedPoint(5,+4,+%22work%22)%3B%0A+++%7D%0A%0A%7D&mode=display&curInstr=0
Rectangle
that has two instance variables, length
and width
, a constructor that initializes them, and a method called draw
that uses nested loops to draw a length
x width
rectangle of stars. Try it out below.
Square
that inherits from Rectangle
. Is a square a rectangle? Yes! A square is a rectangle where the length and width are equal. Square
will inherit length
, width
, and the draw
method. You will write Square
constructors that will call the Rectangle
constructors.
Square
constructor with 1 argument for a side that calls Rectangle
βs constructor with 2 arguments using super
.
main
method to test drawing the squares.
area
method to Rectangle
that computes the area of the rectangle. Does it work for Square
s too? Test it.
LongRectangle
which inherits from Rectangle
but has the additional condition that the length is always 2 x the width. Write constructors for it and test it out. Do not make it public (because only 1 class per file can be public).
Square
constructor with 1 argument for a side that calls Rectangle
βs constructor with 2 arguments using super
.
main
method to test drawing the squares.
area
method to Rectangle
that computes the area of the rectangle. Does it work for Square
s too? Test it.
LongRectangle
which inherits from Rectangle
but has the additional condition that the length is always 2 x the width. Write constructors for it and test it out. Do not make it public (because only 1 class per file can be public).
https://replit.com/@BerylHoffman/Shapes
https://www.dropbox.com/s/2lmkd1m2sfh3xqc/ShapeExample.zip
private
instance variables in a superclass that they extend.
super
and passing appropriate parameters. If there is no explicit call to super
an implicit call to super()
will be added by the Java compiler.
super
provide values that the superclass constructor can use to initialize the objectβs instance variables.
Object
constructor is called. At this point, all of the constructors within the hierarchy execute beginning with the Object
constructor.