Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 112 Introduction to Programming Class Inheritance Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone: 432-6400 Email:

Similar presentations


Presentation on theme: "CS 112 Introduction to Programming Class Inheritance Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone: 432-6400 Email:"— Presentation transcript:

1 CS 112 Introduction to Programming Class Inheritance Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

2 2 Admin  Project teaming and mentor TFs on Google doc m Please get in touch with the TFs to setup meetings (at least once per week, for 45 minutes each meeting)

3 3 Recap: GeoMap and Composition/Association Relationships GeoMapRegion Polygon Color Point 1mm2 111m A composition relationship An association relationship

4 A Law Firm Problem: Setting  The firm has 5 types of employees m Standard employee m Secretary knows how to prepare ordinary documents m Legal secretary knows how to prepare both ordinary documents and legal documents. m Marketer knows how to advertise m Lawyer knows how to sue 4

5 A Law Firm Problem: Policies  Work time policy: Employees work 40 hours / week.  Pay policy: Employees make a base salary of $50,000 per year, except that o legal secretaries make 10% extra over base per year, o marketers make 20% extra over base per year, o lawyers who reach partner level get bonus.  Vacation policy: Employees have 2 weeks of paid vacation leave per year, except that o lawyers get an extra week on top of base, o employees should use a yellow form to apply for leave, except for lawyers who use a pink form. 5

6 Recap: The Employee class public class Employee { public int hours() { return 40; // works 40 hours / week } public double pay() { return 50000.0; // $50,000.00 / year } public int vacationDays() { return 10; // 2 weeks' paid vacation } public String vacationForm() { return "yellow"; // use the yellow form } public String toString() { String result = "Hours: " + hours() + "\n"; result += "Pay: " + pay() + "\n"; result += "Vacation days: " + vacationDays() + "\n"; result += "Vacation Form: " + vacationForm() + "\n"; return result; } } 6

7 Recap: Secretary without Reuse public class Secretary { public int hours() { return 40; // works 40 hours / week } public double pay() { return 50000.0; // $50,000.00 / year } public int vacationDays() { return 10; // 2 weeks' paid vacation } public String vacationForm() { return "yellow"; // use the yellow form } public String toString() { String result += "Hours: " + hours() + "\n"; result += "Pay: ” + pay() + "\n"; result += "Vacation days: " + vacationDays() + "\n"; result += "Vacation Form: " + vacationForm() + "\n"; return result; } public void prepareDoc(String text) { System.out.println(“Working on Document: " + text); } } 7

8 Recap: Defining Secretary using Inheritance // A class to represent secretaries. public class Secretary extends Employee { public void prepareDoc(String text) { System.out.println(“Working on document: " + text); }  By extending Employee, each Secretary object now: receives methods hours, pay, vacationDays, vacationForm, toString from Employee ’s definition automatically can be treated as an Employee by client code (seen later)  Now we only write the parts unique to each type. 8

9 9 Recap: Inheritance Relationship Inheritance should create an is-a relationship, meaning the child is a more specific version of the parent Animal - weight : int + getWeight() : int Bird + fly() : void - flySpeed : int public class extends { } A main technique for software reuse

10 10 Outline  Admin and recap  Class inheritance o why and how? o inheritance and object construction

11 11 Inheritance and Constructor  When constructing an object, Java makes sure that the constructor of the parent is first called  If no parent constructor called, Java automatically inserts super() as the first statement in the constructor of a child class: public class Secretary extends Employee { public Secretary () { // super() is automatically inserted System.out.println(“In Secretary()”); } … }

12 Example 12 public class Secretary extends Employee { public Secretary() { // super() is automatically inserted System.out.println(“In Secretary()”); } … } public class Employee { public Employee() { System.out.println(“In Employee()”); } … } public static void main(String[] args) { Secretary seth = new Secretary(); } In Employee() In Secretary() Output:

13 Exercise: Add Name to Employee public class Employee { private String name; public Employee(String name) { this.name = name; } … } 13

14 Problem with constructors  Now that we've added the constructor to the Employee class, our subclasses do not compile.  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 couple slides)

15 15 The explanation  Constructors aren't inherited.  The Employee subclasses don't inherit the public Employee(String name) constructor.  After defining public Employee(String), Java sees that we have a constructor, and will no longer provide a default Employee constructor.  i.e., public Employee() is not defined unless we define it explicitly  But public Secretary() { // super() is automatically inserted but not defined // in Employee System.out.println(“In Secretary()”); }

16 16 super and Constructor  If you insert super(…) as the first statement in child’s constructor, Java will not insert the default parent constructor: public class Secretary extends Employee { public Secretary(String name) { super(name); System.out.println(“In Secretary()”); } … } public class Employee { private String name; public Employee(String name) { System.out.println(“In Employee()”); this.name = name; } … }

17 Exercise: Reuse Existing Class  Assume you find a Picture class (assume no source file, only.class file)

18 Exercise: Extending the Picture Type  Although the Picture class is quite useful already, it misses some useful filtering functions such as grayScale, scaling  Goal: create a new type InstaPic with all of the existing methods defined in Picture, with two additional methods: gray and scale 18

19 Monochrome Luminance  Monochrome luminance. Effective brightness of a color (NTSC formula): Y = 0.299r + 0.587g + 0.114b. import java.awt.Color; public class Luminance { public static double lum(Color c) { int r = c.getRed(); int g = c.getGreen(); int b = c.getBlue(); return.299*r +.587*g +.114*b; }

20 Grayscale Filter mandrill.jpg InstPic.java

21 Image Scaling  Goal. Shrink or enlarge an image to desired size.  Assume uniform strategy to convert from w s -by- h s to w t -by- h t : ? source image (w s -by-h s ) target image (w t -by-h t ) y (x,y) y * h s / h t )(x * w s / w t,

22 Color Compatibility  Q. Which font colors will be most readable with which background colors on computer and cell phone screens?  A. Rule of thumb: difference in luminance should be  128. public static boolean compatible(Color a, Color b) { return Math.abs(lum(a) - lum(b)) >= 128.0; } 208255281410547

23 Scaling Filter mandrill.jpg (298-by-298)

24 Offline Exercise  You can add a lot more functions such as selecting a region of the picture and zoom, … 24


Download ppt "CS 112 Introduction to Programming Class Inheritance Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone: 432-6400 Email:"

Similar presentations


Ads by Google