Superclass methods with this level of access cannot be called from subclasses.

Subclasses inherit public methods from the superclass that they extend, but they cannot access the private instance variables of the superclass directly and must use the public accessor and mutator methods. And subclasses do not inherit constructors from the superclass.

So, how do you initialize the superclass’ private variables if you don’t have direct access to them in the subclass? In Java, the superclass constructor can be called from the first line of a subclass constructor by using the special keyword super() and passing appropriate parameters, for example super(); or super(theName); as in the code below. The actual parameters given to super() are used to initialize the inherited instance variables, for example the name instance variable in the Person superclass.

public class Employee extends Person
{
    public Employee()
    {
        super(); // calls the Person() constructor
    }
    public Employee(String theName)
    {
        super(theName); // calls Person(theName) constructor
    }
}

Coding Exercise

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

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?

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, for example public Person().

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

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;
     }
0 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
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;
     }
0 as the first line in the constructors of subclasses.

Regardless of whether the superclass constructor is called implicitly or explicitly, the process of calling superclass constructors continues until the Object constructor is called. At this point, all of the constructors within the hierarchy execute beginning with the Object constructor.

I saw the code below online and the subclass constructor calls the superclass constructor before initialising its own variable (i.e. public int seatHeight), after I change the order of initialisation, i.e. by putting seatHeight = startHeight; before super(startCadence, startSpeed, startGear);, my IDE displays error message. I was just wondering what is the reason behind calling superclass constructor before the subclass can initialise its own variable? And what are some of the rules governing superclass and subclass initialisation?

public class MountainBike extends Bicycle {


    public int seatHeight;
    public MountainBike(int startHeight, int startCadence,
                        int startSpeed, int startGear) {
        super(startCadence, startSpeed, startGear);//when change order with seatHeight = startHeight, IDE display error
        seatHeight = startHeight;
    }
    public void setHeight(int newValue) {
        seatHeight = newValue;
    }   

}

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Can subclasses access superclass methods?

Subclass methods can call superclass methods if both methods have the same name. From the subclass, reference the method name and superclass name with the @ symbol.

Which superclass members are accessible by all subclasses of that superclass?

A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.

Can subclasses access superclass variables?

Subclasses inherit public methods from the superclass that they extend, but they cannot access the private instance variables of the superclass directly and must use the public accessor and mutator methods.

Can a superclass call a subclass?

Yes its possible to call sub class methods using super class by type casting to sub class object . By type casting super class object to sub class object we can access all corresponding sub class and all super class methods on that reference.