You are on page 1of 6

Inheritance

Lawrence M. Brown ktee@erols.com

Inheritance
1. Introduction
Inheritance is a technique of deriving new classes from existing classes. The methods and instance variables in the existing class can be reused or modified in the new class. The existing class is known as the superclass and the new class derived from the superclass is known as the subclass. The superclass-subclass relationship defines a hierarchy structure, whereas each subclass inherits the behavior and state of the superclass. The existing class is called the superclass, base class or parent class. The new class is called the subclass, derived class or child class.

2. Extending a Class
A new class can extend any non-final existing class using the keyword extends in the class declaration. class new-class-name extends existing-class { } In Java, a new class can be derived from only one existing class; Java does not support multiple inheritance. In general, subclasses will have more functionality than their superclasses. A subclass cannot access private members (instance variables and methods) of the superclass. A subclass can access public and protected members of a superclass.
1
December 31, 1998

1998

Inheritance

Lawrence M. Brown ktee@erols.com

A subclass can access default members of a superclass, if in the same package. Example 1, Inheritance

import java.util.Date; class Employee { private String name; private Date hireDate; protected static int numEmployee = 0; Private variables are not inherited by subclass. Protected variable is inherited by subclass.

public Employee( String name, Date hireDate ) { this.name = name; this.hireDate = hireDate ; numEmployee++; } public String getName() { return name; } public String getHireDate() { return dateToString(); } protected int getNumEmploy() { return numEmployee; } private String dateToString() { return hireDate.getMonth() + "/" + hireDate.getDate() + "/" + hireDate.getYear(); } } class Manager extends Employee { private String title; public Manager( String name, String title, Date hireDate ) { super( name, hireDate ); this.title = title; call superclass constructor } public String getTitle(){ return title; } public void print() { System.out.println( "Manager: " + getName() + " " + getHireDate() ); } getName() and getHireDate() are inherited from public class Inherit1 class Employee and have direct access. { public static void main( String[] args ) { Employee staff = new Employee( "Mickey Mouse", new Date(1948,3,1) ); Manager boss = new Manager( "Donald Duck", "Marketing", new Date(1946,5,9) ); System.out.println( "Employee: " + staff.getName() + " " + staff.getHireDate() ); System.out.println( "Employee: " + boss.getName() + " " + boss.getHireDate() ); System.out.println( "Title " + boss.getName()+ ": " + boss.getTitle() ); System.out.println( "Number of Employees: " + boss.getNumEmploy() ); } } } Protected method is inherited by subclass.

Manager is also an Employee. Employee public methods can be accessed through a Manager object.

Employee protected method getNumEmploy() can be accessed through a Manager object.

1998

December 31, 1998

Inheritance

Lawrence M. Brown ktee@erols.com

Output
Employee: Mickey Mouse 3/1/1948 Employee: Donald Duck 5/9/1946 Title Donald Duck: Marketing Number of Employees: 2

3. Constructors
A constructor in a subclass can directly invoke a constructor in a superclass through the use of super( parameter-list ); If super() is used in a subclass constructor, then super() must be the first statement in the constructor.

import java.util.Date; class Employee { private String name; private Date hireDate; protected static int numEmployee = 0; public Employee() { this( "No Name", new Date() ); }; public Employee( String name, Date hireDate ) { this.name = name; this.hireDate = hireDate; numEmployee++; } . . . } class Manager extends Employee { private String title; public Manager() { title = "No Title"; } Instance variables are set to their default values before constructor is invoked. calls overloaded constructor Employee( String name, Date hireDate).

Default superclass constructor is automatically executed first.

public Manager(String name, Date hireDate ) { this( name, "No Title", hireDate ) ; }

Call overloaded Manager constructor Manager( String name, String title, Date hireDate ).

public Manager( String name, String title, Date hireDate ) { super(name, hireDate); Call superclass constructor Employee( String name, Date hireDate ). this.title = title; } . . . }

1998

December 31, 1998

Inheritance

Lawrence M. Brown ktee@erols.com

When an object is created, all the instance variables are set to their default initial values, and then the constructor is invoked. Each constructor has three phases: 1. Invoke the constructor of the superclass 2. Initialize all instance variables based on their initialization statements 3. Execute the body of the constructor.

4.0 Method Overriding


Any non-static method in the superclass can be redefined in the subclass. When a class defines a method using the same name, return type, and arguments as a method in the superclass the class overrides the method in the superclass.

Only non-static methods can be overridden. The signature of the subclass method must exactly match the signature of the overridden method from the superclass; similarly, the return types must match. If a method is overridden in the subclass, then the superclass method can still be accessed through the super keyword. The visibility of an overridden method can be changed to provide greater access. A method declared protected in the superclass can be redeclared protected or public in the subclass (but not private). Instance variables cannot be overridden. If a instance variable in the subclass is declared with the same name as the superclass, then the variable in the superclass is considered hidden or shadowed. A reference to the superclass type or the keyword super can be used to access the hidden variable. If a child object (subclass) is cast to a parent object (superclass), then the following will occur: 1. any reference to a hidden variable will access the data field in the superclass and not the data field of the subclass. 2. any reference to an overridden method will still access the subclass method.

In summary, casting up the hierarchy will expose hidden variables, but preserve overridden methods.

1998

December 31, 1998

Inheritance

Lawrence M. Brown ktee@erols.com

Example Method Overriding


import java.util.Date; class Employee { private String name; private Date hireDate; protected static int numEmployee = 0; public Employee( String name, Date hireDate ) { this.name = name; this.hireDate = hireDate; numEmployee++; } public String getName() { return name; } public String getHireDate() { return dateToString(); } private String dateToString() { return hireDate.getMonth() + "/" + hireDate.getDate() + "/" + hireDate.getYear(); } public void print() { System.out.println( "Name: " + name + "\nHire Date: " + dateToString()); } } class Manager extends Employee { private String title; public Manager( String name, String title, Date hireDate ) { super(name, hireDate); this.title = title; } public String getTitle(){ return title; }

print method overrides print method inherited from Employee class

public void print() { System.out.println( "Manager: " + getName() + "\nTitle: " + title + "\nHire Date: " + getHireDate() ); } } public class Inherit3 { public static void main( String[] args ) { Employee staff = new Employee( "Mickey Mouse", new Date(1948,3,1) ); Manager boss = new Manager( "Donald Duck", "Marketing", new Date(1946,5,9) ); staff.print(); boss.print(); } } staff is an Employee object, thus Employee.print() is invoked. boss is a Manager object (as well as an Employee object) however, overridden print() method in the Manager class is invoked.

1998

December 31, 1998

Inheritance

Lawrence M. Brown ktee@erols.com

Output
Name: Mickey Mouse Hire Date: 3/1/1948 Manager: Donald Duck Title: Marketing Hire Date: 5/9/1946

Example Variable Hiding and Casting


class Parent { double value = 10; double getValue(){ return value; } } class Child extends Parent { double value = 5; // hides variable value in Parent double getValue() // overrides method getValue in Parent { return -value; } } public class overriding { public static void main( String[] args ) { Child c = new Child(); Parent p = new Parent(); System.out.println( "c.value = " + c.value ); System.out.println( "c.getValue() yields: " + c.getValue() ); System.out.println( "p.value = " + p.value ); System.out.println( "p.getValue() yields: " + p.getValue() ); p = (Parent) c; System.out.println( "p.value = " + p.value ); System.out.println( "p.getValue() yields: " + p.getValue() ); } } Cast c to an instance of class Parent value now refers to Parent.value getValue still refers to Child.getValue()

Output

c.value = 5.0 c.getValue() yields: -5.0 p.value = 10.0 p.getValue() yields: 10.0

p.value = 10.0 p.getValue() yields: -5.0

1998

December 31, 1998

You might also like