Create a subclass with multiple constructors
Create a class named Rectangle
with two instance variables length
and width
of type int
. The Rectangle
class should have a constructor that accepts two int
parameters and assigns them to length
and width
. Then, create a subclass named Square
that extends Rectangle
. The Square
class should have two constructors: one that accepts a single int
parameter and passes it to the superclass constructor twice, and another that accepts no parameters and passes the number 1
to the superclass constructor twice.
5
1
1
5
1
1
1
1
1
1
1
1
9
1
1
9
1
1
Instructor solution
Think you've got it?
How do you create multiple constructors in a subclass in Java?
- A.
By defining a single constructor in the subclass with multiple parameters and calling
super
with different arguments based on conditional statements. - B.
By defining multiple constructors in the superclass and calling
super
with the same arguments in the subclass constructor. - C.
By defining multiple constructors in the subclass with different parameters and calling
super
with the appropriate arguments in each constructor. - D.
By defining a single constructor in the subclass with no parameters and calling
super
with different arguments based on user input.
You may exit out of this review and return later without penalty.