Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 6: Composition and Inheritance

Similar presentations


Presentation on theme: "Lecture 6: Composition and Inheritance"— Presentation transcript:

1 Lecture 6: Composition and Inheritance
CS202 Fall 2013

2 Inheritance Classes often have natural hierarchies, which can be defined in terms of data or in terms of functionality The simplest form of hierarchy is general-to-specific

3 Inheritance All vehicles have some variables in common weight source of locomotion manufacturer Motor vehicles are a subset of vehicles, and they have additional data fields Engine displacement Fuel type Trucks are a subset of motor vehicles, and they have yet more data fields Hauling capacity Etc

4 Inheritance We can model this kind of general-to-specific hierarchy using class inheritance “Super” means above, as in supervisor. “Sub” means under or below, as in submarine. A subclass is a class which is a more specific form of some other class, which we call its superclass Superclasses are more abstract than their subclasses The terms parent and child are sometimes used in place of superclass and subclass. To define a class as a subclass of a superclass, use the extends keyword. See the examples below

5 Inheritance Hierarchy
A class can inherit from a hierarchy of superclasses, in the same way you have parents, grandparents, great-grandparents, etc. All Java classes are subclasses of Object. Object is, for example, where the original forms of toString() is defined. However, most other classes do not inherit directly from Object. Here is the class hierarchy for OutOfMemoryError java.lang.Object java.lang.Throwable java.lang.Error java.lang.VirtualMachineError java.lang.OutOfMemoryError

6 Inheritance Subclasses inherit the methods and variables of their superclasses. Subclasses can add variables, constants, and methods which are not present in their superclasses.

7 Overriding Subclasses can also replace methods inherited from superclasses with their own methods. This is called overriding. toString()! Use annotation Superclass methods must be public or protected; can’t override private methods Subclass constructors call superclass constructors. If superclass has a no-argument constructor, it is called by default by a no-argument subclass constructor See example

8 Single Inheritance C++ allows a class to have more than one parent class. This is called “multiple inheritance”. This can lead to namepsace conflicts, in which multiple methods or variables with identical names would be in scope at the same time without complex precedence rules. In Java, a class is allowed to inherit from only one parent class. This is called “single inheritance”

9 Inheritance package vehicles; public class Vehicle { protected double weightInKg; protected double speedInKmPerHr = 0; // a new vehicle should stop after it rolls off the assembly line protected Direction direction = new Direction(); // avoid null pointer exceptions by giving new vehicle the default direction 0, 0, 0 public Vehicle() { } public Vehicle(double weightInKgIn) { weightInKg = weightInKgIn; public void steer(double bearing, double z) { direction.setDirection(bearing, z); public void accelerate(double speedIncrement) { speedInKmPerHr += speedIncrement; public String toString() { return "vehicle weighs " + weightInKg + " kg: is going " + speedInKmPerHr + ": " + direction.toString();

10 Inheritance package vehicles; public class Direction { private double bearing, z; public Direction(){} public Direction(double bearingIn, double zIn){ setDirection(bearing, z); } public void setDirection(double bearingIn, double zIn){ bearing = bearingIn; z = zIn; public double getBearing() { return bearing; public double getZ() { return z; public String toString(){ return "bearing: " + bearing + ": z: " + z;

11 Inheritance package vehicles; public class MotorVehicle extends Vehicle { protected double engineDisplacementInCc; protected String fuelType; protected String manufacturer; public MotorVehicle(){} public MotorVehicle(double weightInKgIn, String manufacturerIn, double displacementIn, String fuelTypeIn) { super(weightInKgIn); manufacturer = manufacturerIn; engineDisplacementInCc = displacementIn; fuelType = fuelTypeIn; } public double getEngineDisplacementInCc() { return engineDisplacementInCc; public String getFuelType() { return fuelType; public String getManufacturer() { return manufacturer; // this method is unique to MotorVehicles, not common to all vehicles public void register() { System.out.println("Registered " + manufacturer + " vehicle with DMV"); public String toString() { return "manufacturer: " + manufacturer + "engine displacement: " + engineDisplacementInCc + ": fuelType: " + fuelType + ": " + super.toString();

12 Inheritance package vehicles; public class Car extends MotorVehicle { private String licensePlateNumber; public Car(double weightInKgIn, String manufacturerIn, double displacementIn, String fuelTypeIn, String licensePlateNumberIn){ super(weightInKgIn, manufacturerIn, displacementIn, fuelTypeIn); licensePlateNumber = licensePlateNumberIn; } public String getLicensePlateNumber() { return licensePlateNumber; public void setLicensePlateNumber(String licensePlateNumber) { this.licensePlateNumber = licensePlateNumber; public String toString() { return manufacturer + " car with plate " + licensePlateNumber + " and engine displacement " + engineDisplacementInCc + " cc " + fuelType + " engine weighs " + weightInKg + " kg and is going " + speedInKmPerHr +" KPH " + direction.toString();

13 Inheritance package vehicles; public class Motorcycle extends MotorVehicle { private double volumeInDecibels; public Motorcycle(double weightInKgIn, String manufacturerIn, double displacementIn, double volumeInDecibelsIn) { super(); // note the difference between these assignments and the way the same task is done in the Car constructor. // This way is simpler, but might miss or require duplication of initialization logic in the superclass constructors. manufacturer = manufacturerIn; weightInKg = weightInKgIn; engineDisplacementInCc = displacementIn; fuelType = "gasoline"; speedInKmPerHr = 0; volumeInDecibels = volumeInDecibelsIn; } public double getVolumeInDecibels() { return volumeInDecibels; public void setVolumeInDecibels(double volumeInDecibels) { this.volumeInDecibels = volumeInDecibels; public String toString() { return manufacturer + " motorcycle with a " + engineDisplacementInCc + " cc " + fuelType + " engine weighs " + weightInKg + " kg and is going " + speedInKmPerHr + " KPH " + direction.toString() + " making noise at " + volumeInDecibels + " db";

14 Inheritance package vehicles; public class Driver { public static void main(String[] args) { Vehicle shredder = new Car(1000, "Mazda", 1900, "gasoline", "ABC-123"); System.out.println(shredder); shredder.accelerate(20); shredder.steer(100, 0); System.out.println(); Vehicle hindenburg = new Motorcycle(240, "BMW", 594, 80); hindenburg.steer(70, 0); hindenburg.accelerate(90); System.out.println(hindenburg); Vehicle porky = new Motorcycle(400, "Harley-Davidson", 1200, 150); porky.accelerate(150); porky.steer(180, 45); System.out.println(porky); }


Download ppt "Lecture 6: Composition and Inheritance"

Similar presentations


Ads by Google