Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance INHERITANCE: extending classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class Vehicle{

Similar presentations


Presentation on theme: "Inheritance INHERITANCE: extending classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class Vehicle{"— Presentation transcript:

1 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; }

2 //class Vehicle continued public void setMake(String nmake) { make = nmake; } public void setModel(String nmodel) { model = nmodel; } public void setYear(int nyear) { year = nyear; } public void setMileage(double mpg) { mileage = mpg; }

3 //class Vehicle continued public String getMake() { return make; } public String getModel() { return model; } public int getYear() { return year; } public String getMileage() { return mileage; } public String toString() { return make + “ “ + model+ “, Year: “ + year; }

4 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)

5 Vehicle class = superclass Car class = subclass In general, class SubclassName extends SuperclassName { instance fields methods }

6 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)

7 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 ………….. }

8 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 !!

9 //get and set public void setConvert(boolean conv){ convertible = conv; } public boolean getConvert() { return convertible; } //calculate distance that can currently be traveled public double distance (double gallons) { return gallons * getMileage(); //use superclass method to access // private data } //OVERRIDE the toString method public String toString() { return year + “ “ + model + “ trunk cap: “ + trunksize; }

10 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

11 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

12 Invoking a Superclass Method Subclass can directly call any public method (as we called getMileage 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

13 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

14 //get and set public void setTowPackage(boolean tow){ towpackage = tow; } public boolean getTowPackage() { return towpackage; } //calculate how many of an item that can currently be carried public double quantity (int sizeofone) { return bedsize / sizeofone; }

15 Class Usage //can create and assign to same type reference Vehicle v1 = new Vehicle(“ford”,“mustang”,1966, 28.5); Car c1 = new Car(“vw”,”rabbit”, 1978, 35.2); //a subclass is the superclass type, but not vice versa Vehicle v2 = new Car(“cadillac”,”seville”, 1988, 16.0); Vehicle v3 = new Truck(“MAC”,”pickup”, 1968, 16.0); Car c2 = new Vehicle(“gmc”,”yukon”,122, 13.5); //error //public superclass methods can be called by subclass object v1.setMake(“Mercury”); c1.setMake(“Toyota”); v2.setMake(“Nissan”);

16 Class Usage cont. //public subclass methods cannot ALWAYS be called by super object vN.setConv(true); // only makes sense if vN is a Car //object, so compiler will object Car temp = (Car) vN; temp.setConv(true); //must cast to get by compiler if vN is NOT a Car the attempt to cast will throw a ClassCastException…….. use instanceof operator to check !!!!!!!

17 Class Usage cont. //subclass object ARE of superclass type (but not vice versa) Vehicle[] list = new Vehicle[10]; list[0] = c1; list[1] = t1; list[2] = v1; for (int i=0; i<2; i++) System.out.println(list[i]); WHICH toSTRING will be used??? (polymorphism again)

18 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.

19 Inheritance Hierarchies Hierarchies of classes, subclasses, and sub- subclasses are common Example: Swing hierarchy Superclass JComponent has methods getWidth, getHeight AbstractButton class has methods to set/get button text and icon

20 A Part of the Hierarchy of Swing UI Components


Download ppt "Inheritance INHERITANCE: extending classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class Vehicle{"

Similar presentations


Ads by Google