Anthony K. Tamura

Instructor Kurt Diesch

ITP620 – 1104C – 01

Discussion Board Unit 4

SUPER KEYWORD

Super is a keyword in Java that plays an important role in case of inheritance. Super keyword is used to access the members of the superclass. Java uses this keyword to resolve two purposes. The first purpose is accessing the hidden data variables of the parent class hidden by the child class. An example would be class A that includes two instance variables as int a and float b. Class B extends to class A and contain its own data members having the same name a and b. In this case variables of class B hides the variables of class A, to access to those variables we the super keyword. Accessing of superclass variables is done by using the following syntax.

Ø  super.member: Member can refer to either an instance variable or a method.

Ø  Calling super class constructor: Super keyword is also used to call super class constructor in the subclass. This is done by using the following command.

Ø  Super(param-list): Parameter list includes the parameter that the constructor requires in the super class. Super is always be the first statement to be executed inside a super class constructor. Pass the empty parameter list in order to call the default constructor.

A Java superclass is a class which gives a method or methods to a Java subclass. A Java class may be either a subclass, a superclass, both or neither. In addition, a superclass is a class that has been extended by another class. It allows the extending class to inherit its state and behaviors. Keywords are basically reserved words which have specific maning relevant to a complier in java programming language likewise that supper keyword indicates the following:

Ø  The super keyword in java programming language refers to the superclass of the class where the super keyword is currently being used.

Ø  The super keyword as a standalone statement is used to call the constructor of the superclass in the base class.

The superclass helps solve very complex problems starting with a top down approach and building a model for computer programmers.

INHERITANCE IN REAL-LIFE SITUATIONS

Classes in Java code exist in hierarchies. Classes above a given class in a hierarchy are superclasses of that class. That particular class is a subclass of every class higher up the hierarchy. A subclass inherits from its superclasses. The java.lang.object class is at the top of the class hierarchy, meaning every Java class is a subclass of, and inherits from object.

For example listing 1. Public Person Class

//….

public Class Person {

public static final String GENDER_MALE = “MALE”;

public static final String GENDER_FEMALE = “FEMALE”;

public Person () {

//Nothing to do>

}

private String name;

private int age;

private int height;

private int weight;

private String eyeColor;

private String gender;

//…

}

The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write and debug.

A subclass inherits all the members (fields, methods and nested classes) from its superclass. Constructors are not members so they are not inherited by subclasses, but the constructor of the superclass can involked from the subclass.

Reference

Tyagi, Sameer. Core Java Data Objects (2004). The Sun Microsystems Press

Schildt, Herbert. The Complete Reference Java 2. 4th Edition. Osborne Publishing.