Super classes and subclasses – Java Tutorial

Super classes and subclasses – Java Tutorial

When you are using superclasses and subclasses, it is important to remember that two constructors execute. The base, or parent, class constructor MUST execute prior to the extended, or child, class constructor. For example:
A Base Class
public class ABaseClass
{
// Base constructor that executes first
public ABaseClass()
{
System.out.println("This is from the base class");
}
}

A Subclass
public class ASubClass extends ABaseClass
{
// Subclass constructor that executes second
public ASubClass()
{
System.out.println("This is from the subclass");
}
}

A Demonstration Program
public class DemoConstructors
{
// The main method that executes both programs
public static void main(String[] args)
{
ASubClass child = new ASubClass();
}
}

When you run this application, you will get both sentences printing in order from base class to subclass. If you don’t provide a constructor, Java will provide one for you. You can use as many constructors as you want, however, if you create one, you can never use the automatic version.

When a superclass constructor requires arguments, you need a constructor for each subclass. You call a superclass constructor by typing super(all, your, arguments);. With the exception of comments, super() must be the first line in the subclass constructor. Below are three programs that show how all this comes together. The first is the “parent” or “superclass” program.

Superclasses and subclasses

public class EventWithConstructorArg
{
  private int eventGuests;
  // constructor that requires arguments
  public EventWithConstructorArg(int guests)
  {
    eventGuests = guests;
  }
  // displays the number of guests
  public void printEventGuests()
  {
    System.out.println("Event guests: " + eventGuests);
  }
  public void printHeader()
  {
    System.out.println("Simple event: ");
  }
  // setEventGuests() sets up the prompt for the user and stores the input
  public void setEventGuests() throws Exception
  {
    char inChar;
    String guestsString = new String("");
    System.out.print("Enter the number of guests at your event ");
    inChar = (char)System.in.read();
    while(inChar >= '0' && inChar <= '9')
    {
      guestsString = guestsString + inChar;
      inChar = (char)System.in.read();
    }
    eventGuests = Integer.parseInt(guestsString);
    System.in.read();
  }
}

This next one is the “child” or “subclass” program that passes arguments to the parent program.

public class DinnerEventWithConstructorArg extends EventWithConstructorArg
{
  char dinnerChoice;
  // constructor that calls parent constructor
  public DinnerEventWithConstructorArg(int guests)
  {
    // with the exception of these comments, super() MUST be
    // the first statement in the subclass (or child) constructor
    super(guests);
  }
  public void printDinnerChoice()
  {
    if(dinnerChoice == 'b')
      System.out.println("Dinner choice is beef.");
    else
      System.out.println("Dinner choice is chicken.");
  }
  public void printHeader()
  {
    System.out.println("Dinner event: ");
  }
  public void setDinnerChoice() throws Exception
  {
    System.out.println("Enter dinner choice");
    System.out.print("b for beef, c for chicken: ");
    dinnerChoice = (char)System.in.read();
    System.in.read(); System.in.read();
  }
}

This last programs shows how the child program sends the arguments to the parent class. Although there are more things programmed than are being asked of this program, it is only the statments that are relavent to this lesson that are being called in this program.

public class UseEventsWithConstructorsArg
{
  public static void main(String[] args) throws Exception
  {
    EventWithConstructorArg anEvent = new EventWithConstructorArg(45);
    DinnerEventWithConstructorArg aDinnerEvent = new DinnerEventWithConstructorArg(65);
    anEvent.printHeader();
    anEvent.printEventGuests();
    aDinnerEvent.printHeader();
    aDinnerEvent.printEventGuests();
  }
}

Once compiled and error-free, your programs should have this output:

Simple event:
Event guests: 45
Dinner event:
Event guests: 65

You can access a parent class’ methods by using the keyword super. To do this, you would need to type is as this:

super.anySuperclassMethod();

A child class cannot inherit “private” parent class members. They can, however, inherit protected data. This is called information hiding. Private data and methods cannot be extended from a superclass. If a superclass has data or methods that are protected, then a child class can use the inherited protected data or methods. In other words, if you could use private data outside of a superclass, the idea of information hiding would be lost.

Question: Wow!! Inheritance seems to make things easier to program. Is there anything more about inheritance that I should know?

Answer: Yes, there is. Next we’ll learn about abstract classes and dynamic method binding.