Home : Course Map : Chapter 4 : Java :
Demo 1: Class Inheritance
JavaTech
Course Map
Chapter 4

Introduction
Inheritance
  Demo 1
Overriding
  Demo 2a
  Demo 2b
this,super
MoreConstructors
  Demo 3
Abstraction
Interface 
  Demo 4
Casting References
MoreAboutArrays
Object class
Class Summary
Exercises

    Supplements
Override/Overload
Annotation-J2SE5.0
Java Security
Class Loading
Class Verifier
SecurityManager
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

In the demo belwo we use the StartApplet3.java starter, which includes a class called Test and its subclass Test1, which in turn is subclassed by Test2.

Each class includes a method. Because of inheritance, each of the subclass also has the methods of its superclass(es). Each subclass also inherits the data fields in its superclass(es)

Here we create an instance of each class and invoke the methods available for that class.

InheritanceApplet.java
(Output goes to browser's Java console.)
ublic class InheritanceApp extends java.applet.Applet
{
  public static void main (String args[]) {

    // Create an instance of Test
    Test test = new Test ();
    // and invoke its only method.
    test.aMethod ();
    System.out.println ();


    // Create an instance of Test1, which extends Test
    Test1 test1 = new Test1 ();
    // Test1 inherits the aMethod from Test and adds
    // its own bMethod().
    test1.aMethod ();
    test1.bMethod ();
    System.out.println ();

    // Create an instance of Test2, which extends Test1.
    Test2 test2 = new Test2 ();
    // Test2 inherits the aMethod from Test, the bMethod from Test1,
    //  and adds its own cMethod().
    test2.aMethod ();
    test2.bMethod ();
    test2.cMethod ();

  }

}

/** Test is our base class. **/
class Test
{
  int i = 5;

  Test () {
    System.out.println ("In Test  object");
    System.out.println ("   i = " + i);
  }

  void aMethod () {
    System.out.println("   Called aMethod()");
  }
}

/** Test 1 is a subclass of Test. **/
class Test1 extends Test
{
  int j = 10;

  Test1 () {
    System.out.println ("In Test1 object");
    System.out.println ("   i = " + i);
    System.out.println ("   j = " + j);
  }

  void bMethod () {
    System.out.println("   Called bMethod()");
  }

}

/** And Test2 is a subclass of Test1. **/
class Test2 extends Test1
{
  int k = 15;;

  Test2 () {
    System.out.println ("In Test2 object");
    System.out.println ("   i = " + i);
    System.out.println ("   j = " + j);
    System.out.println ("   k = " + k);
  }

  void cMethod () {
    System.out.println("   Called cMethod()");
  }
}

The following shows the output from the above class:

In Test object
   i = 5
   Called aMethod()


In Test object
   i = 5
In Test1 object
   i = 5
   j = 10
   Called aMethod()
   Called bMethod()


In Test object
   i = 5
In Test1 object
   i = 5
   j = 10
In Test2 object
   i = 5
   j = 10
   k = 15
   Called aMethod()
   Called bMethod()
   Called cMethod()

 

We see that when we instantiate a subclass, the constructor(s) of its superclass(es) are invoked first, starting from the lowest base class.

Latest update: Oct. 21, 2004

            Tech
MoreComplexClass
ImprovedHistogram
JavaRandomNums
Vectors & Matrices
Exercises

           Physics
Runge-Kutta 2nd
  Demo 1
Runge-Kutta 4th
  Demo 2
BoundaryVal.Prob
Shooting Method
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.