Download presentation
Presentation is loading. Please wait.
1
Introduction to Inheritance Fall 2005 OOPD John Anthony
2
Remember our Team Example
3
What is it The property of objects by which instances of a class can have access to data and method definitions contained in a previously defined class, without those definitions being restated. Inheritance is a "parent/child" relationship between two classes, the "superclass" (parent) and "subclass" (child). The child "inherits" instance variables and methods from the parent.
4
Reasons to Use Inheritance As a means of code reuse (extension) As a means of code reuse (extension) As a means of concept reuse (overriding) As a means of concept reuse (overriding) Supports the notion of loose coupling Supports the notion of loose coupling
5
Let’s Collapse the Hierarchy Into One Type
6
Here is what the code may look like… public void noInheritanceExample2() { Team aTeam = new Team(1); TeamMember genericMember = new TeamMember(TeamMember.TEAM_MEMBER); aTeam.addTeamMember(genericMember); TeamMember developer = new TeamMember(TeamMember.INNOVATION_LAB_DEVELOPER); aTeam.addTeamMember(developer); //or shortcut aTeam.addTeamMember(new TeamMember(TeamMember.INNOVATION_LAB_ARCHITECT)); //display the burn rates Iterator teamMembers = aTeam.retrieveTeamMembers().iterator(); while(teamMembers.hasNext()) { TeamMember member = (TeamMember)teamMembers.next(); if(member.getMemberType() == TeamMember.TEAM_MEMBER) { System.out.println("Burn Rate = " + member.calculateBurnRate()); } else if(member.getMemberType() == TeamMember.INNOVATION_LAB_DEVELOPER) { System.out.println("Burn Rate = " + member.calculateBurnRateForDeveloper()); } else { System.out.println("Burn Rate = " + member.CalculateBurnRateForArchitect()); }
7
Let’s Create a Class for Each Team Member Type
8
Here is what the code may look like… public void noInheritanceExample1() { Team aTeam = new Team(1); aTeam.addTeamMember(new TeamMember()); aTeam.addArchitect(new InnovationLabArchitect()); aTeam.addDeveloper(new InnovationLabDeveloper()); //display burn rate for each team member. Iterator teamMembers = aTeam.retrieveTeamMembers().iterator(); while(teamMembers.hasNext()) { Object someMember = teamMembers.next(); if( someMember instanceof InnovationLabArchitect) { InnovationLabArchitect architect = (InnovationLabArchitect)someMember; System.out.println(architect.calculateBurnRate()); } else if( someMember instanceof InnovationLabDeveloper) { InnovationLabDeveloper developer = (InnovationLabDeveloper)someMember; System.out.println(developer.calculateBurnRate()); } else { TeamMember member = (TeamMember)someMember; System.out.println(member.calculateBurnRate()); }
9
Now with Inheritance
10
Here is what the code may look like public void inheritanceExample() { Team aTeam = new Team(1); TeamMember genericMember = new TeamMember(); aTeam.addTeamMember(genericMember); aTeam.addTeamMember(new InnovationLabArchitect()); aTeam.addTeamMember(new InnovationLabDeveloper()); Iterator teamMembers = aTeam.retrieveTeamMembers().iterator(); while(teamMembers.hasNext()) { TeamMember member = (TeamMember)teamMembers.next(); System.out.println("Burn Rate = " + member.calculateBurnRate()); }
11
Liskov Substitution Principle (subtyping) If S is a subtype of T, then objects of type T may be replaced with objects of type S without altering the correctness of the program. For a function f(A a), if f satisfies its specification when passed an object whose actual type is A, f also satisfies its specification when passed an object whose actual type is B. public void addTeamMember(TeamMember member) {…} What happens when an instance of InnovationLabArchitect is passed?
12
Subclass java.util.Vector java.util.Stack public Object peek(); Public Object pop(); Public Object elementAt(int index) Vector myStack = new Stack();
13
Polymorphism Polymorphism – the ability to take different forms. In OO, it refers to the ability of executing different operations (methods) in response to the same message. Types of Polymorphism: Polymorphic variable & function Polymorphic variable & function Overloading (later) Overloading (later) Overriding (later) Overriding (later) Generics (parameterized types) (later) Generics (parameterized types) (later)
14
Polymorphic Variable A variable (or reference) that can be attached to more than one type of object. A variable (or reference) that can be attached to more than one type of object. TeamMember member = new TeamMember(); InnovationLabArchitect architect = new InnovationLabArchitect(); InnovationLabArchitect architect = new InnovationLabArchitect (); polymorphic variable non polymorphic variable
15
Polymorphic Attachment What is the difference between a reference and an object? TeamMember member = new TeamMember(); InnovationLabArchitect architect = new InnovationLabArchitect(); InnovationLabArchitect architect = new InnovationLabArchitect (); TeamMember InnovationLabArchitectmember architect before after member = architect;
16
Example I public static void main(String args[]) { TeamMember member = new TeamMember(); System.out.println(member.toString()); InnovationLabArchitect architect = new InnovationLabArchitect(); System.out.println(architect.toString()); member = architect; System.out.println(member.toString()); architect = (InnovationLabArchitect)member; System.out.println(architect.toString()); } public String toString() { return "I am a TeamMember!"; } TeamMember public String toString() { return "I am a Lab Architect!"; } InnovationLabArchitect
17
Polymorphic Function A function that can take objects of various classes (or types). public void addTeamMember(InnovationLabArchitect member) {…} vs. public void addTeamMember(TeamMember member) {…}
18
Overriding (intro) A method in a subclass with the same signature and return type as a method in the superclass overrides the superclass's method. Rules for Overriding 1. Method name must match 2. Some number and type (or subtype) of parameters 3. Same return value (or subclass) 4. Same throws clause (or subclass) 5. Access modifier can allow or more access but not less. 6. Parent method must not be final
19
Overloading (intro) Occurs when a class declares two or more methods with the same name but different signatures. When a message is sent to an object or class with overloaded methods, the method with the best matching signature is the one that is used ("invoked"). When a message is sent to an object or class with overloaded methods, the method with the best matching signature is the one that is used ("invoked").
20
Overloading (intro) con’t Signature can differ by order, number, and type of arguments. Signature can differ by order, number, and type of arguments. public void foo(String x, int y) { } public void foo(int y, String x) { } public String foo(int y, String x) { return null; } public void foo(int y, String x, int z) { } While two lines will generate a duplicate method error?
21
Forms of Inheritance Subclassing for specialization (subtyping) Subclassing for specialization (subtyping) Subclassing for specification Subclassing for specification Subclassing for construction Subclassing for construction Subclassing for generalization Subclassing for generalization Subclassing for extension Subclassing for extension Subclassing for limitation Subclassing for limitation Subclassing for variance Subclassing for variance Subclassing for combination Subclassing for combination
22
Subclassing for specialization (subtyping) The subclass is a specialized form the parent class. Most common use (and perhaps best use of) inheritance. Most common use (and perhaps best use of) inheritance. Satisfies the substitution principle. Satisfies the substitution principle. Consider the InnovationLabArchitect as a subclass as TeamMember. Consider the InnovationLabArchitect as a subclass as TeamMember. calculateBurnRate() calculateBurnRate()
23
Subclassing for specification Subclass adheres to the contract as specified by the parent (i.e. does not add additional features). provides implementations for abstract or deferred methods in the parent class. provides implementations for abstract or deferred methods in the parent class. subclasses in this cases are not specialization of the parent class but rather realizations of an incomplete implementation. subclasses in this cases are not specialization of the parent class but rather realizations of an incomplete implementation. All classes that implement interfaces are Subclassing for specification. All classes that implement interfaces are Subclassing for specification. java.util.AbstractList java.util.ArrayList java.util.AbstractCollection public abstract int size() public abstract int size(), public abstract Object get(int index)
24
Subclassing for construction Used when there is functionality in the parent class that the child class desires. However, the parent class is not a supertype of the child class. Consider inheriting from a List class in order to build a Set. Consider inheriting from a List class in order to build a Set. What do you think about this? What do you think about this? Does this break the principle of substitution? Does this break the principle of substitution? What are the advantages to form of inheritance? What are the advantages to form of inheritance?
25
Subclassing for generalization The subclass offers more features (behavior or state) than the parent class. Opposite of subclassing for specialization Opposite of subclassing for specialization Often used when the parent class cannot be modified (i.e. use of frameworks). Often used when the parent class cannot be modified (i.e. use of frameworks). Usually results in overriding a feature from the parent class. Usually results in overriding a feature from the parent class. Override *Drivers() features to throw an exception if called.
26
Subclassing for extension The subclass adds additional features to the parent class. Since parent features are not altered, subclasses are always subtypes. Since parent features are not altered, subclasses are always subtypes.
27
Subclassing for limitation Seeks to limit the behavior of the parent class by overriding parent features. Implementer may override a method and then throw an exception when the “restricted” method is called. Implementer may override a method and then throw an exception when the “restricted” method is called. Breaks the goal of substitution Breaks the goal of substitution Example: create stack from deque Example: create stack from deque
28
Subclassing for combination We call this multiple inheritance… Looks to combine the features of two parent classes into one. Looks to combine the features of two parent classes into one. Consider InnovationLabArchitect inheriting from both a TeamMember and InnovationLabDeveloper class. Consider InnovationLabArchitect inheriting from both a TeamMember and InnovationLabDeveloper class.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.