Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ) reading: 9.3 - 9.4.

Slides:



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

Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
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.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
1 Inheritance Readings: Writing classes Write an Employee class with methods that return values for the following properties of employees at a.
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
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-x: Critters reading: HW9 Spec.
CS 112 Introduction to Programming Inheritance Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
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.,
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
1 Building Java Programs Chapter 9: Inheritance and Interfaces These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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 ); Discussion of Homework 9:
Copyright 2010 by Pearson Education Building Java Programs Homework 8: Critters reading: Critters Assignment Spec.
Copyright 2010 by Pearson Education Homework 8: Critters reading: HW8 spec.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
Copyright 2009 by Pearson Education Building Java Programs Chapter 8: Classes Lecture 8-3: More Critters, static.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1.
CS 112 Introduction to Programming Class Inheritance Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
CS 112 Introduction to Programming Method Overriding; Object Hierarchy; Event-Driven Programming Yang (Richard) Yang Computer Science Department Yale University.
1 Interacting with the superclass (continued) suggested reading:9.4.
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs Chapter 9
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs Chapter 9
Building Java Programs
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Lecture 15: More Inheritance
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 9-2: Interacting with the Superclass (super);
Adapted from slides by Marty Stepp and Stuart Reges
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
Law firm employee analogy
Building Java Programs
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 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
Homework 8: Critters (cont.)
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ) reading:

Copyright 2008 by Pearson Education 2 Changes to common behavior Let's return to our previous company/employee example. Imagine a company-wide change affecting all employees. Example: Everyone is given a $10,000 raise due to inflation. The base employee salary is now $50,000. Legal secretaries now make $55,000. Marketers now make $60,000. We must modify our code to reflect this policy change.

Copyright 2008 by Pearson Education 3 Modifying the superclass // A class to represent employees in general (20-page manual). public class Employee { public int getHours() { return 40; // works 40 hours / week } public double getSalary() { return ; // $50, / year }... } Are we finished? The Employee subclasses are still incorrect. They have overridden getSalary to return other values.

Copyright 2008 by Pearson Education 4 An unsatisfactory solution public class LegalSecretary extends Secretary { public double getSalary() { return ; }... } public class Marketer extends Employee { public double getSalary() { return ; }... } Problem: The subclasses' salaries are based on the Employee salary, but the getSalary code does not reflect this.

Copyright 2008 by Pearson Education 5 Calling overridden methods Subclasses can call overridden methods with super super. method ( parameters ) Example: public class LegalSecretary extends Secretary { public double getSalary() { double baseSalary = super.getSalary(); return baseSalary ; }... } Exercise: Modify Lawyer and Marketer to use super.

Copyright 2008 by Pearson Education 6 Improved subclasses public class Lawyer extends Employee { public String getVacationForm() { return "pink"; } public int getVacationDays() { return super.getVacationDays() + 5; } public void sue() { System.out.println("I'll see you in court!"); } public class Marketer extends Employee { public void advertise() { System.out.println("Act now while supplies last!"); } public double getSalary() { return super.getSalary() ; }

Copyright 2008 by Pearson Education 7 Calling overridden methods super. method ( parameters ) Example: public class LegalSecretary extends Secretary { public double getSalary() { double baseSalary = super.getSalary(); return baseSalary ; }... } Recall: Subclasses can call overridden methods with super.

Copyright 2008 by Pearson Education 8 Inheritance and constructors Imagine that we want to give employees more vacation days the longer they've been with the company. For each year worked, we'll award 2 additional vacation days. When an Employee object is constructed, we'll pass in the number of years the person has been with the company. This will require us to modify our Employee class and add some new state and behavior. Exercise: Make necessary modifications to the Employee class.

Copyright 2008 by Pearson Education 9 Modified Employee class public class Employee { private int years; public Employee(int initialYears) { years = initialYears; } public int getHours() { return 40; } public double getSalary() { return ; } public int getVacationDays() { return * years; } public String getVacationForm() { return "yellow"; }

Copyright 2008 by Pearson Education 10 Problem with constructors Now that we've added the constructor to the Employee class, our subclasses do not compile. The error: Lawyer.java:2: cannot find symbol symbol : constructor Employee() location: class Employee public class Lawyer extends Employee { ^ The short explanation: Once we write a constructor (that requires parameters) in the superclass, we must now write constructors for our employee subclasses as well. The long explanation: (next slide)

Copyright 2008 by Pearson Education 11 The detailed explanation Constructors are not inherited. Subclasses don't inherit the Employee(int) constructor. Subclasses receive a default constructor that contains: public Lawyer() { super(); // calls Employee() constructor } But our Employee(int) replaces the default Employee(). The subclasses' default constructors are now trying to call a non-existent default Employee constructor.

Copyright 2008 by Pearson Education 12 Calling superclass constructor super( parameters ); Example: public class Lawyer extends Employee { public Lawyer(int years) { super(years); // calls Employee constructor }... } The super call must be the first statement in the constructor. Exercise: Make a similar modification to the Marketer class.

Copyright 2008 by Pearson Education 13 Modified Marketer class // A class to represent marketers. public class Marketer extends Employee { public Marketer(int years) { super(years); } public void advertise() { System.out.println("Act now while supplies last!"); } public double getSalary() { return super.getSalary() ; } } Exercise: Modify the Secretary subclass. Secretaries' years of employment are not tracked. They do not earn extra vacation for years worked.

Copyright 2008 by Pearson Education 14 Modified Secretary class // A class to represent secretaries. public class Secretary extends Employee { public Secretary() { super(0); } public void takeDictation(String text) { System.out.println("Taking dictation of text: " + text); } } Since Secretary doesn't require any parameters to its constructor, LegalSecretary compiles without a constructor. Its default constructor calls the Secretary() constructor.

Copyright 2008 by Pearson Education 15 Inheritance and fields Try to give lawyers $5000 for each year at the company: public class Lawyer extends Employee {... public double getSalary() { return super.getSalary() * years; }... } Does not work; the error is the following: Lawyer.java:7: years has private access in Employee return super.getSalary() * years; ^ Private fields cannot be directly accessed from subclasses. One reason: So that subclassing can't break encapsulation. How can we get around this limitation?

Copyright 2008 by Pearson Education 16 Improved Employee code Add an accessor for any field needed by the subclass. public class Employee { private int years; public Employee(int initialYears) { years = initialYears; } public int getYears() { return years; }... } public class Lawyer extends Employee { public Lawyer(int years) { super(years); } public double getSalary() { return super.getSalary() * getYears(); }... }

Copyright 2008 by Pearson Education 17 Revisiting Secretary The Secretary class currently has a poor solution. We set all Secretaries to 0 years because they do not get a vacation bonus for their service. If we call getYears on a Secretary object, we'll always get 0. This isn't a good solution; what if we wanted to give some other reward to all employees based on years of service? Redesign our Employee class to allow for a better solution.

Copyright 2008 by Pearson Education 18 Improved Employee code Let's separate the standard 10 vacation days from those that are awarded based on seniority. public class Employee { private int years; public Employee(int initialYears) { years = initialYears; } public int getVacationDays() { return 10 + getSeniorityBonus(); } // vacation days given for each year in the company public int getSeniorityBonus() { return 2 * years; }... } How does this help us improve the Secretary ?

Copyright 2008 by Pearson Education 19 Improved Secretary code Secretary can selectively override getSeniorityBonus ; when getVacationDays runs, it will use the new version. Choosing a method at runtime is called dynamic binding. public class Secretary extends Employee { public Secretary(int years) { super(years); } // Secretaries don't get a bonus for their years of service. public int getSeniorityBonus() { return 0; } public void takeDictation(String text) { System.out.println("Taking dictation of text: " + text); }

Copyright 2008 by Pearson Education 20 Critter exercise: Toad Write a critter class Toad : Does some of this behavior sound familiar? MethodBehavior constructor public Toad() eat Never eats (the default eating behavior) fight Always forfeits (the default fighting behavior) getColor brown (red=192, green=128, blue=0) getMove Walks west once every 5 moves: C, C, C, C, W, C, C, C, C, W,... toString"F"

Copyright 2008 by Pearson Education 21 Modified Frog import java.awt.*; // for Color public class Frog extends Critter { private int age; private int count; public Frog(int age) { this.age = age; this.count = 0; } public Direction getHopDirection() { // added so that it can return Direction.EAST; // be overridden by Toad } public Direction getMove() { count++; if (count >= age) { // go EAST once every 'age' moves count = 0; return getHopDirection(); } else { return Direction.CENTER; }... }

Copyright 2008 by Pearson Education 22 Toad solution import java.awt.*; // for Color public class Toad extends Frog { private static final Color BROWN = new Color(192, 128, 0); public Toad() { super(5); } public Color getColor() { return BROWN; } public Direction getHopDirection() { // overrides the version return Direction.WEST; // from Frog }

Copyright 2008 by Pearson Education 23 Critter: WhiteRabbit In section, you wrote a Rabbit critter Hops: N N, S S, E E, N N, S S, E E,... Let's write WhiteRabbit White, not brown Hops in cycles of 5 (N*5, S*5, E, E, N*5, S*5, E, E,...)