11.11. Inheritance and Constructors

How do you initialize inherited private fields if you don’t have direct access to them in the subclass? In Java you can put a call to the parent constructor using the keyword super as the first line in a subclass constructor to initialize inherited fields. See the constructor in Employee below for an example.

The super(theName) in the Employee constructor will call the constructor that takes a String object in the Person class to set the name.

If a class has no constructor in Java, the compiler will add a no-argument constructor. A no-argument constructor is one that doesn’t have any parameters.

public Person()

The code above is a no-argument constructor for the Person class. Remember that constructors don’t have a return type and the constructor name must match the class name.

If a subclass has no call to a superclass constructor using super as the first line in a subclass constructor then the compiler will automatically add a super() call as the first line in a constructor. So, be sure to provide no-argument constructors in parent classes or be sure to use an explicit call to super as the first line in the constructors of subclasses.

    10-6-1: Given the class definitions of Point2D and Point3D below, which of the constructors that follow (labeled I, II, and III) would be valid in the Point3D class?

    class Point2D {
       public int x;
       public int y;
    
       public Point2D() {}
    
       public Point2D(int x,int y) {
         this.x = x;
         this.y = y;
       }
       // other methods
    }
    
    public class Point3D extends Point2D
    {
       public int z;
    
       // other code
    }
    
    // possible constructors for Point3D
    I.  public Point3D() {}
    II. public Point3D(int x, int y, int z)
        {
           super(x,y);
           this.z = z;
        }
    III. public Point3D(int x, int y)
         {
            this.x = x;
            this.y = y;
            this.z = 0;
         }
    
  • II only
  • I is true because Point2D does have a no-arg constructor. II is true because Point2D does have a constructor that takes x and y. III is true because Point2D does have a no-arg constructor which will be called before the first line of code is executed in this constructor. The fields x and y are public in Point2D and thus can be directly accessed by all classes.
  • III only
  • Point2D does have a constructor that takes an x and y value so this is okay. Also the call to super is the first line of code in the child constructor as required. However, both I and III are okay as well.
  • I and II only
  • The x and y values in Point2D are public and so can be directly accessed by all classes including subclasses. Also there is a no-arg constructor in Point2D so the super no-arg constructor will be called before the first line of code in this constructor.
  • I, II, and III
  • I is true because Point2D does have a no-arg constructor. II is true because Point2D does have a constructor that takes x and y. III is true because Point2D does have a no-arg constructor which will be called before the first line of code is executed in this constructor. The fields x and y are public in Point2D and thus can be directly accessed by all classes.

You can step through this code in the Java Visualizer by clicking on the following link Constructor Test1.

    10-6-2: Given the class definitions of Point 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;
         }
    
  • I only
  • I is okay but III is also okay.
  • I and III
  • NamedPoint will inherit from Point all fields but the fields are private and they can not be directly accessed in NamedPoint. You can use super as the first line in a constructor to initialize inherited fields. You can also set your own fields in a constructor. If you don't use super as the first line in a constructor one will be put there by the compiler that will call the parent's no argument constructor.
  • II only
  • II is invalid. Children inherit all of the fields from a parent but do not have direct access to private fields. You can use super in a constructor to initialize inherited fields by calling the parent's constructor with the same parameter list.
  • III only
  • I is also okay

You can step through this code using the Java Visualizer by clicking the following link Named Point.

Next Section - 11.12. Abstract Classes