3 InheritanceInheritance is a fundamental object- oriented design technique used to create and organize reusable classesClasses are able to re-use other classesLike inheritance in people, child classes inherit properties from their parent classesWe have said all classes inherit from Object – but what does this mean?
4 Inheritance How do you get one class to inherit from another? What happens to private fields and methods?What happens to public fields and methods?What is method overriding?What is super?
5 Abstract classes What’s the use of this concept? How do we make a class abstract?What is an abstract method?What are the rules associated with abstract methods?
6 Benefits of software reuse Initially, "new" software has a large number of defectsTesting and debugging reduce the number of defects, up to a pointAfter a while, more you start introducing new defects during debugging!Want to reuse good, tested code without changing it
7 Software reuse Also has the added benefit that changes are centralized As opposed to having multiple versions of the same code that you have to update in the same way each timeIf everyone is using the same piece of code, it gets run more, and is therefore, in theory likely to be better de-bugged
8 Multiple InheritanceJava supports single inheritance, meaning that a derived class can have only one parent classMultiple inheritance allows a class to be derived from two or more classes, inheriting the members of all parentsCollisions, such as the same variable name in two parents, have to be resolved
9 Overloading vs. Overriding Overloading deals with multiple methods with the same name in the same class, but with different signaturesOverriding deals with two methods, one in a parent class and one in a child class, that have the same signatureOverloading lets you define a similar operation in different ways for different parametersOverriding lets you define the same operation in a different way for the child class
10 The Object ClassA class called Object is defined in the java.lang package of the Java standard class libraryAll classes are derived from the Object classIf a class is not explicitly defined to be the child of an existing class, it is assumed to be the child of the Object classTherefore, the Object class is the ultimate root of all class hierarchies
11 The Object ClassThe Object class contains a few useful methods, which are inherited by all classesFor example, the toString method is defined in the Object classEvery time we define the toString method, we are actually overriding an inherited definitionThe toString method in the Object class is defined to return a string that contains the name of the object's class along with some other information
12 The Object ClassThe equals method of the Object class returns true if two references are aliasesWe can override equals in any class to define equality in some more appropriate wayAs we've seen, the String class defines the equals method to return true if two String objects contain the same charactersThe designers of the String class have overridden the equals method inherited from Object in favor of a more useful version
13 equalsIf equals is not overridden, it behaves the same way as for Object:Two objects are considered the same ONLY if they both have the same memory address pointer (reference)This is kind of silly, when you would probably consider two objects the same when their contents are the sameYou will often want to implement your own version of equals for classes you createBecause equals is defined by Object, every object can be accessed this way, and many Java classes expect to use your class' equals method because it's guaranteed to exist (just may not be doing the "right" thing by default)
14 Inheritance Design Issues Every derivation should be an is-a relationshipThink about the potential future of a class hierarchy, and design classes to be reusable and flexibleFind common characteristics of classes and push them as high in the class hierarchy as appropriateOverride methods as appropriate to tailor or change the functionality of a childAdd new variables to children, but don't redefine (shadow) inherited variables
15 Inheritance Design Issues Allow each class to manage its own data; use the super reference to invoke the parent's constructor to set up its dataEven if there are no current uses for them, override general methods such as toString and equals with appropriate definitionsUse abstract classes to represent general concepts that lower classes have in commonUse visibility modifiers carefully to provide needed access without violating encapsulation
16 Restricting Inheritance The final modifier can be used to curtail inheritanceIf the final modifier is applied to a method, then that method cannot be overridden in any descendent classesIf the final modifier is applied to an entire class, then that class cannot be used to derive any children at allThus, an abstract class cannot be declared as final – it would be useless!These are key design decisions, establishing that a method or class should be used as-is, and no variation in implementation is possible.
18 Final note: Nested/inner classes Java allows you to define one class inside of anotherNon-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared privateStatic nested classes do not have access to other members of the enclosing classWhy use nested classes?