Class Hierarchy Discussion D Constructor public class X { private - - PDF document

class hierarchy
SMART_READER_LITE
LIVE PREVIEW

Class Hierarchy Discussion D Constructor public class X { private - - PDF document

Class Hierarchy Discussion D Constructor public class X { private int capacity; public X() { capacity = 16;} public X(int i) {capacity = i;} public int getCapacity() {return capacity;} } public class Y extends X { private double


slide-1
SLIDE 1

Class Hierarchy

Discussion D

slide-2
SLIDE 2

Constructor

public class X { private int capacity; public X() { capacity = 16;} public X(int i) {capacity = i;} public int getCapacity() {return capacity;} } public class Y extends X { private double loadFactor; public Y(double d) { this.loadFactor = d;} public getLoad() {return loadFactor;} }

slide-3
SLIDE 3

Usage

public static void main(String [] args) { X x = new X(32); //1 Y y = new Y(); //2 Y y2 = new Y(0.25); //3 x = (X) y; //4 x.getCapacity(); //5 x.getLoad(); //6 y = (Y) x; //7 }

slide-4
SLIDE 4

X x = new X(32); //1

  • Allocate a new object of class X
  • Invoke X(int capacity) constructor

– Invoke super()

  • Object()

– capacity == 32 – return

slide-5
SLIDE 5

Y y = new Y(); //2

  • No Y() constructor defined

– Constructors are not inherited

  • Uses compiler generated

– Y() { super(); }

  • Invokes X()

– Invokes Object ()

  • capacity == 16
  • loadFactor == 0.0
slide-6
SLIDE 6

Y y2 = new Y(0.25); //3

  • Allocates a new object of class Y
  • Invokes Y(double loadFactor)

– Invokes X()

  • Invokes Object()
  • capacity == 16

– loadFactor == 0.25

slide-7
SLIDE 7

x = (X) y; //4

  • Is unnecessary
  • Improves readability

X Y

slide-8
SLIDE 8

y = (Y) x; //7

  • Is correct
  • will succeed check cast at run time

X Y

slide-9
SLIDE 9

x.getLoad(); //6

  • Substitutability

– y extends x – y can be substituted for x

  • Variable declared of Type X
  • cannot access methods of Class Y
slide-10
SLIDE 10

Polymorphism

public class X { private int [] data; public X(int [] data) { this.data = data;} public int [] sort() {...} public void print() {} } public class Y extends X { public Y(boolean direction) {} public int [] sort(boolean descending) {...} public void print() {} }

slide-11
SLIDE 11

Visibility

public class X { public void u (p();); private void p(); } public class Y extends X { private void p(); } Y y = new Y(); y.u();

slide-12
SLIDE 12

Hierarchy

public class X extends Y {} public class Y extends X {} public class X {} public class Y extends X { public class Z extends X { private Y;}

slide-13
SLIDE 13

Hierarchy

public class X {} public interface Y {} public class Z extends X implements Y {} public interface X {} public interface Y extends X {} public class Z implements Y {}

slide-14
SLIDE 14

Travel Hierarchy

Travel AirTravel CarTravel

slide-15
SLIDE 15

public abstract class Travel { public Travel (String source, String Destination){} public double cost() {} //cost of travel,fare public int time() {} //travel time for meals public int compensation() {} //dollar amount }

slide-16
SLIDE 16

public class AirTravel extends Travel { public AirTravel(String source, String destination, String airline) { super (source, destination); } public AirTravel(String source, String destination, String airline, String class) { super (source, destination); } public double cost();//verify cost from airline web public int time(); public int compensation () { // compute allowable parking, meals etc. } }

slide-17
SLIDE 17

public class CarTravel extends Travel { public CarTravel(String source, String destination, String route) { super (source, destination); } public double cost(){ // compute mileage // maybe from yahoo maps directions // based on a per mile cost, compute total cost } public int time(); public int compensation () { // compute allowable parking based on // num days spent at destination } }

slide-18
SLIDE 18

Why Travel Interface?

  • Clients of Travel can compute the necessary

information without knowing all the details

  • Flexibility is limited to the generality of the

Travel interface for use by the clients

  • If the interface needs modification then object
  • riented programming benefits are lost
slide-19
SLIDE 19

Merchandise

Merchandise Tax Clothing ClothingTax

slide-20
SLIDE 20

public abstract class Merchandise { Tax tax; public int getCost() {} public int getTax(int zipCode) { return tax.getTax(zipCode); } } public class Clothing extends Merchandise { public Clothing () { tax = new ClothingTax(this); } public int getCost() {} }

slide-21
SLIDE 21

public abstract class Tax { Merchandise article; public Tax(); public int getTax(int zipCode); } public class ClothingTax extends Tax { //imagine a static zipcode indexed table for looking up //taxation public int getTax(int zipCode); } public class PharmaTax extends Tax { public int getTax(int zipCode); } We may want to model zip code explicitly using a Location class.

slide-22
SLIDE 22

Interfaces

Merchandise Clothing Tax

slide-23
SLIDE 23

Extension

  • Assumed that tax rate was flat for a type
  • It may depend on cost of item

– Clothes > $250 may be taxed differently

slide-24
SLIDE 24

Detailed

Merchandise Tax Clothing ClothingTax Location 01003 MA