8. Inheritance8. Inheritance
““Is-a” RelationshipIs-a” Relationship
TopicsTopics
Creating SubclassesOverriding MethodsClass HierarchiesAbstract ClassInheritance and GUIsThe Timer Class
InheritanceInheritance InheritanceInheritance allows developer to derive a allows developer to derive a
new class from an existing one new class from an existing one
The existing class is called the The existing class is called the parent class,parent class, or or superclasssuperclass, or , or base classbase class
The derived class is called the The derived class is called the child classchild class or or subclasssubclass
The child inherits characteristics of the The child inherits characteristics of the parentparent
That is, the child class inherits the methods That is, the child class inherits the methods and data defined by the parent classand data defined by the parent class
UML DiagramUML Diagram
UML Diagram for Parent-Child class UML Diagram for Parent-Child class relationshiprelationship
Is-A RelationshipIs-A RelationshipChild is a more specific version of the parentChild is a more specific version of the parent
Vehicle
Car
InheritanceInheritance
A derived class can be tailored to A derived class can be tailored to specific needs byspecific needs by Adding new methodsAdding new methods Adding new dataAdding new data
Takes advantage of design, Takes advantage of design, implementation and testing result of implementation and testing result of the parent classthe parent class
Promotes reusability of code, Promotes reusability of code, modifiability of program.modifiability of program.
InheritanceInheritance Use reserved word extends to establish Use reserved word extends to establish
inheritance.inheritance.
Words.javaWords.java (page 442) (page 442) Book.javaBook.java (page 443) (page 443) Dictionary.javaDictionary.java (page 444) (page 444)
Car extends VehicleCar extends Vehicle
{{
// class contents// class contents
}}
Visibility ModifiersVisibility Modifiers Visibility modifiers affect the way that Visibility modifiers affect the way that
class members can be used in a child classclass members can be used in a child class
PrivatePrivate variables and methods cannot be variables and methods cannot be referenced by name in a child classreferenced by name in a child class
PublicPublic variables and methods are visible variables and methods are visible to all classes -- but public variables violate to all classes -- but public variables violate the principle of encapsulationthe principle of encapsulation
ProtectedProtected variables and methods are variables and methods are visible to child classes, but not to others.visible to child classes, but not to others.
InheritanceInheritance
ProtectedProtected methods or data in the methods or data in the parent class are inherited by a child parent class are inherited by a child class.class.
They are not visible to other classes, They are not visible to other classes, even in the same package.even in the same package.
super() Referencesuper() Reference Constructors are not inherited.Constructors are not inherited. To make use of a constructor in the parent To make use of a constructor in the parent
class—e.g., to use its initialization code—class—e.g., to use its initialization code—use use super(),super(), which is same as invoking which is same as invoking the parent’s constructor.the parent’s constructor.
Constructor in a child class should call Constructor in a child class should call super() as the first statement.super() as the first statement.
Words2.javaWords2.java (page 447) (page 447) Book2.javaBook2.java (page 448) (page 448) Dictionary2.javaDictionary2.java (page 449) (page 449)
TopicsTopics
Creating SubclassesOverriding MethodsClass HierarchiesAbstract ClassInheritance and GUIsThe Timer Class
Overriding a MethodOverriding a Method
An inherited method can be overridden—An inherited method can be overridden—so that it has the same signature by so that it has the same signature by completely different body definition.completely different body definition.
If such a method has a different signature, If such a method has a different signature, e.g., more parameters, then the method is e.g., more parameters, then the method is a new (overloaded) method.a new (overloaded) method.
Messages.javaMessages.java (page 452) (page 452) Thought.javaThought.java (page 453) (page 453) Advice.javaAdvice.java (page 454) (page 454)
Overloading Vs Overloading Vs OverridingOverriding
OverloadingOverloading refers to multiple methods refers to multiple methods with the same name in the same class, but with the same name in the same class, but with different signatureswith different signatures
OverridingOverriding refers to two methods, one in a refers to two methods, one in a parent class and one in a child class, that parent class and one in a child class, that have the same signaturehave the same signature
Overloading lets you define a similar Overloading lets you define a similar operation in different ways for different operation in different ways for different parametersparameters
Overriding lets you define a similar operation Overriding lets you define a similar operation in different ways for different object typesin different ways for different object types
TopicsTopics
Creating SubclassesOverriding MethodsClass HierarchiesAbstract ClassInheritance and GUIsThe Timer Class
Inheritance HierarchyInheritance Hierarchy
Business
KMart Macys
ServiceBusiness
Kinkos
RetailBusiness
A child class can be the parent class of another subclass.
Inheritance HierarchyInheritance Hierarchy Two children of the same parent are called Two children of the same parent are called
siblingssiblings
Common features should be put as high in the Common features should be put as high in the hierarchy as is reasonablehierarchy as is reasonable
An inherited member is passed continually down An inherited member is passed continually down the linethe line
Therefore, a child class inherits from all its Therefore, a child class inherits from all its ancestor classesancestor classes
There is no single class hierarchy that is There is no single class hierarchy that is appropriate for all situationsappropriate for all situations
The Object ClassThe Object Class A class called A class called ObjectObject is defined in the is defined in the java.langjava.lang package of the Java standard package of the Java standard class libraryclass library
All classes are derived from the All classes are derived from the ObjectObject classclass
If a class is not explicitly defined to be the If a class is not explicitly defined to be the child of an existing class, it is assumed to be child of an existing class, it is assumed to be the child of the the child of the ObjectObject class class
Therefore, the Therefore, the ObjectObject class is the ultimate class is the ultimate root of all class hierarchiesroot of all class hierarchies
Java 1.5
The Object ClassThe Object Class The The ObjectObject class contains a few useful class contains a few useful
methods, which are inherited by all classesmethods, which are inherited by all classes
For example, the For example, the toStringtoString method is method is defined in the defined in the ObjectObject class class
Every time we define the Every time we define the toStringtoString method, method, we are actually overriding an inherited we are actually overriding an inherited definitiondefinition
The The toStringtoString method in the method in the ObjectObject class is class is defined to return a string that contains the defined to return a string that contains the name of the object’s class along with some name of the object’s class along with some other informationother information
TopicsTopics
Creating SubclassesOverriding MethodsClass HierarchiesAbstract ClassInheritance and GUIsThe Timer Class
Abstract ClassAbstract Class
An An abstract classabstract class is a placeholder in a is a placeholder in a class hierarchy that represents a class hierarchy that represents a generic conceptgeneric concept
An abstract class cannot be An abstract class cannot be instantiatedinstantiated
We use the modifier We use the modifier abstractabstract on the on the class header to declare a class as class header to declare a class as abstract:abstract:
Abstract ClassAbstract Class
public abstract class Productpublic abstract class Product
{{
// contents// contents
}}
Abstract ClassAbstract Class
An abstract class often contains abstract An abstract class often contains abstract methods with no definitions (like an methods with no definitions (like an interface)interface)
Unlike an interface, the Unlike an interface, the abstractabstract modifier modifier must be applied to each abstract methodmust be applied to each abstract method
Also, an abstract class typically contains Also, an abstract class typically contains non-abstract methods with full definitionsnon-abstract methods with full definitions
A class declared as abstract does not have A class declared as abstract does not have to contain abstract methods -- simply to contain abstract methods -- simply declaring it as abstract makes it sodeclaring it as abstract makes it so
Abstract ClassAbstract Class The child of an abstract class must The child of an abstract class must
override the abstract methods of the override the abstract methods of the parent, or it too will be considered abstractparent, or it too will be considered abstract
An abstract method cannot be defined as An abstract method cannot be defined as finalfinal or or staticstatic
The use of abstract classes is an important The use of abstract classes is an important element of software design – it allows us element of software design – it allows us to establish common elements in a to establish common elements in a hierarchy that are too generic to hierarchy that are too generic to instantiateinstantiate
Top Related