






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







CS108, Stanford Handout # Fall, 2008-09 Osvaldo Jiménez
Thanks to Nick Parlante for much of this handout
Previously we looked at basic OOP encapsulation and API design. Here we start looking at inheritance.
units 10
Student (RT)
yot 2 units 10
Grad (RT)
s
Student (CT)
obj
Object (CT)
Variables with their CT types
Objects in the heap, each with its true RT type
g
Grad (CT)
((Grad)s).getYearsOnThesis(); // ok, put in downcast // only works if s, in fact, does point to a Grad object at runtime
Inheritance Syntax and Features
GetStress() or getStress(int arg) that is different from getStress(). The compiler does not provide any warning about this by default (but @Override provides a warning).
public class Student { protected int units;
// Constructor for a new student public Student(int initUnits) { units = initUnits; // NOTE this is example of "Receiver Relative" coding -- // "units" refers to the ivar of the receiver. // OOP code is written relative to an implicitly present receiver. }
// Standard accessors for units public int getUnits() { return units; }
public void setUnits(int units) { if ((units < 0) || (units > MAX_UNITS)) { return; // Could use a number of strategies here: throw an // exception, print to stderr, return false } this.units = units; // NOTE: "this" trick to allow param and ivar to use same name }
/* Stress is units *10.
NOTE another example of "Receiver Relative" coding / public int getStress() { return(units10); }
// Grad.java
/*
Grad is a subclass of Student -- a simple example of subclassing. -adds the state of yearsOnThesis -overrides getStress() to provide a Grad specific version */ public class Grad extends Student { private int yearsOnThesis;
/* Ctor takes an initial units and initial years on thesis. */ public Grad(int units, int thesis) { // we use "super" to invoke the superclass ctor // to init that part of ourselves super(units);
// init our own ivars yearsOnThesis = thesis; }
/* Default ctor builds a Grad with 10 units and 0 yot. */ public Grad() { this(10, 0); // "this" on first line of a ctor calls // a different ctor in the same class }
/* getStress() override Grad stress is 2 * Student stress + yearsOnThesis. */ @Override() public int getStress() { // Use super.getStress() to invoke the Student // version of getStress() instead of copy/pasting that // code down here. The whole point of inheritance // is not duplicating code. int stress = super.getStress();
return(stress*2 + yearsOnThesis); }
// Standard accessors public void setYearsOnThesis(int yearsOnThesis) { this.yearsOnThesis = yearsOnThesis; }
public int getYearsOnThesis() { return(yearsOnThesis); }
/* Example client code of Student and Grad, demonstrating inheritance concepts. */ public static void main(String[] args) { Student s = new Student(13); Grad g = new Grad(13, 2); Student x = null;
System.out.println("s " + s.getStress()); System.out.println("g " + g.getStress());
// Note how g responds to everything s responds to // with a combination of inheritance and overriding... s.dropUnits(3); g.dropUnits(3); System.out.println("s " + s.getStress()); System.out.println("g " + g.getStress());
/* OUTPUT... s 130
public boolean isIrate() { return(getStress() >= 100); // POPS DOWN to Grad.getStress() // if the receiver is a Grad } ... }
Student s = new Student(10); Grad g = new Grad(10, 2); s.isIrate(); // false g.isIrate(); // true
class MyCar extends Car { //Override turn()... public void turn() { beep(); super.turn(); }