Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Revisited Other Issues. Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not.

Similar presentations


Presentation on theme: "Inheritance Revisited Other Issues. Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not."— Presentation transcript:

1 Inheritance Revisited Other Issues

2 Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not permitted in Java, but is used in C++ Also Lisp, Eiffel, etc Also Lisp, Eiffel, etc Allows a way to combine several classes Allows a way to combine several classes

3 The Diamond Problem virtual void print() = 0; print() A BC D Which print()?

4 C++ Solution Need to disambiguate the print() method by fully qualifying the members, e.g. B::print() in B. Need to disambiguate the print() method by fully qualifying the members, e.g. B::print() in B. Using nonvirtual inheritance there is still a problem with polymorphism if a and d are pointers to A and D respectively, what is Using nonvirtual inheritance there is still a problem with polymorphism if a and d are pointers to A and D respectively, what is A *a = &d; ? Way out:Use virtual base class inheritance Way out:Use virtual base class inheritance

5 C++ Solution This works if use this technique This works if use this technique Idea: Follow each inheritance path separately Idea: Follow each inheritance path separately In a way D is the union of B and C, so 2 A objects are created In a way D is the union of B and C, so 2 A objects are created  If A->B and A->C are virtual then only one A object is created  If virtual and non-virtual are mixed then there is one virtual A and a nonvirtual A for each nonvirtual inheritance path  D should provide the functionality of B and C

6 Advantages to Inheritance Defined statically, i.e. at compile time Defined statically, i.e. at compile time Easy to modify the implementation being reused Easy to modify the implementation being reused SW reuse is greatly facilitated SW reuse is greatly facilitated

7 Disadvantages to Inheritance Cannot change implementations inherited from parent class at run-time Cannot change implementations inherited from parent class at run-time Parent classes often define at least part of their subclasses implementation Parent classes often define at least part of their subclasses implementation  A change to the parent entails a change to the subclass  “Inheritance breaks encapsulation”  Best to inherit from an abstract class

8 Inheritance: Controlling Access Modifiers Modifiers  public accessible everywhere  private only accessible in defining class  protected accessible within all subclasses  accessible to classes in package  final no subclass, no override  static one copy, the class  abstract implementation in subclass final is the opposite of abstract final is the opposite of abstract

9 The Modifiers static and final static applied both to data and methods static applied both to data and methods static shared by all instances static shared by all instances static means data/method is not part of an instance static means data/method is not part of an instance final means change/override no more final means change/override no more static final variables should be private static final variables should be private

10 Benefits of Inheritance White-box reuse: reuse code/data from parent White-box reuse: reuse code/data from parent Increased reliability--reuse exposes bugs Increased reliability--reuse exposes bugs Code sharing : define a class in one place but use it in several, e.g. Java library Code sharing : define a class in one place but use it in several, e.g. Java library Interface consistency: parent propogates same behavior Interface consistency: parent propogates same behavior Rapid prototyping, e.g. beans Rapid prototyping, e.g. beans Frameworks: structure a generic solution to a common problem and use polymorphism for specific solutions Frameworks: structure a generic solution to a common problem and use polymorphism for specific solutions

11 Cost of Inheritance Using polymorphism together with inheritance means run-time lookup to decide which method is executed: dynamic binding Using polymorphism together with inheritance means run-time lookup to decide which method is executed: dynamic binding Program size: lots of superfluous baggage in importing from libraries Program size: lots of superfluous baggage in importing from libraries Message-passing overhead: Sending information back and forth between objects is not really that expensive Message-passing overhead: Sending information back and forth between objects is not really that expensive

12 Cost of Inheritance Program complexity: coupling high in an inheritance hierarchy means propogating changes is difficult: refactoring (later) Program complexity: coupling high in an inheritance hierarchy means propogating changes is difficult: refactoring (later) HW: Budd, ch. 8, due 17 May

13 Alternates to Inheritance Inheritance isn’t the only way to relate classes to each other Inheritance isn’t the only way to relate classes to each other  We can also use composition (later)  Use instance variables that refer to other objects  Embodies the has_a relation better than inheritance  Has a beautiful recursive formulation (allows unified treatment of composite and primitive objects)

14 For Example Instead of inheriting a cockroach from an insect Instead of inheriting a cockroach from an insect Insect Cockroach class Insect{ //... } class Cockroach extends Insect{ //... }

15 For Example Relate Cockroach to Insect by composition: Relate Cockroach to Insect by composition: class Insect{...// } class Cockroach{ private Insect critter= new Insect(); //... }

16 UML Diagram critter Cockroach Insect

17 Guideline Prefer composition to inheritance Prefer composition to inheritance Composition does not break encapsulation Composition does not break encapsulation


Download ppt "Inheritance Revisited Other Issues. Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not."

Similar presentations


Ads by Google