1 Inheritance Readings: 9.1. 2 Writing classes Write an Employee class with methods that return values for the following properties of employees at a.

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 9: Inheritance and Interfaces.
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 9: Inheritance and Interfaces.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Encapsulation, this, Subclasses.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Critters; Subtype Polymorphism Reading: HW9 Handout, Chapter 9.2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-1: Inheritance reading:
Copyright 2010 by Pearson Education Topic 31 - inheritance.
Building Java Programs Chapter 9
CS 112 Introduction to Programming Inheritance Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Building Java Programs Chapter 9 Inheritance and Interfaces Copyright (c) Pearson All rights reserved.
AD Lecture #2 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism Abstract.
Inheritance - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University 1050 Wachman Hall, Main Campus
CSC 142 Computer Science II Zhen Jiang West Chester University
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 19: encapsulation, inheritance reading: (Slides adapted from Stuart.
CS 112 Introduction to Programming Inheritance Hierarchy; Polymorphism Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-1: Inheritance reading:
1 Building Java Programs Chapter 9: Inheritance and Interfaces These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 9: Inheritance and Interfaces.
Some Object-Oriented Programming (OOP) Review. Let’s practice writing some classes Write an Employee class with methods that return values for the following.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
1 Building Java Programs Chapter 9: Inheritance and Interfaces These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
Building Java Programs Chapter 9 Inheritance and Interfaces Copyright (c) Pearson All rights reserved.
Copyright 2010 by Pearson Education Building Java Programs Chapter 9 Lecture 9-1: Inheritance reading: 9.1.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ) reading:
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-3: Polymorphism reading: 9.3.
1 Interacting with the superclass (continued) suggested reading:9.4.
Building Java Programs Chapter 9 Interfaces Copyright (c) Pearson All rights reserved.
Building Java Programs
Lecture 16: Polymorphism (Part I)
Building Java Programs Chapter 9
Building Java Programs Chapter 9
Building Java Programs Chapter 9
Building Java Programs Chapter 9
Inheritance - CIS 1068 Program Design and Abstraction
Building Java Programs
Building Java Programs Chapter 9
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Lecture 15: More Inheritance
Building Java Programs
Lecture 9-2: Interacting with the Superclass (super);
Topic 32 - Polymorphism.
Adapted from slides by Marty Stepp and Stuart Reges
Topic 32 - Polymorphism.
Building Java Programs
Building Java Programs
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Law firm employee analogy
Lecture 14: Inheritance Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Building Java Programs
Topic 31 - inheritance.
Building Java Programs
Inheritance Readings: 9.1.
Lecture 17: Polymorphism (Part I)
Lecture 15: Inheritance II
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 9 9-3: Polymorphism reading: 9.3
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

1 Inheritance Readings: 9.1

2 Writing classes Write an Employee class with methods that return values for the following properties of employees at a particular company:  Work week: 40 hours  Annual salary: $40,000  Paid time off: 2 weeks  Leave of absence form: Yellow form

3 Employee class // A class to represent employees public class Employee { public int getHours() { return 40; // works 40 hours / week } public double getSalary() { return ; // $40, / year } public int getVacationDays() { return 10; // 2 weeks' paid vacation } public String getVacationForm() { return "yellow"; // use the yellow form }

4 Writing more classes Write a Secretary class with methods that return values for the following properties of secretaries at a particular company:  Work week: 40 hours  Annual salary: $40,000  Paid time off: 2 weeks  Leave of absence form: Yellow form Add a method takeDictation that takes a string as a parameter and prints out the string prefixed by "Taking dictation of text: ".

5 Secretary class // A class to represent secretaries public class Secretary { public int getHours() { return 40; // works 40 hours / week } public double getSalary() { return ; // $40, / year } public int getVacationDays() { return 10; // 2 weeks' paid vacation } public String getVacationForm() { return "yellow"; // use the yellow form } public void takeDictation(String text) { System.out.println("Taking dictation of text: " + text); }

6 Why are they very similar? // A class to represent employees public class Employee { public int getHours() { return 40; } public double getSalary() { return ; } public int getVacationDays() { return 10; } public String getVacationForm() { return "yellow"; } // A class to represent secretaries public class Secretary { public int getHours() { return 40; } public double getSalary() { return ; } public int getVacationDays() { return 10; } public String getVacationForm() { return "yellow"; } public void takeDictation(String text) { System.out.println("Taking dictation of text: " + text); }

7 Is-a relationship is-a relationship: A hierarchical connection where one category can be treated as a specialized version of another. Examples:  Every secretary is an employee.  Every dog is a mammal.  Every refrigerator is an appliance.

8 Reusing code: why re-invent the wheel? code reuse: The practice of writing program code once and using it in many contexts. We'd like to be able to say the following: // A class to represent secretaries public class Secretary { public void takeDictation(String text) { System.out.println("Taking dictation of text: " + text); } }

9 Inheritance inheritance: A way to specify a relationship between two classes where one class inherits the state and behavior of another. The child class (also called subclass) inherits from the parent class (also called superclass). The subclass receives a copy of every field and method from the superclass.

10 Inheritance syntax Creating a subclass, general syntax: public class extends { Example: public class Secretary extends Employee {.... } By extending Employee, each Secretary object receives a getHours, getSalary, getVacationDays, and getVacationForm method automatically.

11 Improved Secretary class // A class to represent secretaries public class Secretary extends Employee { public void takeDictation(String text) { System.out.println("Taking dictation of text: " + text); }

12 Writing even more classes Write a Marketer class that represents marketers who have the same properties as general employees, but instead of making only a paltry $40,000, marketers make $50,000! Can we still leverage the Employee class or do we have to re-write everything, because one method ( getSalary ) is different? If only Marketer could write a new version of the getSalary method, but inherit everything else…

13 Overriding methods override: To write a new version of a method in a subclass to replace the superclass's version. To override a superclass method, just write a new version of it in the subclass. This will replace the inherited version.

14 Marketer class // A class to represent marketers public class Marketer extends Employee { public void advertise() { System.out.println("Act now while supplies last!"); } public double getSalary() { return ; // $50, / year }

15 Based in reality or too convenient? At many companies, all new employees attend a common orientation to learn general rules (e.g., what forms to fill out when). Each person receives a big manual of these rules. Each employee also attends a subdivision-specific orientation to learn rules specific to their subdivision (e.g., marketing department). Everyone receives a smaller manual of these rules.

16 Rules, rules, everywhere The smaller manual adds some rules and also changes (read: overrides) some rules from the large manual (e.g., "use the pink form instead of the yellow form")

17 Why bother with separate manuals? Why not just have a 22-page manual for lawyers, 21-page manual for secretaries, 23-page manual for marketers, etc…?

18 Advantages of separate manuals maintenance: If a common rule changes, only the common manual needs to be updated. locality: A person can look at the manual for lawyers and quickly discover all rules that are specific to lawyers.

19 Key ideas It is useful to be able to specify general rules that will apply to many groups (the 20-page manual). It is also useful to specify a smaller set of rules for a particular group, including being able to replace rules from the overall set (e.g., "use the pink form instead of the yellow form").

20 Exercise: LegalSecretary Write a LegalSecretary class that represents legal secretaries—a special type of secretary that can file legal briefs. Legal secretaries also earn more money ($45,000).

21 Solution: LegalSecretary // A class to represent legal secretaries public class LegalSecretary extends Secretary { public void fileLegalBriefs() { System.out.println("I could file all day!"); } public double getSalary() { return ; // $45, / year }

22 Inheritance hierarchies Deep hierarchies can be created by multiple levels of subclassing. inheritance hierarchy: A set of classes connected by is-a relationships that can share common code.

23 Exercise: Lawyer Lawyers are employees that know how to sue. They get an extra week of paid vacation (a total of 3) and have to use the pink form when applying for vacation leave. Write the Lawyer class.

24 Solution: Lawyer // A class to represent lawyers public class Lawyer extends Employee { // overrides getVacationForm from Employee class public String getVacationForm() { return "pink"; } // overrides getVacationDays from Employee class public int getVacation() { return 15; // 3 weeks vacation } public void sue() { System.out.println("I'll see you in court!"); }

25 Polymorphism Readings: 9.2

26 Motivation Given the following: Lawyer laura = new Lawyer(); Marketer mark = new Marketer(); Write a program that will print out the salaries and the color of the vacation form for each employee.

27 Polymorphism A reference variable of type T can refer to an object of any subclass of T. Employee person = new Lawyer(); polymorphism: The ability for the same code to be used with several different types of objects and behave differently depending on the type of object used.

28 Properties of polymorphism Employee person = new Lawyer(); System.out.println(person.getSalary()); // System.out.println(person.getVacationForm()); // "pink" You can call any method from Employee on the person variable, but not any method specific to Lawyer (such as sue ). Once a method is called on the object, it behaves in its normal way (as a Lawyer, not as a normal Employee ).

29 Polymorphism and parameters public class EmployeeMain { public static void main(String[] args) { Lawyer laura = new Lawyer(); Marketer mark = new Marketer(); printInfo(laura); printInfo(mark); } public static void printInfo(Employee empl) { System.out.println("salary = " + empl.getSalary()); System.out.println("days = " + empl.getVacationDays()); System.out.println("form = " + empl.getVacationForm()); System.out.println(); } } Output: salary = vacation days = 15 vacation form = pink salary = vacation days = 10 vacation form = yellow

30 Polymorphism and arrays public class EmployeeMain2 { public static void main(String[] args) { Employee[] employees = { new Lawyer(), new Secretary(), new Marketer(), new LegalSecretary() }; for (int i = 0; i < employees.length; i++) { System.out.println("salary = " + employees[i].getSalary()); System.out.println("vacation days = " + employees[i].getVacationDays()); System.out.println(); } } } Output: salary = vacation days = 15 salary = vacation days = 10 salary = vacation days = 10 salary = vacation days = 10

31 Exercise 1 public class Foo { public void method1() { System.out.println("foo 1"); } public void method2() { System.out.println("foo 2"); } public String toString() { return "foo"; } public class Bar extends Foo { public void method2() { System.out.println("bar 2"); } public class Baz extends Foo { public void method1() { System.out.println("baz 1"); } public String toString() { return "baz"; } public class Mumble extends Baz { public void method2() { System.out.println("mumble 2"); } Assume that the following four classes have been declared:

32 Exercise 1 What would be the output of the following client code? Foo[] pity = { new Baz(), new Bar(), new Mumble(), new Foo() }; for (int i = 0; i < pity.length; i++) { System.out.println(pity[i]); pity[i].method1(); pity[i].method2(); System.out.println(); }

33 Diagramming polymorphic code

34 Finding output with tables method FooBarBazMumble method1 method2 toString method FooBarBazMumble method1foo 1 baz 1 method2foo 2bar 2foo 2mumble 2 toStringfoo baz method FooBarBazMumble method1foo 1baz 1 method2foo 2bar 2mumble 2 toStringfoobaz

35 Solution 1 The code produces the following output: baz baz 1 foo 2 foo foo 1 bar 2 baz baz 1 mumble 2 foo foo 1 foo 2

36 Exercise 2 public class Lamb extends Ham { public void b() { System.out.println("Lamb b"); } public class Ham { public void a() { System.out.println("Ham a"); } public void b() { System.out.println("Ham b"); } public String toString() { return "Ham"; } public class Spam extends Yam { public void a() { System.out.println("Spam a"); } public class Yam extends Lamb { public void a() { System.out.println("Yam a"); } public String toString() { return "Yam"; } Assume that the following four classes have been declared:

37 Exercise 2 What would be the output of the following client code? Ham[] food = { new Spam(), new Yam(), new Ham(), new Lamb() }; for (int i = 0; i < food.length; i++) { System.out.println(food[i]); food[i].a(); food[i].b(); System.out.println(); }

38 Diagramming polymorphic code

39 Finding output with tables method HamLambYamSpam a b toString method HamLambYamSpam aHam a Yam aSpam a bHam bLamb b toStringHam Yam method HamLambYamSpam aHam aYam aSpam a bHam bLamb b toStringHamYam

40 Solution 2 The code produces the following output: Yam Spam a Lamb b Yam Yam a Lamb b Ham Ham a Ham b Ham Ham a Lamb b