Java. Inheritance. A reference to a subclass object. Calling the superclass constructor. The keyword super




Inheritance. A reference to a subclass object. Calling the superclass constructor. The keyword super


Contents


Search other websites:

1. Is it possible a link to a superclass assign a reference to an object of a subclass?

Yes, it is. Let a superclass and a subclass be given. If you declare a reference to a superclass, then this reference can be assigned the value of a reference to a subclass that inherits this superclass.

For example. Given two classes A and B. Class A is a superclass (base class). Class B is a subclass of class A.

// class A is a superclass for class B
class A {
    int a; // internal variable
}

// class B - subclass of class A
class B extends A {
    int b;
}

Then, in another method, you can declare a reference to class A and assign it value of an instance of class B

// reference to class A may refer to a class B
A objA = new A(); // instance (object) of class A
B objB = new B(); // instance (object) of class B
A rA; // reference to class A

// a reference to an instance of class B is assigned to reference to class A
rA = objB;

rA.a = 30; // it is possible, rA.a = 30
// rA.b = 20; // error, forbidden
rA = objA; // this is also possible, however rA.a = 0

In the case when the reference to the superclass rA is assigned a reference objB to an object of the subclass, then access by reference rA can be obtained to members of class A. In this case, for the reference rA, a class A member is available with the name a. Class B members are not available for reference rA.

Conclusion: for the reference to the superclass, the available members of the class are determined by the type of reference, and not the type of object this reference refers to.

If using the rA reference you will try to access the data member b of class B

r.b = 20; // error!

the compiler will generate an error

b cannot be resolved or is not a field

 

2. Is it possible a reference to a subclass assign a reference to a superclass object?

No, it is not. The superclass can not be extended to the subclass opportunities. This is contrary to the paradigm of inheritance. Conversely, a subclass that extends the capabilities of a superclass may “narrow” to a superclass.

For example. Let two classes A and B be given. Class A is a superclass (base class). Class B is a subclass of class A.

// class A is a superclass for class B
class A {
    int a; // internal variable
}

// class B - subclass of class A
class B extends A {
    int b;
}

Attempting to assign a reference to class B to the value of reference to class A

A objA = new A(); // objA - reference to instance of class A
B rB; // reference to subclass B

// superclass A cannot be cast to class B
rB = (B)objA; // error!

will lead to a critical situation with the message

A cannot be cast to B

 

3. What are the forms of using the super keyword?

There are two forms of using the super keyword:

  • to call the constructor of the superclass;
  • to access the superclass member from the subclass. Such access is needed in situations where the name of a member of a subclass matches the name of a member of a superclass to which you need to access from a subclass. In this case, the subclass data member name overrides the name of the superclass.

 

4. How is a call made from a subclass of a superclass constructor? The keyword super

The constructor of a superclass (base class) can be invoked from a subclass (derived class). This call is made using the super keyword. The call to the superclass constructor must be made first in the body of the subclass constructor.

The general form for invoking a superclass constructor from a subclass constructor is as follows:

super(parameters);

here

  • parameters – list of parameters that the constructor receives. If the constructor has no parameters, then the superclass constructor is called as super().


 

5. Examples of calling superclass constructors from a subclass

Example 1. A Point class is defined that describes a point on the plane. The class is extended by a subclass of ColorPoint, which adds a color property to a point.

The implementation of the Point and ColorPoint classes is as follows

// a class that describes a point on a plane
class Point {
    // coordinates of a point
    private double x;
    private double y;

    // class constructor
    public Point()
    {
        x = y = 0;
    }

    public Point(double _x, double _y) {
        x = _x;
        y = _y;
    }

    // access methods
    // get the point
    public Point GetP() {
        return this;
    }

    // set a new value of point
    public void SetP(Point np) {
        x = np.x;
        y = np.y;
    }

    public double GetX() { return x; }
    public double GetY() { return y; }
}

// class point with color - extends the capabilities of Point
class ColorPoint extends Point {
    private double color;

    // constructors of class ColorPoint
    ColorPoint() {
        // call the constructor without the parameters of the superclass Point
        super();
        color = 0;
    }

    ColorPoint(double _x, double _y, double _c) {
        // call a constructor with two parameters of the Point class
        super(_x,_y);
        color = _c;
    }

    // set a new value ColorPoint
    void SetCP(double _x, double _y, double _c) {
        // super(_x,_y); // it is forbidden to call a constructor from a method
        Point p = new Point(_x,_y); // create an object of class Point
        SetP(p); // set a new value of point coordinate
        color = _c;
    }

    // get the color
    public double GetColor() {
        return color;
    }
}

In the above code, the constructors of the ColorPoint subclass call the constructor of the class Point using the keyword super

...

// the constructors of class ColorPoint
ColorPoint() {
    // call the constructor without the parameters of the superclass Point
    super();
    color = 0;
}

ColorPoint(double _x, double _y, double _c) {
    // call a constructor with two parameters of the Point class
    super(_x,_y);
    color = _c;
}

...

Example 2. A call to the superclass constructor in the case of multi-level inheritance. The example declares 4 classes with the names A1, A2, A3, A4. Class A1 is a superclass for class A2. Class A2 is a superclass for class A3. Class A3 is a superclass for class A4.

Below is the program code that demonstrates the use of the super keyword and multilevel inheritance

// superclass
class A1 {
    A1() {
        System.out.println("A1");
    }
}

// subclass of class A1
class A2 extends A1 {
    A2() {
        super();
        System.out.println("A2");
    }
}

// subclass of class A2
class A3 extends A2 {
    A3() {
        super();
        System.out.println("A3");
    }
}

// subclass of class A3
class A4 extends A3 {
    A4() {
        super();
        System.out.println("A4");
    }
}

If you create an instance of class A4

A4 objA4 = new A4();

then the following result will be displayed

A1
A2
A3
A4

In the above example, class A2 inherits all the characteristics of class A1. Class A3 inherits all the characteristics of classes A1, A2. Class A4 inherits all the characteristics of classes A1, A2, A3.

 

6. What requirements apply to invoking a superclass constructor using the super keyword?

Calling the superclass constructor using the super keyword includes the following requirements:

  • the superclass constructor can only be called from the subclass constructor;
  • the call to the superclass constructor must be first in the constructor of the subclass;
  • it is forbidden to call the superclass constructor from any method of the subclass (except constructor).





 

7. Is it possible to call the superclass constructor from any subclass method using the super call?

No, it is not. If in some subclass method you try to call the superclass constructor using the super keyword, the Java compiler will generate an error:

Constructor call must be the first statement in a constructor

 

8. Example of accessing a superclass member from a subclass using the super keyword

The super keyword is used to access a member of a superclass if the exact same name is used in the subclass.

// access to a superclass member with the word super
class A {
    double x = 2.5;
}

class B extends A {
    double x = 5.0; // variable x of class A
}

class C extends B {
    double x = super.x; // x = 5.0 - variable x of class B
}

In the above example, the x member of class C is initialized with the x value of class B using a call:

super.x

 

9. How is the order of constructor calls in the case of inheritance? Example

If two or more classes form a hierarchy of inheritance, then constructors are called from the highest class in the hierarchy (from the superclass) to the lowest (to the subclass of the lowest level).

For example. Given the four classes with the names A, B, C, D. Class A is a superclass for class B. Class B is a superclass for class C. Class C is a superclass for class D.

// class A - the highest class in the hierarchy
class A {  
    // constructor of class A
    A() {
        System.out.println("The constructor of class A.");
    }
}

// class B is a subclass of class A
class B extends A {
    // constructor of class B
    B() {
        System.out.println("The constructor of class B.");
    }
}

// class C is a subclass of class B
class C extends B {
    // constructor of class C
    C() {
        System.out.println("The constructor of class C.");
    }
}

// class C is a subclass of class D
class D extends C {
    // constructor of class D
    D() {
        System.out.println("The constructor of class D.");
    }
}

public class Train01 {

    public static void main(String[] args) {
        // create an object of class D
        D obj = new D(); // constructors are called in the order A(), B(), C(), D()         
    }

}

In the above example, when declaring an object of class D

D obj = new D();

constructors are called in order

A()
B()
C()
D()

The result of the program

The constructor of class A.
The constructor of class B.
The constructor of class C.
The constructor of class D.

 


Related topics