Simple Inheritance Using Java

Simple inheritance is implemented in a Java application using a technique similar to that used in the C++ example. The Java application must define a super class and a subclass, as shown in the following example. The Student class is the super class, which is the same as a base class in C++. The GradStudent class is the subclass, similar to the derived class in the previous example.

The Student class definition is the same as the Student class definition in C++, except that the access specifiers are placed in each statement and method definition. The GradStudent class definition is very similar to the GradStudent class definition in C++, except for two variations.

First, the extends keyword is used in Java to signify inheritance. The extends keyword must be followed by the name of the super class that is being inherited, which is the Student class in this example.

The other variation is found within the Display() method. The first statement within the Display() method definition calls the Student class s Display() method using the keyword super . You use super whenever you want to reference a member of the super class. In this example, the Student class s Display() method causes values of the Student class attributes to be displayed on the screen. The second statement within the GradStudent class s Display() method definition causes attributes of the GradStudent class to be shown on the screen following the student information.

The main method of this example is identical to the main() function of the C++ version of this application, except this example uses Java syntax to declare an instance of the GradStudent class, which you ll remember from your Java course.

Here s what the following program displays on the screen:

ID: 100
First: Mike
Last: Lee
Graduation: 2008
ID: 101
First: Marta
Last: Enriquez
Graduation: 2008
Major: Computer Science
Undergrad Graduation year: 2002
Undergrad School: Columbia

 class Student { 
protected int nID, nGraduation;
protected String sFirst, sLast;
public Student() {
nID = 0;
nGraduation = 0;
sFirst = new String();
sLast = new String();
public void Display() {
System.out.println("ID: " + nID);
System.out.println("First: " + sFirst);
System.out.println("Last: " + sLast);
System.out.println("Graduation: " + nGraduation);
public void Write( int ID, String First, String Last, int Graduation ) {
nID = ID;
sFirst = First;
sLast = Last;
nGraduation = Graduation;
class GradStudent extends Student {
String sMajor, sUndergradSchool;
int nUndergradGraduation;
public GradStudent() {
sMajor = "";
public void Display() {
System.out.println("Major: " + sMajor );
System.out.println("Undergrad Graduation year: " +
System.out.println("Undergrad School: " + sUndergradSchool );
public void Write( int ID, String First, String Last, int Graduation,
String Major, String UndergradSchool, int UndergradGraduation ) {
super.Write( ID, First, Last, Graduation);
sUndergradSchool = UndergradSchool;
sMajor = Major;
nUndergradGraduation = UndergradGraduation;
public class StudentApp {
public static void main(String[] args) {
Student s = new Student();
GradStudent g = new GradStudent();
s.Write( 100, "Mike", "Lee", 2008);
g.Write(101, "Marta", "Enriquez", 2008, "Computer Science", "Columbia", 2002);
public static void Display( Student s ) {
public static void Display( GradStudent g ) {

OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: