Introduction to Inheritance Fall 2005 OOPD John Anthony.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 1 Inheritance University Of Ha’il.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
C8: Understanding Inheritance. Intuitive description Intuitive: FLORISTS are SHOPKEEPERS, inheriting various shopkeeper behaviors Tension in OOP languages:
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Copyright W. Howden1 Lecture 15: Generalization, Polymorphism and States.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
BY:- TOPS Technologies
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Sections Inheritance and Abstract Classes
Lecture 12 Inheritance.
Inheritance and Polymorphism
ATS Application Programming: Java Programming
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
CIS 199 Final Review.
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
CS 240 – Advanced Programming Concepts
Presentation transcript:

Introduction to Inheritance Fall 2005 OOPD John Anthony

Remember our Team Example

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.

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

Let’s Collapse the Hierarchy Into One Type

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()); }

Let’s Create a Class for Each Team Member Type

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()); }

Now with Inheritance

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()); }

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?

Subclass java.util.Vector java.util.Stack public Object peek(); Public Object pop(); Public Object elementAt(int index) Vector myStack = new Stack();

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)

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

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;

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

Polymorphic Function A function that can take objects of various classes (or types). public void addTeamMember(InnovationLabArchitect member) {…} vs. public void addTeamMember(TeamMember member) {…}

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

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").

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?

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

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()

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)

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?

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.

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.

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

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.