IMPLEMENTING INHERITANCE IN JAVA OBJECTS
Rodelio P. Barcenas
NOTE: A copy of this program and document can be downloaded at
http://www.pinoytechzone.com/extends.zip
JAVA objects has the capability to inherit to qualities of an existing
object.
For an example, let us use an analogy to parent and child
relationship;
When the child was born, the child inherits the qualities of her
parent such as his eyes, hair, actions, etc. When the child was born
this qualities are already existing in the child.
Normally, when the child grows, the given methods are then extended by
adding new methods other than the ones the child inherited.
IN JAVA:
The parent object can be named (example only) parent.java
inheritance can be achieved by the child.java;
public class child extends parent{
}
The class child inherits the parent object;
TYPICAL USES OF INHERITANCE:
- hierarchical structure of objects
- extending a given java object without changing the
existing object.
JAVA EXAMPLE PROGRAM
-----------------------------------------------------------------------
THE PARENT CLASS
-----------------------------------------------------------------------
/* vehicle.java
written by: Rodelio P. Barcenas
July 10,2002
*/
public class vehicle{
// instance variables for vehicle.java object
private boolean hasWheel,hasEngine;
private boolean hasSail,hasWings;
// vehicle constructor
vehicle(boolean wheel,boolean engine, boolean sail, boolean wings){
hasWheel = wheel;
hasEngine = engine;
hasSail = sail;
hasWings = wings;
}
// action method available only to vehicle object
public void checkConfig(){
System.out.println("Has Wheel : "+hasWheel);
System.out.println("Has Engine: "+hasEngine);
System.out.println("Has Sail : "+hasSail);
System.out.println("Has Wings : "+hasWings);
System.out.println();
}
}
-----------------------------------------------------------------------
THE CHILD OBJECT
-----------------------------------------------------------------------
/* car.java
written by: Rodelio P. Barcenas
July 10, 2002
*/
public class car extends vehicle{
// instance variables of car.java object
private String engineType,chassis;
private int wheels;
// car object constructor
car(String e, String c, int w){
super(true,true,false,false);
// super - a method which invokes the constructor
// of the extends (vehicle.java)
// - should be executed first inside the
// constructor.
engineType = e;
chassis = c;
wheels = w;
}
// action method available only to car.java
public void spec(){
System.out.println("Engine Type : "+ engineType);
System.out.println("Chassis : "+ chassis);
System.out.println("No of Wheels: "+ wheels);
System.out.println();
}
}
-----------------------------------------------------------------------
A TEST PROGRAM
-----------------------------------------------------------------------
/*
This is a program which shows how to implement
inheritance in java objects.
Written by: Rodelio P. Barcenas
July 10, 2002
vehicle.java - topmost object which describes the
wholeness of the object
car.java - is an object which inherits the
instances and methods of vehicle.java
factory.java - test program for testing the inheritance
*/
public class factory{
public static void main(String[] arg){
car honda;
honda = new car("VTEC","light",4);
honda.checkConfig(); // inherited from vehicle.java
honda.spec(); // methods local to car.java
}
}