Building Java Programs



From this document you will learn the answers to the following questions:

What is the name of the class that represents employees?

How long do employees have paid vacation leave?

What class has the only unique behavior in the secretary?

Similar documents
SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

Chapter 13 - Inheritance

Advanced Data Structures

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Java Programming Language

Java CPD (I) Frans Coenen Department of Computer Science

Agile Software Development

Java (12 Weeks) Introduction to Java Programming Language

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Software Engineering Techniques

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

AP Computer Science A - Syllabus Overview of AP Computer Science A Computer Facilities

Implementation Aspects of OO-Languages

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

CS 111 Classes I 1. Software Organization View to this point:

AP Computer Science Java Subset

The first program: Little Crab

Java Interview Questions and Answers

Entity-Relationship Model. Purpose of E/R Model. Entity Sets

2 The first program: Little Crab

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Extending Classes with Services

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Object-Oriented Programming: Polymorphism

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Diamond Effect in Object Oriented Programming Languages

Konzepte objektorientierter Programmierung

Instance Creation. Chapter

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Lecture 15: Inheritance

Introduction to Programming Block Tutorial C/C++

Lecture 1 Introduction to Android

Inheritance, overloading and overriding

An Introduction To UML Class Diagrams Classes

Part I. Multiple Choice Questions (2 points each):

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Install Java Development Kit (JDK) 1.8

D06 PROGRAMMING with JAVA

CS193j, Stanford Handout #10 OOP 3

3 Improving the Crab more sophisticated programming

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Introduction to Object-Oriented Programming

Introduction to Java Applets (Deitel chapter 3)

Formal Engineering for Industrial Software Development

UML Class Diagrams. Lesson Objectives

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Creating a Custom Class in Xcode

The Rules 1. One level of indentation per method 2. Don t use the ELSE keyword 3. Wrap all primitives and Strings

Abstract Class & Java Interface

Fundamentals of Java Programming

1 Hour, Closed Notes, Browser open to Java API docs is OK

History OOP languages Year Language 1967 Simula Smalltalk

Compile-time type versus run-time type. Consider the parameter to this function:

ICOM 4015: Advanced Programming

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Checking Access to Protected Members in the Java Virtual Machine

Design Patterns in Java

a. Inheritance b. Abstraction 1. Explain the following OOPS concepts with an example

Abstract Classes. Suppose we want write a program that manipulates various types of bank accounts. An Account typically has following features;

DEVELOPING DATA PROVIDERS FOR NEEDFORTRADE STUDIO PLATFORM DATA PROVIDER TYPES

CSE 1020 Introduction to Computer Science I A sample nal exam

How To Design Software

The Interface Concept

Practical Programming Methodology. Michael Buro. Class Inheritance (CMPUT-201)

Programming Languages Featherweight Java David Walker

Lecture 5: Java Fundamentals III

Chapter 24 - Quality Management. Lecture 1. Chapter 24 Quality management

Generating Unit Tests for Checking Refactoring Safety

Habanero Extreme Scale Software Research Project

Tutorial on Writing Modular Programs in Scala

Building Java Programs

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Object Oriented Software Design

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

MCI-Java: A Modified Java Virtual Machine Approach to Multiple Code Inheritance

CS 106 Introduction to Computer Science I

Object Oriented Databases (OODBs) Relational and OO data models. Advantages and Disadvantages of OO as compared with relational

Contents. 9-1 Copyright (c) N. Afshartous

INHERITANCE, SUBTYPING, PROTOTYPES in Object-Oriented Languages. Orlin Grigorov McMaster University CAS 706, Fall 2006

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Quality Management. Objectives

Quality Management. Ian Sommerville 2004 Software Engineering, 7th edition. Chapter 27 Slide 1

CS-XXX: Graduate Programming Languages. Lecture 25 Multiple Inheritance and Interfaces. Dan Grossman 2012

RTAP Database Lifecycle Management

11 November

Java Classes. GEEN163 Introduction to Computer Programming

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

Transcription:

Building Java Programs Chapter 9 Lecture 9-1: Inheritance reading: 9.1

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

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 3

Separating behavior Why not just have a 22 page Lawyer manual, a 21-page Secretary manual, a 23-page Marketer manual, etc.? Some advantages of the separate manuals: maintenance: Only one update if a common rule changes. locality: Quick discovery of all rules specific to lawyers. Some key ideas from this example: General rules are useful (the 20-page manual). Specific rules that may override general ones are also useful. 4

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. 5

Employee regulations Consider the following employee regulations: Employees work 40 hours / week. Employees make $40,000 per year, except legal secretaries who make $5,000 extra per year ($45,000 total), and marketers who make $10,000 extra per year ($50,000 total). Employees have 2 weeks of paid vacation leave per year, except lawyers who get an extra week (a total of 3). Employees should use a yellow form to apply for leave, except for lawyers who use a pink form. Each type of employee has some unique behavior: Lawyers know how to sue. Marketers know how to advertise. Secretaries know how to take dictation. Legal secretaries know how to prepare legal documents. 6

An Employee class // 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 40000.0; // $40,000.00 / year public int getvacationdays() { return 10; // 2 weeks' paid vacation public String getvacationform() { return "yellow"; // use the yellow form Exercise: Implement class Secretary, based on the previous employee regulations. (Secretaries can take dictation.) 7

Redundant Secretary class // A redundant class to represent secretaries. public class Secretary { public int gethours() { return 40; // works 40 hours / week public double getsalary() { return 40000.0; // $40,000.00 / 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); 8

Desire for code-sharing takedictation is the only unique behavior in Secretary. We'd like to be able to say: // A class to represent secretaries. public class Secretary { copy all the contents from the Employee class; public void takedictation(string text) { System.out.println("Taking dictation of text: " + text); 9

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 10

Inheritance syntax 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) 11

Improved Secretary code // A class to represent secretaries. public class Secretary extends Employee { public void takedictation(string text) { System.out.println("Taking dictation of text: " + text); Now we only write the parts unique to each type. Secretary inherits gethours, getsalary, getvacationdays, and getvacationform methods from Employee. Secretary adds the takedictation method. 12

Implementing Lawyer Consider the following lawyer regulations: Lawyers who get an extra week of paid vacation (a total of 3). Lawyers use a pink form when applying for vacation leave. Lawyers have some unique behavior: they know how to sue. Problem: We want lawyers to inherit most behavior from employee, but we want to replace parts with new behavior. 13

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

Lawyer class // 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 getvacationdays() { return 15; // 3 weeks vacation public void sue() { System.out.println("I'll see you in court!"); Exercise: Complete the Marketer class. Marketers make $10,000 extra ($50,000 total) and know how to advertise. 15

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 50000.0; // $50,000.00 / year 16

Levels of inheritance Multiple levels of inheritance in a hierarchy are allowed. Example: A legal secretary is the same as a regular secretary but makes more money ($45,000) and can file legal briefs. public class LegalSecretary extends Secretary {... Exercise: Complete the LegalSecretary class. 17

LegalSecretary class // 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 45000.0; // $45,000.00 / year 18