Oop L6
Oop L6
When only the subclass defines a constructor, the process is straightforward: simply
construct the subclass object. The superclass portion of the object is constructed
automatically using its default constructor. For example, here is a reworked version of
Triangle that defines a constructor. It also makes style private since it is now set by the
constructor.
1
2022 OOP-LECTURE 6
}
void showStyle() {
System.out.println("Triangle is " + style);
}
}
class Shapes3 {
public static void main(String args[]) {
Triangle t1 = new Triangle("isosceles", 4.0, 4.0);
Triangle t2 = new Triangle("right", 8.0, 12.0);
System.out.println("Info for t1: ");
t1.showStyle();
t1.showDim();
System.out.println("Area is " + t1.area());
System.out.println();
System.out.println("Info for t2: ");
t2.showStyle();
t2.showDim();
System.out.println("Area is " + t2.area());
Here, Triangle’s constructor initializes the members of TwoDClass that it inherits along
with its own style field.
When both the superclass and the subclass define constructors, the process is a bit
more complicated because both the superclass and subclass constructors must be
executed.
2
2022 OOP-LECTURE 6
Here, Triangle( ) calls super( ) with the parameters w and h. This causes the
TwoDShape( ) constructor to be called, which initializes width and height using these
values. Triangle no longer initializes these values itself. It need only initialize the value
unique to it: style. This leaves TwoDShape free to construct its subobject in any
manner that it so chooses. Furthermore, TwoDShape can add functionality about
which existing subclasses have no knowledge, thus preventing existing code from
breaking.
Any form of constructor defined by the superclass can be called by super( ). The
constructor executed will be the one that matches the arguments. For example, here
3
2022 OOP-LECTURE 6
are expanded versions of both TwoDShape and Triangle that include default
constructors and constructors that take one argument.
// Add more constructors to TwoDShape.
class TwoDShape {
private double width;
private double height;
// A default constructor.
TwoDShape() {
width = height = 0.0;
}
// Parameterized constructor.
TwoDShape(double w, double h) {
width = w;
height = h;
}
// Construct object with equal width and height.
TwoDShape(double x) {
width = height = x;
}
// Accessor methods for width and height.
double getWidth() { return width; }
double getHeight() { return height; }
void setWidth(double w) { width = w; }
void setHeight(double h) { height = h; }
void showDim() {
System.out.println("Width and height are " +
width + " and " + height);
}
}
// A subclass of TwoDShape for triangles.
class Triangle extends TwoDShape {
private String style;
// A default constructor.
Triangle() {
super();
style = "null";
}
// Constructor
Triangle(String s, double w, double h) {
super(w, h); // call superclass constructor
style = s;
}
// Construct an isosceles triangle.
Triangle(double x) {
super(x); // call superclass constructor
style = "isosceles";
}
double area() {
return getWidth() * getHeight() / 2;
}
4
2022 OOP-LECTURE 6
void showStyle() {
System.out.println("Triangle is " + style);
}
}
class Shapes5 {
public static void main(String args[]) {
Triangle t1 = new Triangle();
Triangle t2 = new Triangle("right", 8.0, 12.0);
Triangle t3 = new Triangle(4.0);
t1 = t2;
System.out.println("Info for t1: ");
t1.showStyle();
t1.showDim();
System.out.println("Area is " + t1.area());
System.out.println();
System.out.println("Info for t2: ");
t2.showStyle();
t2.showDim();
System.out.println("Area is " + t2.area());
System.out.println();
System.out.println("Info for t3: ");
t3.showStyle();
t3.showDim();
System.out.println("Area is " + t3.area());
System.out.println();
}
}
Here is the output from this version.
Info for t1:
Triangle is right
Width and height are 8.0 and 12.0
Area is 48.0
Info for t2:
Triangle is right
Width and height are 8.0 and 12.0
Area is 48.0
Info for t3:
Triangle is isosceles
Width and height are 4.0 and 4.0
Area is 8.0
There is a second form of super that it always refers to the superclass of the subclass
in which it is used. This usage has the following general form:
super.member
Here, member can be either a method or an instance variable.
5
2022 OOP-LECTURE 6
Note: in multilevel inheritance in JAVA when we need to use three classes (A,B,C). We
must put the main method in the Class C to get read from errors.
public class A {
int i;
}
public class B extends A {
int i; // this i hides the i in A
B(int x, int y) {
super.i = x; // i in A
i = y; // i in B
}
void show() {
System.out.println("i in Grandclass: " + super.i);
System.out.println("i in ParentClass: " + i);
}
6
2022 OOP-LECTURE 6