| Inheritance Inner Classes |
| inheritance |
| Similar, yet Different, Concepts |
| Inner classes | |
| Inheritance | |
| Both are based on hierarchical
(Òtree-likeÓ) structures |
| Inner Classes |
| In Java (and C++), classes can be nested within one another. | |
| Objects in the inner class has available instance variables and methods of the outer class. |
| Ways to Construct ClosedList |
| class Cell {É} class ClosedList {É} |
|
| class ClosedList { class Cell {É} ... } |
| Interpretation of Identifiers |
| In an inner class, the innermost meaning of an identifier applies. |
| Usage |
| Normally one or more objects of the inner class are created for a given object of the outer class. | |
| Objects of the inner class only make sense in the context of a supporting object of the outer class. |
| Exporting Inner Objects |
| Inner objects can be used outside, understanding that they are always relative to the object in which they are contained. |
| Example: List Iterator |
| We want to define an Iterator for a ClosedList. | |
| For read-only Iteration, the Iterator class can be defined outside the ClosedList class. | |
| For modification, such as remove(), it is sometimes necessary for the Iterator to change part of the list itself. |
| Example: List Iterator (2) |
| By making the ListIterator an inner class, we can: | ||
| Use data elements defined in the ClosedList. | ||
| Avoid exposing those data elements to the world at large. | ||
| Use Iterators outside ClosedList. | ||
| ClosedList.Iterator |
| ClosedList.Iterator:
remove() Defined to remove the value just produced by next(). |
| Test Program |
| Test Program |
| Test Program Output |
| Inheritance |
| ÒInheritanceÓ is a way of building one class on top of another | ||
| The original class is called the base class, or parent class. | ||
| The new class is called the derived class, or child class. | ||
| Diagrammatic Notation (UML) |
| Inherited Capabilities |
| Extension: The derived class can potentially use all data components and methods from the base class, and add more of its own. |
|
| Over-Riding: It can also selectively re-define or Òover-rideÓ methods of the same name. (It is a good idea to keep the same approximate meaning.) |
| Purposes of Inheritence |
| Use the same concepts and code for many classes (base-class concepts and code shared by derived classes): | ||
| Work economy | ||
| Intellectual economy | ||
| Tie together similar classes: | ||
| Increases the utility of methods that use such classes. |
||
| Extension = Java Inheritance |
| In Java, the keyword for Òinherits fromÓ is | |
| extends | |
| The derived class extends the base class. | |
| Extension allows over-riding as well; there is no separate keyword for over-riding. | |
| Extension Example |
| class Account defines a basic bank account | |
| class CheckingAccount defines a special account for check-writing |
| Slide 21 |
| Slide 22 |
| Slide 23 |
| Multiple derived classes |
| Which methods can be over-ridden? |
| In order to be over-ridden, a method must be declared either: | ||
| public | ||
| protected | ||
| in the base class. | ||
| Inheritance Examples abound in Java Libraries |
| class java.lang.Object | |
| class java.util.AbstractCollection (implements java.util.Collection) | |
| class java.util.AbstractList (implements java.util.List) | |
| class java.util.AbstractSequentialList | |
| class java.util.LinkedList (implements java.util.List) | |
| class java.util.ArrayList (implements java.util.List) | |
| class java.util.Vector (implements java.util.List) | |
| class java.util.Stack | |
| Implications of Inheritance |
| The preceding diagram means, for example, that to find all methods for class java.util.Stack, you may wish to look at: | ||
| java.util.Vector | ||
| java.util.AbstractList | ||
| java.util.AbstractCollection | ||
| java.lang.Object | ||
| Methods of java.util.Stack |
| Methods of Stack proper: | ||
| Object push(Object item) | ||
| Object pop() | ||
| boolean empty() | ||
| Object peek() | ||
| int search(Object o) | ||
| Methods of Vector: | ||
| add, add, addAll, addAll, addElement, capacity, clear, clone, contains, containsAll, copyInto, elementAt, elements, ensureCapacity, equals, firstElement, get, hashCode, indexOf, insertElementAt, isEmpty, lastElement, lastIndexOf, remove, removeAll, removeAllElements, removeElement, removeElementAt, removeRange, retainAll, set, setElementAt, setSize, size, subList, toArray, toString, trimToSize | ||
| Testing where Object is in Hierarchy |
| instanceof operator | |
| Object ob = É; | |
| if( ob instanceof Vector ) É | |
| if( ob instanceof Stack ) ... | |
| More than one can be true! |
| Casting |
| class Object |
| Object is the ancestor of all classes | ||
| Some methods of Object: | ||
| boolean equals(Object) | ||
| Class getClass(): returns the Òmost derivedÓ Class object | ||
| String toString() | ||
| Method of class Class: | ||
| String getName() | ||
| So Ob.getClass().getName() will get you the class name of the object. | ||
| Implementing an
Interface is similar to Inheritance |
| Interface Å Base Class | |
| Implementor Å Derived Class | |
| By declaring methods to use the Interface rather than the Implementor class as an argument, more generality is afforded to that method. |
| Example |
| java.util.Iterator is standard | |
| Make ClosedList.Iterator implement java.util.Iterator | |
| Any other code accepting a java.util.Iterator
can now use our: ClosedList.Iterator |
|
| We can still do everything we did before. |
| Abstract Classes |
| A class is abstract if it is not intended to be instantiated directly; rather, objects in derived classes are instantiated. | |
| Each derived-class object implicitly entails an underlying base-class object. | |
| In Java, an abstract class is
so-declared: abstract class MyClass {. . .} |
| Abstract Classes in Java |
| An abstract class is so-declared: abstract class MyClass {. . .} |
|
| A class declared abstract cannot be instantiated directly. |
| Abstract Methods |
| A method is abstract if there is no code for it in the abstract class; instead the meaning of the method is obtained from over-riding in derived classes. | |
| Only abstract classes can contain abstract methods. |
| Abstract Class Example: Tree |
| Consider the following type of Tree: | ||
| A Tree can be an Atom | ||
| A Tree can be a Composite: a pair of Sub-Trees (each of which is a tree in its own right). | ||
| Type Tree is abstract: | ||
| We never create a tree directly. | ||
| We only create an Atom or a Composite. | ||
| Tree in Java |
| Adding a Method to Tree |
| Add method int leafCount(); to Tree |
| leafCount in the base class |
| leafCount in the Derived Classes |
| Abstract Class vs. Interface |
| An abstract class can still contain data and methods; an interface cannot. | |
| It is common for an abstract class to define methods with the intention that they be over-ridden differently by each derived class. | |
| Multiple Inheritance |
| Some languages allow one class to derive from multiple base classes; Java does not. | |
| The nearest thing would be a class deriving from a single basic class and implementing an interface at the same time. |