Reminder: Inheritance Is For : CS2110: SW Development Methods
Reminder: Inheritance Is For : CS2110: SW Development Methods
Forms of Inheritance
Let's re-visit why we might choose to use inheritance again Textbook, pp. 52-56: There are five forms of inheritance
Specification, Specialization, Extension, Limitation, and Combination
Inheritance of Implementation
Some common behaviors are implemented in the superclass
Each subclass may override a method, or implement a few abstract methods Superclass specifies what and how
Inheritance of Interface
In general in OO, interface refers to what operations can be performed on an object
More general use than Javas keyword interface Interface defined by class public methods
This is the book's specification form of inheritance Vote: Could you use an abstract class to do this?
But Other things support clock-like activities: get and set time
CellPhone: not IS-A Clock Computer: not IS-A Clock
2.
Limitation
A subclass removes or blocks a behavior defined in the superclass Not supported directly in Java, but
Collections library: Sometimes a subclass will throw an
3.
UnsupportedOperationException
4.
Combination
A superclass/subclass exhibits more than one Multiple-inheritance supported in some languages
If you could do limitation, do you think this has a negative impact on the OO substitutability principle? 1. Yes 2. No
But, it can also inherit an interface from one or many interface definitions Java term: a class implements an interface
The class says: I promise to support the operations listed in this interface!
Note this is Java-specific terminology! Most languages just call this inheritance (of interface)
Interfaces in Java
Widely used and powerful! Cleaner / safer than allowing full multiple inheritance (like C++ does)
public class CellPhone implements TimePiece, Ringable {... public class WallClock extends GenericClock implements Ringable {
You now see how polymorphism supports this Now, two ideas about what makes a good design
Abstraction; Hiding Design Decisions
Examples: PlayableItem p; // abstract class TimePiece t; // interface public void syncTimeWith(TimePiece t) {} ArrayList<PlayableItem> thePlayList;
Summary
Pure inheritance of interface
Java lets you define an interface (like an empty class, just method stubs, no fields) Classes can implement one or many interfaces
BTW, one interface definition can extend another one (i.e. add to it)
An object-reference to a Java interface or a superclass is a use of abstraction Coding using abstractions makes systems more flexible, easy to change, easier to re-use parts
Could rephrase this as coding to abstractions
Result: better software design (Seems vague? Too abstract? Don't worry. You'll see more later!)
Hiding exactly how that abstraction is implemented (i.e. its real class) from the code that uses it
Prefer interfaces!
Can mix in several. (Can only extend one class.) Not hierarchical.