The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.

Slides:



Advertisements
Similar presentations
CS 112 Introduction to Programming
Advertisements

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 2010 by Pearson Education 1 Assignment 11: Critters reading: HW11 assignment spec.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-1: Inheritance reading:
Copyright 2010 by Pearson Education Topic 31 - inheritance.
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:
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-1: Inheritance reading:
Some Object-Oriented Programming (OOP) Review. Let’s practice writing some classes Write an Employee class with methods that return values for the following.
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.
Programming Abstractions Cynthia Lee CS106X. Inheritance Topics Inheritance  The basics › Example: Stanford GObject class  Polymorphism › Example: Expression.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
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 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 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.
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1.
Programming Abstractions Cynthia Lee CS106B. Inheritance Topics Inheritance  The basics › Example: Stanford GObject class  Polymorphism.
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.
Adapted from slides by Marty Stepp and Stuart Reges
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
Homework 8: Critters reading: HW8 spec.
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);
HW11 Assignment Specification
Lecture 8-3: Encapsulation, this
Critter exercise: Snake
Adapted from slides by Marty Stepp and Stuart Reges
CSE 142 Critters (IPL) behavior: Ant Bird Hippo Vulture
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
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
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face many issues: programmers working together getting code finished on time avoiding redundant code finding and fixing bugs maintaining, reusing existing code code reuse: The practice of writing program code once and using it in many contexts.

Law firm employee analogy common rules: hours, vacation, benefits, regulations ... all employees attend a common orientation to learn general company rules each employee receives a 20-page manual of common rules each subdivision also has specific rules: employee receives a smaller (1-3 page) manual of these rules smaller manual adds some new rules and also changes some rules from the large manual

Is-a relationships, hierarchies is-a relationship: A hierarchical connection where one category can be treated as a specialized version of another. every marketer is an employee every legal secretary is a secretary inheritance hierarchy: A set of classes connected by is-a relationships that can share common code.

Inheritance inheritance: A way to form new classes based on existing classes, taking on their attributes/behavior. a way to group related classes a way to share code between two or more classes One class can extend another, absorbing its data/behavior. superclass: The parent class that is being extended. subclass: The child class that extends the superclass and inherits its behavior. Subclass gets a copy of every field and method from superclass

Inheritance syntax By extending Employee, each Secretary object now: public class name extends superclass { Example: public class Secretary extends Employee { ... } By extending Employee, each Secretary object now: receives a getHours, getSalary, getVacationDays, and getVacationForm method automatically can be treated as an Employee by client code (seen later)

Overriding methods override: To write a new version of a method in a subclass that replaces the superclass's version. No special syntax required to override a superclass method. Just write a new version of it in the subclass. public class Lawyer extends Employee { // overrides getVacationForm method in Employee class public String getVacationForm() { return "pink"; } ... Exercise: Complete the Lawyer class. (3 weeks vacation, pink vacation form, can sue)

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 + 5000.0; } ...

A Critter subclass public class name extends Critter { ... } public abstract class Critter { public boolean eat() public Attack fight(String opponent) // ROAR, POUNCE, SCRATCH public Color getColor() public Direction getMove() // NORTH, SOUTH, EAST, WEST, CENTER public String toString() }

How the simulator works "Go" → loop: move each animal (getMove) if they collide, fight if they find food, eat Simulator is in control! getMove is one move at a time (no loops) Keep state (fields) to remember future moves Next move? %

Critter exercise: Cougar Write a critter class Cougar: Method Behavior constructor public Cougar() eat Always eats. fight Always pounces. getColor Blue if the Cougar has never fought; red if he has. getMove Walks west until he finds food; then walks east until he finds food; then goes west and repeats. toString "C"