Object Oriented Design CSC 171 FALL 2001 LECTURE 12.

Slides:



Advertisements
Similar presentations
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Advertisements

Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
1 CSC 222: Computer Programming II Spring 2005 Inheritance  derived class, parent class  inheriting fields & methods, overriding fields and methods 
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Object-Oriented Programming in C++ Lecture 6 Inheritance.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
CHAPTER 11 INHERITANCE CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CS 106 Introduction to Computer Science I 11 / 13 / 2006 Instructor: Michael Eckmann.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
Chapter 10  Inheritance 1 Chapter 10 Inheritance.
Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance Motivation –Code reuse –Conceptual modeling.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
ITM 352 Class inheritance, hierarchies Lecture #.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Inheritance.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Object-Oriented Programming in C++
Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Tutorial 10 COMPSCI 230 Software Design & Construction Muhammad Sulyaman.
Java Programming Week 4: Inheritance (Chapter 10).
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Topic 4 Inheritance.
John Byrne Inheritance Bank account examples. John Byrne Example account hierarchy F account relationships: ACCOUNT SAVINGSCHEQUE TIME_ACCT.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
CSC 205 Java Programming II Inheritance Inheritance In the real world, objects aren’t usually one-of-a-kind. Both cars and trucks are examples of.
Inheritance and Access Control CS 162 (Summer 2009)
Chapter 10 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.
Chapter 13 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Chapter 13 - Inheritance.
Data Structures and Algorithms revision
CSC 222: Object-Oriented Programming Fall 2015
CSC 222: Object-Oriented Programming Spring 2017
Lecture Notes – Inheritance (Ch 9-10)
Inheritance in Java.
Implementing Classes Yonglei Tao.
CSC 222: Object-Oriented Programming Fall 2017
Phil Tayco Slide version 1.1 Created Oct 30, 2017
Computing with C# and the .NET Framework
Packages, Interfaces & Exception Handling
CSC 205 Java Programming II
Chapter Three - Implementing Classes
Chapter 10 – Inheritance Big Java by Cay Horstmann
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Adapted from Java Concepts Companion Slides
Presentation transcript:

Object Oriented Design CSC 171 FALL 2001 LECTURE 12

History: Harvard Mark I The first large scale, automatic, general purpose, electromechanical calculator (aka IBM Automatic Sequence Control Calculator [ASCC]) Intended to compute navigation tables -- the same purpose as intended by Babbage for the Difference Engine

History: The first bug Grace Murray Hopper, working in a temporary World War I building at Harvard University on the Mark II computer, found the first computer bug beaten to death in the jaws of a relay.

Design Methodology 1. Problem Definition 2. Requirements Analysis 3. Architecture 4. Construction 5. Testing 6. Future Improvements

CRC cards Classes, Responsibilities, Collaborators

Hierarchies Humans have found that organizing concepts into hierarchies a useful method of organizing information

Inheritance Hierarchies Object oriented languages, such as JAVA allows us to group classes into inheritance hierarchies. The most general classes are near the root The more specific classes are near the leaves

Example: Banking Systems Consider a system that supports Savings and Checking accounts – What are the similarities? – What are the specifics

Accounts Both savings and Checking accounts support the idea of – Balance – Deposit – Withdraw Savings accounts pay interest checking accounts do not Checking accounts have transaction fees, savings accounts do not

Super & Sub classes More general concepts are in super classes More specific concepts are in sub classes Sub classes extend (inherit from) superclasses When we find, though CRC analysis, that two classes share many responsibilities, we think about making a super-class

Why inheritance? The power of inheritance is that sub-classes inherit the capabilities of the super-classes they extend This reduces code redundancy

Example: Banking systems Bank accounts are a type of object Savings accounts are a type of bank account Checking Accounts are bank accounts public class BankAccount {... } public class SavingsAccount extends BankAccount {...} public class CheckingAccount extends BankAccount {...}

Bank Accounts public class BankAccount { private double balance; public double getBalance() {... } public void deposit(double d) {...} public void withdraw(double d){... } }

Savings account Savings accounts are like Bank accounts but they support the notion of interest

Savings Accounts

public class SavingsAccount extends BankAccount { private double interestRate; public void addInterest() { double interest = getBalance() * interestRate/100; balance += interest; // will this work? } public SavingsAccount(double rate){ interestRate = rate; }

Savings Accounts public class SavingsAccount extends BankAccount { private double interestRate; public void addInterest() { double interest = getBalance() * interestRate/100; deposit(interest); // where is this defined? } public SavingsAccount(double rate){ interestRate = rate; }

Checking accounts For checking accounts, we need to keep track of transactions So, we need to add the ability to deduct fees However, we also need to change deposit

Checking Accounts public class CheckingAccount extends BankAccount { private int transactionCount; // how about this? public void deposit (double amount){ transactionCount++; deposit(amount); } ….

Checking Accounts public class CheckingAccount extends BankAccount { private int transactionCount; // this works better public void deposit (double amount){ transactionCount++; super.deposit(amount); } ….

Checking Accounts // and of course public void withdraw (double amount){ transactionCount++; super.withdraw(amount); } ….

Checking Accounts // and, in addition public void deductFees (){ if (transactionCount > FREE_TRANS) { double fees = TRANS_FEE * (transactionCount – FREE_TRANS); super.withdraw(fees); } transactionCount = 0 ; } private static final int FREE_TRANS = 3; private static final double TRANS_FEE = 2.0 ; }

Polymorphism Inheritance is thought of as an “is-a” relationship – A CheckingAccount “is-a” BankAccount – A BankAccount is not a CheckingAccount Due to this fact, it is possible to use a subclass object in place of a superclass object

Polymorphism Example Consider the problem of “transfer” – We want to be able to transfer Savings -> savings Savings -> checking Checking -> savings Checking -> checking In short, we want to transfer from one BankAccount, to another

Transfer A method of BankAccount public void transfer(BankAccount other, double amount) { withdraw(amount); other.deposit(amount); }

Set up some accounts CheckingAccount harrysChecking = new CheckingAccount(); harrysChecking.deposit(1000); SavingsAccount harrysSavings = new SavingsAccount(); harrysSavings.deposit(5000);

Using Transfer harrysSavings.transfer(harrysChecking,500); Does this work? transfer is defined on BankAccounts transfer takes a BankAccount as a parameter

Polymorphism & Arrays BankAccount myBank = new BankAccount[accnts]; myBank[0]= new CheckingAccount(); myBank[0].deposit(1000); myBank[1]= new SavingsAccount(); myBank[1].deposit(1000);

instanceOf for (int i = 0 ; i<myBank.length;i++){ if (myBank[i] instanceOf CheckingAccount) myBank[i].deductFees(); if (myBank[i] instanceOf SavingsAccount) myBank[i].addInterest(); }

Interfaces Suppose we wanted to be able to sort Savings accounts based on interest rate We could write our own sort method, but there are lots of good sorts in the JAVA lib If objects are of the Comparable type, many methods in the JAVA lib could sort them

One Approach public class SavingsAccount extends BankAccount, Comparable {....} This does not work Multiple inheritance ins not support in JAVA

Instead – use an interface public interface Comparable { int compareTo(Object other); // no impementation } public class SavingsAccount extends BankAccount implements Comparable { public int compareTo(Object other) { // we supply implementation }.. }

Implement an interface public int compareTo(Object other) { // sub is super, not super sub – So, make it so SavingsAccount otherAccount = (SavingsAccount) other; if (interestRate < otherAccount.interestRate) return 1; if (interestRate > otherAccount.interestRate) return -1; }

Other interfaces Listeners of most kinds