Inheritance INHERITANCE extending classes by adding or redefining
Inheritance • INHERITANCE: extending classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class Vehicle{ private String make; private String model; private int year; private double mileage; public Vehicle(String mk, String mdl, int yr, double mpg){ make = mk; model = mdl; year = yr; mileage = mpg; }
//class Vehicle continued public void set. Make(String nmake) { make = nmake; } public void set. Model(String nmodel) { model = nmodel; } public void set. Year(int nyear) { year = nyear; } public void set. Mileage(double mpg) { mileage = mpg; }
//class Vehicle continued public String get. Make() { return make; } public String get. Model() { return model; } public int get. Year() { return year; } public String get. Mileage() { return mileage; } public String to. String() { return make + “ “ + model+ “, Year: “ + year; } }
And now we wanted a class Car. A car certainly needs all the data and methods we saw in the existing Vehicle class … plus more public class Car extends Vehicle{ } before we even write one declaration for class Car … All public (and protected) data and methods of Vehicle are automatically inherited !! (object of type Car will have all storage and access behaviors seen in Vehicle)
• Vehicle class = superclass • Car class = subclass • In general, class Subclass. Name extends Superclass. Name { instance fields methods }
Inheritance and Instance Fields • Inherited data: All public and protected instance variables from the superclass are automatically inherited • You can add supply additional instance data in an subclass that doesn’t exist in superclass • Do not reuse instance data name in subclass (will hide super class data)
The class Car has inherited all instance data from the class Vehicle, but will need instance data of it’s own: public class Car extends Vehicle{ // data private boolean convertible; private double trunksize; // Car methods …………. . }
The class Car has inherited all methods from the class Vehicle, but will need methods of it’s own: public class Car extends Vehicle{ // data ……. . // Car constructor public Car (String mk, String md, int yr, double mpg, boolean convert, double trusize){ super (mk, md, yr, mpg); //these locations are PRIVATE!! convertible = convert; trunksize = trusize; } ** call to superclass constructor MUST be first statement !!
//get and set public void set. Convert(boolean conv){ convertible = conv; } public boolean get. Convert() { return convertible; } //calculate distance that can currently be traveled public double distance (double gallons) { return gallons * get. Mileage(); //use superclass method to access // private data } //OVERRIDE the to. String method public String to. String() { return year + “ “ + model + “ trunk cap: “ + trunksize; }
Inherited Field Access • we need to use constructor or set methods to change contents of instance data in Vehicle • We need to use get methods to access data in data fields inherited from Vehicle ** This is because instance data in Vehicle was all declared as Private
Inheritance and Methods • Override method: Supply a different implementation of a method that exists in the superclass • Inherit method: Don't supply a new implementation of a method that exists in the superclass • Add method: Supply a new method that doesn't exist in the superclass
Invoking a Superclass Method • Subclass can directly call any public method (as we called get. Mileage above) *If subclass method has the same signature as a superclass method (say xxx) xxx() call within subclass calls subclass method super. xxx() call within subclass calls superclass method
Suppose we also need a class Truck : public class Truck extends Vehicle{ private double bedsize; private boolean towpackage; public Truck (String mk, String md, int yr, double mpg, boolean tow, double bsize){ super (mk, md, yr, mpg); //these locations are PRIVATE!! bedsize = bsize; towpackage = tow; ; } //same concepts apply to Truck class as seen in Car class
//get and set public void set. Tow. Package(boolean tow){ towpackage = tow; } public boolean get. Tow. Package() { return towpackage; } //calculate how many of an item that can currently be carried public double quantity (int sizeofone) { return bedsize / sizeofone; } }
Class Usage //can create and assign to same type reference Vehicle v 1 = new Vehicle(“ford”, “mustang”, 1966, 28. 5); Car c 1 = new Car(“vw”, ”rabbit”, 1978, 35. 2); //a subclass is the superclass type, but not vice versa Vehicle v 2 = new Car(“cadillac”, ”seville”, 1988, 16. 0); Vehicle v 3 = new Truck(“MAC”, ”pickup”, 1968, 16. 0); Car c 2 = new Vehicle(“gmc”, ”yukon”, 122, 13. 5); //error //public superclass methods can be called by subclass object v 1. set. Make(“Mercury”); c 1. set. Make(“Toyota”); v 2. set. Make(“Nissan”);
Class Usage cont. //public subclass methods cannot ALWAYS be called by super object v. N. set. Conv(true); // only makes sense if v. N is a Car //object, so compiler will object Car temp = (Car) v. N; temp. set. Conv(true); //must cast to get by compiler if v. N is NOT a Car the attempt to cast will throw a Class. Cast. Exception……. . use instanceof operator to check !!!!!!!
Class Usage cont. //subclass object ARE of superclass type (but not vice versa) Vehicle[] list = new Vehicle[10]; list[0] = c 1; list[1] = t 1; list[2] = v 1; for (int i=0; i<2; i++) System. out. println(list[i]); WHICH to. STRING will be used? ? ? (polymorphism again)
Access Control Level • public - access provides to all classes Recommended way to work with objects == by calling their public methods • private - access is restricted to inside the class Class instance variables should be kept private, ensuring their integrity and enforcing encapsulation model of object oriented programming • protected -accessible by subclasses and package This is not recommended (except for efficiency purposes). As repeated inheritance occurs, access propagates and the benefits of information hiding is lost • package access (the default, no modifier) Appropriate for package of classes which share data.
Inheritance Hierarchies • Hierarchies of classes, subclasses, and subsubclasses are common • Example: Swing hierarchy • Superclass JComponent has methods get. Width, get. Height class has methods to set/get button text and icon • Abstract. Button
A Part of the Hierarchy of Swing UI Components
- Slides: 20