RU beehive logo ITEC dept promo banner
ITEC 380
2012fall
ibarland
tlewis32

homelecturesexamshwsbreeze (snow day)

lect13b
inheritance
also in C++: pass-by-reference

Last lecture, we saw one way of (nearly) implementing objects, just using functions and local-variables, nested so that the function's closures enforced data-hiding.

Today we'll think a bit about (a) class-based O.O. languages (like java), and (b) instance-based O.O. languages (like javascript).

Two uses of inheritances: (a) express type constraints/promises (the primary use); (b) Re-use code (it is discouraged to use subclassing just fore this). An example of the latter is pre-1.5 Java's LinkedList implementing push and pop, but there was not an interface for Queue itself.

multiple inheritance vs. interfaces

C++ allowed multiple inheritnace, which sounds like a fine idea at first.

abstract class Vehicle {
  double speed;
  boolean hasPassedInspection;
  abstract void turn();
  abstract void accelerate( double amt );
  void inspect() { hasPassedInspection = true; }
  }

class Car :: Vehicle {
  double[] tirePressure;
  void changeTire() { ... };

  // Implementations of all the Vehicle methods:
  void turnLeft() { ... };
  // ...
  }

  
class Boat :: Vechicle {
  double keelDepth;
  boolean anchor;

  void aweigh() { anchor = true; }
  // Implementations of all the Vehicle methods:
  void turnLeft() { ... };
  // ...

  }
It makes sense that James Bond should get a Vehicle which operates on land and sea:
class Spymobile :: Car, Boat {
  // You might think, nothing else needed -- we've inherited all aspects of both!

  /*
   * However, when you call a Spymobile's `turn` method,
   * which of the above to methods should get called?  Both, or just one?
   * If both, in which order?
   * Somehow, we need to specify/resolve all fields and methods that occur
   * the nearest common ancester.
   * So you might have two fields named `super` -- `Car::super` and `Boat::super`,
   * and the Spymobile's code has to be clear about which it's using.
   * People worked around that, but had bugs introduced in more subtle ways:
   */
   }
Indeed, multiple-inheritance seemed like a good enough idea that this was included in C++, and large projects used it. However, experience showed that it led to troubles down the road, in long-term maintenance. Solution: Java, developed after ten years industrial experience with C++, decided to dis-allow multiple inheritance of code. However, it does add the concept of interfaces — you can guarantee an object will satisfy a set of methods, but there is no automatic 1 code-inheritance.

Other C++'isms


1 (If you want code-inheritance, a common solution is the “delegate pattern” —

class Spymobile extends Vehicle 
                implements Boat, Car {
  // Two delegate objects, and we'll write our methods to use them as appropriate:
  Boat b;
  Car c;
  
  void inspect() {
    b.inspect();
    c.inspect();
    }

  void accelerate() {
    // up to us, 
    }

  }
  
Delegation is not a cure-all, though — there are now three different `speed` fields happening for each Spymobile: one in the Spymobile, and one in each of its two delegates.      

homelecturesexamshwsbreeze (snow day)


©2012, Ian Barland, Radford University
Last modified 2012.Nov.28 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme