435 Java: Inheritance - An Overview

Java: Inheritance

Author: Ronald S. Holland
Total Application Works

To visit my site

HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#

To contact us

© 2002 - 2016 All Rights Reserved Total Application Works

  • Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)

  • <html>
    <title>Example of a link </title>
    <a href="http://sumtotalz.com/TotalAppsWorks/Java/JAV_Inheritance.html">
    Getting Started with Java: Inheritance </a>

Table of Contents

Java Resources

Read Me First

The JAV_Inheritance.zip file contains the following files:

  1. UnderGraduateStudent.java - this file contains the main method code and is the entry point when the program is loaded into memory,
  2. UnderGraduateStudent.bat - this file compiles and runs the main program (UnderGraduateStudent.java). Double click on this filename in the directory to run.
  3. JAV_Inheritance.html - is the tutorial that describes how to build the Student class programs


This tutorial is intended for that person who is a beginner with object oriented programming and Inheritance. In order for you to be able to follow this tutorial, you will need a Java compiler. I don't believe in buying something if you can get it for free. At this point, if you don't have a compiler, you can download one for free from

Free Integrated Development Environments (IDE)

Here are 18 of the best free Java IDE software programs. These let you create various useful Java applications easily. All these Java IDE software are completely free and can be downloaded to Windows PC. These free software offer various features, like: lets you build Java applications, testing for JUnit, TestNG, debugging, code inspections, code assistance, support for multiple refactorings, Maven build tools, ant, visual GUI builder and code editor for XML, Java, make backend server, do data modeling and build queries, builds classes in Java and transform tables into objects in Java, lets you build cross platform and professional mobile, web, enterprise and desktop applications etc. So, go through this list of free Java IDE software and see which ones you like the most.

  1. IntelliJ IDEA Community Edition
  2. Java Inventor
  3. NetBeans IDE
  4. NaviCoder IDE for Java
  5. Jcoder Java IDE and Compiler
  6. SkyIDE
  7. Enide Studio 2014
  8. ObjectScript
  9. Java Development Kit
  10. BlueJ
  11. DrJava
  12. Eclipse
  13. Javelin
  14. jEdit
  15. jGRASP
  16. JSource
  17. JotAzul
  18. Asterix IDE


The ability to use the object-oriented programming is an important feature of Java. The tutorial on classes introduces the concept of the class. You should read it before continuing this tutorial if you have not read it and do not know the basic details of classes. For a brief description of classes, see the section on class.

What is inheritance?

We deal with inheritance every day in the real wolrd. We inherit our genes from our parents. Inheritance is the process wherein characteristics/behaviors are inherited from ancestors/parents.

There are two ways to reuse the existing classes, namely, composition and inheritance. With composition (aka aggregation), you define a new class, which is composed of existing classes. With inheritance, you derive a new class based on an existing class, with modifications or extensions.

When you derive a class from a parent class and the derived class has access to the parent's methods, it is called inheritance. In real life, you can use your DNA to trace your ancestry. Similarly, some IDEs allow you to trace the inheritance of a class. The new class created is called a derived/child class and the old class is called a base/parent class in JAV. In JAV inheritance, the derived class will inherit most of the features of the base class. It should be noted that there are some exceptions in JAV inheritance that are as follows.

  • The constructor and destructor of a base class are not inherited
  • The assignment operator is not inherited
      This keeps one class from being assigned to different class e.g., apples objects being assigned to oranges objects.
  • In addition, the friend functions and friend classes of the base class are not inherited.
There are some points to be remembered regarding JAV inheritance. The protected and public variables or members of the base class can be accessed in the derived class. But, a derived class cannot access a private member in the base class.

Inheritance is an important feature of classes; basically, it is central to the idea of object-oriented programming. Inheritance allows you to create a hierarchy of classes, with various classes of more specific natures inheriting the general aspects of more generalized classes. In this way, it is possible to structure a program starting with abstract ideas that are then implemented by specific classes. What is a class?


A class is an abstract data type that contains both data and functions. Let's consider the class of automobile, which contains the set of all automobiles. A sampling might be:

  • Acura TL
  • BMW 5 series
  • Buick Century
  • Ford Explorer
  • Honda Pilot
  • Mazda6
  • Toyota Avalon
  • Toyota Highlander
  • Volvo S60
  • Etc.
If we were to write one of the examples above in JAV, it might look like:


class Toyota : automobile {

class Avalon : Toyota {
Figure 1: Inheritance from Automobile

This example is interpreted as derived class Avalon inherits from class Toyota and derived class Toyota inherits from class automobile. In addition, the derived class Avalon inherits from the Automobile class. We will discuss inheritance further in the next section.

Student class as a base class

For example, you might have a class student from which classes GraduateStudent, UnderGraduateStudent, HighSchoolStudent, and ElementrySchoolStudent inherit the traits/behaviors that are general to all students; at the same time, each of those classes will have attributes specific to the
  • GraduateStudent;
  • UnderGraduateStudent;
  • HighSchoolStudent; or
  • ElementrySchoolStudent.
Consider the following figure.

       Base Class ---> |    Student  | 
                              | Derived classes
    |                     |            |            
+------+--------+  +------+--------+   |
|GraduateStudent|  |UnderGraduate  |   |
+---------------+  |  Student      |   |
                   +---------------+   | 
                        |                |
                +-------+-------+ +------+--------+
                |   HighSchool  | |ElementrySchool|
                |     Student   | |   Student     |   
                +---------------+ +---------------+     
Figure 2: Inheritance

In Figure 2, if the class Student has the following behaviors;
  1. Learn()
  2. Study()
  3. Read()
  4. Research()
  5. KissUpToTeacher()
  6. SitInClass()
  7. TakeNotes()
  8. WritePapers()
  9. WriteThesis()
  10. SleepInClass()
  11. GoToClass()
  12. GoToNextClass()
  13. GoHome()
  14. AskTeacherQuestions()
  15. GetSomethingToEat()
  16. GetSomethingToDrink()
  17. GoToPractice()
  18. ...
  19. TakeTest()
The derived classes inherit these behaviors from the base class Student. An example of this would be as follows:

import java.awt.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane ;

class Student {

   int grade ;

   void KissUpToTeacher( int a ) {
	String kissingUp[] = {
          "You are the best teacher ever!",
        "I have never seen a better teacher",
	"You make learning so easy.",
	"When I look up teacher in the dictionary, "
        + " I see picture of you."
	} ;	

     System.out.println(" " + kissingUp[ a ] + "\n" ) ;   

   int GetGrade() {
      SetGrade( 95 )  ;
      return grade  ;

   void SetGrade( int a) {
      grade = 95   ;
} ;

class UnderGraduateStudent extends Student {

   int grade ;

   int TakeTest( String str ) {
      return GetGrade()  ;

   public static void main( String args[] ) {

      int grade ;

      UnderGraduateStudent ogs = new UnderGraduateStudent() ;

      ogs.KissUpToTeacher( 1 ) ;

      grade = ogs.TakeTest("MathTest") ; 

     System.out.println("My math grade was: " + grade + "\n" ) ;  
      ogs.KissUpToTeacher( 3 ) ;    

      return ;

} ;

Program output

I have never seen a better teacher

My math grade was: 95

When I look up teacher in the dictionary, I see picture of you.
Figure 3: Graduate Student Inheritance

The example in Figure 3 is a basic inheritance demonstration. When you note how GetGrade() and KissUpToTeacher( int a ) are accessed, you will see the power of inheritance. This example is not intended to be an industrial strength program. It is intended to be a beginner's example. We will explore inheritance concepts further in future tutorials on class.

Frequently asked questions

  1. What is inheritance?
  2. What is Composition in Java?
  3. Why would you favor Composition over Inheritance in Java?
  4. What is one disadvantage of Inheritance over composition in Java?
  5. What is the difference between superclass and subclass in Java?
  6. Does Java support multiple inheritance?
  7. Does a class declared final support inheritance?
  8. What is a class in the lower inheritance hierarchy is called?


abstract data type


    This is a header for the Microsoft Foundation Classes JAV library. It must be included in all MFC applications.


    An ancestor or forebear is a parent or (recursively) the parent of an ancestor (i.e., a grandparent, great-grandparent, great-great-grandparent, and so forth). Ancestor is "any person/class from whom one/class is descended. In law the person from whom an estate/class has been inherited."


    The CFrameWnd class encapsulates the functionality of a Windows single document interface (SDI) frame window. A frame window is a window that frames an application.


    The CWinApp class is the base class from which you derive a Windows application object. An application object provides member functions for initializing your application (and each instance of it) and for running the application.


    The CStatic class provides the functionality of a Windows static control. A static control is a control used to display text, to draw frames or lines separating other controls, or to display icons. A static control doesn't accept user input, but it can notify its parent of stylus taps if it's created with SS_NOTIFY style.

DNA (Deoxyribonucleic acid)

    is a molecule that carries most of the genetic instructions used in the development, functioning and reproduction of all known living organisms and many viruses.

event handling

gene -

    a unit of heredity that is transferred from a parent to offspring and is held to determine some characteristic of the offspring.

IDE (Integrated development environment)

    is a software application that provides comprehensive facilities that allows you to develop software An IDE usually consists of a source code editor, build automation tools and a debugger.


    is the ability to create a class that derives properties from one or more base classes.


    Override InitInstance to initialize each new instance of your application running under Windows. Typically, you override InitInstance to construct your main window object and set the CWinThread::m_pMainWnd data member to point to that window.


    Holds a pointer to the application’s main window.


    Designates a simple rectangle and displays the given text centered in the rectangle. The text is formatted before it is displayed. Words that would extend past the end of a line are automatically wrapped to the beginning of the next centered line.


    is a unique class or pattern that can be used to build other software code.


    Creates a window that has a border.


    Creates a child window. Cannot be used with the WS_POPUP style.


    Creates a window that is initially visible.

HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#

© 2002 - 2016 All Rights Reserved Total Application Works