© 2006 Pearson Addison-Wesley. All rights reserved 8.1.1 Inheritance When one class inherits another class, the new class becomes a specialized version.

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
UML Class Diagram: class Rectangle
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance using Java
Writing Classes (Chapter 4)
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming Fundamentals 2: Inheritance/ F II Objectives – –the use of super, overriding, method polymorphism (dynamic binding), protected.
© 2006 Pearson Addison-Wesley. All rights reserved Inheritance Systems Merchandise ElectronicsClothing TelevisionCamcorderShirtDressShoe DigitalAnalog.
Inheritance Like father like son Image from:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Lecture 8.3 The Use of JComponent. © 2006 Pearson Addison-Wesley. All rights reserved More About the Standard Drawing Classes java.awt.Container.
Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes.
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
Topic 4 Inheritance.
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
Chapter 13 ATM Case Study Part 2: Implementing an Object-Oriented Design Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
© 2006 Pearson Addison-Wesley. All rights reserved char and String char is for storing single characters primitive type constants: a printable character.
Lecture 9.4 Java Interfaces. © 2006 Pearson Addison-Wesley. All rights reserved Java does not support multiple inheritance. Interface Characteristics...
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Problem 1 Bank.  Manage customers’ bank account using the following operations: Create a new account given a customer’s name and initial account. Deposit.
Example of Aggregation 1) aggregation - the class contains other structure(s) 2) specialization - the new class is a special case of the data structure.
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:
 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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1.
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Advanced Java Topics Chapter 9
Inheritance ITI1121 Nour El Kadri.
public class Doubler extends Base {
An Introduction to Inheritance
UML Class Diagram: class Rectangle
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CSC 205 Java Programming II
The Basics of Class Diagrams for a single class
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Inheritance, Polymorphism, and Interfaces. Oh My
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Example with Static Variable
Class Hierarchies and Type Conformance
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
An Example of Inheritance
Chapter 11 Inheritance and Polymorphism Part 1
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved Inheritance When one class inherits another class, the new class becomes a specialized version of the original. Java Syntax public class subclass extends superclass {... } public class subclass extends superclass {... } UML (class diagram) Notation

© 2006 Pearson Addison-Wesley. All rights reserved public class RedDot extends Oval { public RedDot( int x, int y, int d ) { super(x, y, d, d); setBackground( Color. red ); } public class RedDot extends Oval { public RedDot( int x, int y, int d ) { super(x, y, d, d); setBackground( Color. red ); } RedDot dotty; dotty = new RedDot(10, 20, 5); theWindow.add(dotty, 0); dotty.repaint(); // methods such as setSize and setLocation // are also available on dotty. Client code

© 2006 Pearson Addison-Wesley. All rights reserved public class BasicCheckbook { protected int balance; // in cents /** post balance == bd*100 + bc */ public BasicCheckbook(int bd, int bc) { balance = bd*100 + bc; } /** post balance == + dd*100 + dc */ public void deposit(int dd, int dc) { balance = balance + dd*100 + dc; } /** post balance == - (wd*100 + wc) */ public void withdraw(int wd, int wc) { balance = balance - (wd*100 + wc); } /* post result == balance */ public int balanceInCents() { return balance; } public class BasicCheckbook { protected int balance; // in cents /** post balance == bd*100 + bc */ public BasicCheckbook(int bd, int bc) { balance = bd*100 + bc; } /** post balance == + dd*100 + dc */ public void deposit(int dd, int dc) { balance = balance + dd*100 + dc; } /** post balance == - (wd*100 + wc) */ public void withdraw(int wd, int wc) { balance = balance - (wd*100 + wc); } /* post result == balance */ public int balanceInCents() { return balance; } BasicCheckbook # balance : int «constructor» + BasicCheckbook( int, int) «update» + deposit( int, int) + withdraw( int, int) «query» + balanceInCents() : int

© 2006 Pearson Addison-Wesley. All rights reserved public class CheckbookWithStrBalance extends BasicCheckbook { public CheckbookWithStrBalance(int bd, int bc) { super(bd, bc); } public String toString() { String dollarStr, centStr; int cents; dollarStr = "" + (balance / 100); cents = balance % 100; if (cents < 10) { centStr = "0" + cents; } else { centStr = "" + cents; } return "$" + dollarStr + "." + centStr; } public class CheckbookWithStrBalance extends BasicCheckbook { public CheckbookWithStrBalance(int bd, int bc) { super(bd, bc); } public String toString() { String dollarStr, centStr; int cents; dollarStr = "" + (balance / 100); cents = balance % 100; if (cents < 10) { centStr = "0" + cents; } else { centStr = "" + cents; } return "$" + dollarStr + "." + centStr; } BasicCheckbook # balance : int «constructor» + BasicCheckbook( int, int) «update» + deposit( int, int) + withdraw( int, int) «query» + int balanceInCents() CheckbookWithStrBalance «constructor» + CheckbookWithStr...( int, int) «query» + String toString()

© 2006 Pearson Addison-Wesley. All rights reserved public class CheckbookWithTotals extends CheckbookWithStrBalance { protected int depositTot, withdrawTot; public CheckbookWithTotals(int bd, int bc) { super(bd, bc); depositTot = 0; withdrawTot = 0; } public void deposit(int dd, int dc) { super.deposit(dd, dc); depositTot = depositTot + dd*100 + dc; } public void withdraw(int wd, int wc) { super.withdraw(wd, wc); withdrawTot = withdrawTot - (wd*100 + wc); } public int deposits() { return depositTot; } public int withdraws() { return withdrawTot; } public class CheckbookWithTotals extends CheckbookWithStrBalance { protected int depositTot, withdrawTot; public CheckbookWithTotals(int bd, int bc) { super(bd, bc); depositTot = 0; withdrawTot = 0; } public void deposit(int dd, int dc) { super.deposit(dd, dc); depositTot = depositTot + dd*100 + dc; } public void withdraw(int wd, int wc) { super.withdraw(wd, wc); withdrawTot = withdrawTot - (wd*100 + wc); } public int deposits() { return depositTot; } public int withdraws() { return withdrawTot; } BasicCheckbook # int balance «constructor» + BasicCheckbook( int, int) «update» + deposit( int, int) + withdraw( int, int) «query» + int balanceInCents() CheckbookWithStrBalance «constructor» + CheckbookWith...( int, int) «query» + String toString() CheckbookWithTotals # int depositTot # int withdrawTot «constructor» + CheckbookWithTotals( int, int) «update» + deposit( int, int) + withdraw( int, int) «query» + int deposits() + int withdraws()

© 2006 Pearson Addison-Wesley. All rights reserved public class CheckbookWithRedInk extends CheckbookWithTotals { public CheckbookWithRedInk(int bd, int bc) { super(bd, bc); } public void deposit(int dd, int dc) { super.deposit(dd, dc); if (dd*100+dc < 0 && balance < 0) { System.out.println("$10 surcharge"); balance = balance ; } public void withdraw(int wd, int wc) { super.withdraw(wd, wc); if (wd*100+wc > 0 && balance < 0) { System.out.println("$10 surcharge"); balance = balance ; } public String toString() { String str; if (balance >= 0) { str = super.toString(); } else { balance = -balance; str = "(" + super.toString() + ")"; balance = -balance; } return str; } public class CheckbookWithRedInk extends CheckbookWithTotals { public CheckbookWithRedInk(int bd, int bc) { super(bd, bc); } public void deposit(int dd, int dc) { super.deposit(dd, dc); if (dd*100+dc < 0 && balance < 0) { System.out.println("$10 surcharge"); balance = balance ; } public void withdraw(int wd, int wc) { super.withdraw(wd, wc); if (wd*100+wc > 0 && balance < 0) { System.out.println("$10 surcharge"); balance = balance ; } public String toString() { String str; if (balance >= 0) { str = super.toString(); } else { balance = -balance; str = "(" + super.toString() + ")"; balance = -balance; } return str; } BasicCheckbook # int balance «constructor» + BasicCheckbook( int, int) «update» + deposit( int, int) + withdraw( int, int) «query» + int balanceInCents() CheckbookWithStrBalance «constructor» + CheckbookWith...( int, int) «query» + String toString() CheckbookWithTotals # int depositTot # int withdrawTot «constructor» + CheckbookWithTotals( int, int) «update» + deposit( int, int) + withdraw( int, int) «query» + int deposits() + int withdraws()

© 2006 Pearson Addison-Wesley. All rights reserved public class Driver{ private CheckbookWithRedInk checkbook; public Driver() { checkbook = new CheckbookWithRedInk( 100, 0 ); checkbook.deposit( 20, 0 ); checkbook.withdraw( 125, 99 ); System.out.println("Final Balance: " + checkbook.toString()); } public class Driver{ private CheckbookWithRedInk checkbook; public Driver() { checkbook = new CheckbookWithRedInk( 100, 0 ); checkbook.deposit( 20, 0 ); checkbook.withdraw( 125, 99 ); System.out.println("Final Balance: " + checkbook.toString()); } CheckbookWithRedInk # int balance # int depositTot # int withdrawTot «constructor» + CheckbookWithRedInk( int, int) «update» + deposit( int, int) + withdraw( int, int) «query» + int balanceInCents() + String toString() + int deposits() + int withdraws() A flattened version of the class A client