inheritance

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

Upload: jane-pollard

Post on 30-Dec-2015

17 views

Category:

Documents


0 download

DESCRIPTION

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; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Inheritance

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

Page 2: Inheritance

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

Page 3: Inheritance

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

Page 4: Inheritance

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)

Page 5: Inheritance

• Vehicle class = superclass• Car class = subclass

• In general,

class SubclassName extends SuperclassName{       instance fields

methods }

Page 6: Inheritance

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)

Page 7: Inheritance

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

…………..

}

Page 8: Inheritance

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

Page 9: Inheritance

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

Page 10: Inheritance

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

Page 11: Inheritance

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

Page 12: Inheritance

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

Page 13: Inheritance

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

Page 14: Inheritance

//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;

}

}

Page 15: Inheritance

Class Usage//can create and assign to same type referenceVehicle 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 versaVehicle 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”);

Page 16: Inheritance

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

Page 17: Inheritance

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)

Page 18: Inheritance

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.

Page 19: Inheritance

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

Page 20: Inheritance

A Part of the Hierarchy of Swing UI Components