Chapter 8 – Inheritance 

Section A

A.      Concept

The concept of a class inheritance is useful because it makes class code reusable.

When you use inheritance to create the EmployeeWith Territory class  from the Employee class, you:

A class that is used as a basis for inheritance, such as Employee , is called a base class.   When you create a class that inherits from a base class, it is a derived class.

A base class can be called a superclass, and a derived class can be named as a subclass.  You can also call a base class a parent class, and the derived class a child class

B.      Extending classes

You use the keyword “extends” to achieve inheritance within the Java programming language.

Ex:  public class EmployeeWithTerritory extends Employee

Ex:  You used the phrase extends Applet throughout ch6 and ch7.  Every applet that you write is a child of the Applet class.

You instantiate an object using the statement:

 EmployeeWithTerritory northernRep = new EmployeeWithTerritory();

·         Inheritance is a one-way proposition; a child inherits from a parent, and not the other way around.

 Example :

  To create the Event class:

public class Event

{

  private int eventGuests;

  public void printEventGuests()

  {

    System.out.println("Event Guests: " + eventGuests);

}

  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();

}

}

·         Create an useEvent class to use the Event class

public class useEvent

{

   public static void main(String[] args) throws Exception

  {

    Event anEvent = new Event( );

    anEvent.setEventGuests();

    anEvent.printEventGuests();

}

}

·         Create a dinner event that extend the Event class

public class dinnerEvent extends Event

{

  char dinnerChoice;

public void printDinnerChoice()

{

  if (dinnerChoice =='b')

    System.out.println("Dinner choice is beef");

  else

    System.out.println("Dinner choice is chicken");

}

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( );

}

}

·         Create a useDinnerEvent to use both the Event and the DinnerEvent classes

public class useDinnerEvent

{

   public static void main(String[] args) throws Exception

  {

    Event anEvent = new Event( );

    System.out.println(" A plain event ");

    anEvent.setEventGuests();

    anEvent.printEventGuests();

    dinnerEvent aDinnerEvent = new dinnerEvent( );

    System.out.println(" An event with dinner" );

    aDinnerEvent.setEventGuests();

    aDinnerEvent.setDinnerChoice();

    aDinnerEvent.printEventGuests();

    aDinnerEvent.printDinnerChoice();

}

}

C.      Overriding Superclass Methods

      You can override the methods in the superclass.  You already have overridden methods in your applets.  When you write your own init() or start() method within an applet, you are overriding the automatically supplied superclass version you get when you use the phrase extends Applet.

If a superclass and its subclass have methods with the same name but different argument lists, you are overloading methods, and not overriding them.

·         Create a EventWithHeader class

public class EventWithHeader

{

  private int eventGuests;

  public void printEventGuests()

  {

    System.out.println("Event Guests: " + eventGuests);

}

public void printHeader()

{

   System.out.println("Simple event: ");

}

  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();

}

}

·         Create a DinnerEventWithHeader

public class dinnerEventWithHeader extends EventWithHeader

{

  char dinnerChoice;

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( );

}

}

·         Create a UseEventsWithHeads class

public class useEventWithHeaders

{

  public static void main(String[] args) throws Exception

{

EventWithHeader anEvent = new EventWithHeader();

dinnerEventWithHeader aDinnerEvent = new dinnerEventWithHeader();

 

anEvent.printHeader();

anEvent.setEventGuests();

anEvent.printEventGuests();

 

aDinnerEvent.printHeader();

aDinnerEvent.setEventGuests();

aDinnerEvent.setDinnerChoice();

aDinnerEvent.printEventGuests();

aDinnerEvent.printDinnerChoice();

}

}

Section B – Using Superclasses and SubClasses

  1. Working with superclasses that have constructors

Example :

a.   create a base class

public class ABaseClass

{

   public ABaseClass()

  {

    System.out.println(" In base class constructor");

  }

}

  1. create a subclass

public class ASubClass extends ABaseClass

{

  public ASubClass()

  {

    System.out.println("In subclass constructor");

  }

}

  1. create a class to call the subclass

public class DemoConstructors

{

  public static void main(String[] args)

  {

   ASubClass child = new ASubClass();

  }

}

The output appears :  In base class constructor

                                     In subclass constructor

Even thought in the DemoConstructors, only one subclass object is created, two separate message print – one from the superclass constructor and one from the subclass constructor.

* When constructors initialize variables, you usually want the base class constructor to take care of initializing the data fields that originate in the base class.  The subclass constructor only needs to initialize the data fields that are specific to the subclass.

Example –

  1. create the EventWithConstructor class

public class EventWithConstructor

{

  private int eventGuests;

public EventWithConstructor()

{

   eventGuests = 0;

}

  public void printEventGuests()

  {

    System.out.println("Event Guests: " + eventGuests);

}

public void printHeader()

{

   System.out.println("Simple event: ");

}

  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();

}

}

  1. Create the DinnerEventWithConstructor class

public class dinnerEventWithConstructor extends EventWithConstructor

{

  char dinnerChoice;

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( );

}

}

  1. Create the UseEventWithConstructor class

public class UseEventWithConstructors

{

  public static void main(String[] args)

   {

      EventWithConstructor anEvent =

      new EventWithConstructor();

      dinnerEventWithConstructor aDinnerEvent =

      new dinnerEventWithConstructor();

      anEvent.printHeader();

      anEvent.printEventGuests();

      aDinnerEvent.printHeader();

      aDinnerEvent.printEventGuests();

  }

}

  1. Using superclass constructors that require arguments

When a superclass constructor requires arguments, you are required to include a constructor for each subclass you create.  Your subclass constructor can contain any number of statements, but the first statement within the constructor must call the superclass constructor.

        The format of the statement that calls a superclass constructor is super(list of arguments)

  1. Accessing superclass method – use the keyword super to access the parent class method

Example

    1. AparentClass

public class AParentClass

{

  private int aVal;

  public void printClassName()

{

   System.out.println("AParentClass");

}

}

    1. AchildClass

public class AChildClass extends AParentClass

{

  public void printClassName()

  {

    System.out.println(" I am AChildClass");

    System.out.println("My parent is " );

    super.printClassName();

  }

}

    1. DemoSuper

public class DemoSuper

{

   public static void main(String[] args)

  {

     AChildClass child = new AChildClass();

     child.printClassName();

 }

}