Example of Aggregation 1) aggregation - the class contains other structure(s) 2) specialization - the new class is a special case of the data structure.

Slides:



Advertisements
Similar presentations
A software specification indicates the task (or some aspect of the task) that is supposed to be performed when software executes. Types of Specifications.
Advertisements

Some containers are called linear because their content (items) are stored in a single row (line). Which of the following are linear?  the rungs of a.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
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.
© 2006 Pearson Addison-Wesley. All rights reserved Inheritance When one class inherits another class, the new class becomes a specialized version.
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.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
Inheritance Chapter 14. What is inheritance?  The ability to create a class from another class, the “parent” class, extending the functionality and state.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Software reuse means to “borrow” existing code. How can you reuse an existing class to make a new one? Such reuse is really an adaptation -- using the.
What is inheritance? It is the ability to create a new class from an existing class.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Inheritance Like father like son Image from:
Two Parts of Every ADT An abstract data type (ADT)  is a type for encapsulating related data  is abstract in the sense that it hides distracting implementation.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Chapter 11 More about classes and OOP. Relationships Between Classes Possible relationships – Access ("uses-a") – Ownership/Composition ("has-a") – Inheritance.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
1 Chapter 5 - Object-Oriented Programming 1 What is inheritance? Object-oriented systems allow classes to be defined in terms of other classes. Classes.
Inheritance  Inheritance is a fundamental object-oriented technique  it enhances software design and promotes reuse  We will focus on:  deriving new.
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 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
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.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
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:
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
A software specification indicates the task (or some aspect of the task) that is supposed to be performed when software executes. Types of Specifications.
A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
Polymorphic Variables CMPS2143. The Polymorphic Variable A polymorphic variable is a variable that can reference more than one type of object (that is,
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
1 C++ Classes and Data Structures Course link…..
Modern Programming Tools And Techniques-I
Advanced Java Topics Chapter 9
Lecture 12 Inheritance.
03/10/14 Inheritance-2.
Computing with C# and the .NET Framework
An Introduction to Inheritance
CSC 205 Java Programming II
Inheritance Chapter 5.
Figure 8.1 Inheritance: Relationships among timepieces.
null, true, and false are also reserved.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Inherited Classes in Java
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Lecture 18: Polymorphism (Part II)
Chapter 9 Carrano Chapter 10 Small Java
Figure 8.1 Inheritance: Relationships among timepieces.
Inheritance and Polymorphism
Presentation transcript:

Example of Aggregation 1) aggregation - the class contains other structure(s) 2) specialization - the new class is a special case of the data structure public class PersonalInfo { private String firstName, lastName; private char midInitial; private Address street;... } PersonalInfo StringAddress 2 Two ways to use a data structure to implement a class The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Inheritance When one class inherits another class, the new class becomes a specialized version of the original. Java Syntax public class childClass extends parentClass {... } UML (class diagram) Notation parentClass childClass The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Example of Specialization Consider the relationships among classes for the following: TimePiece GrandfatherClock DigitalWatch Note that GrandfatherClock is a TimePiece DigitalWatch is a TimePiece TimePiece GrandfatherClockDigitalWatch The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Review of Inheritance How do you express inheritance in Java? parentClass childClass What is meant by overriding a method? (Note that variables can’t be overridden.) What does a child class inherit from its parent class? Can the child class declare additional variables & methods? Does the child class inherit constructor methods? What is the notation for calling a parent class constructor? Parent class constructor calls are only permitted in one location. Where? How does a child class refer to the parent class version of an overridden method? When is the following expression true? a instanceof b Are private members from a parent class accessible within a child class?... public members?... protected members? The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

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 protected public private is “-” The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Including a toString method (returns the balance as a String) The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

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() The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Maintain total amounts for deposits and withdrawls The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

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

Permit overdraft (negative balance) (charge $10 for each transaction in the red) The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

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

public class Director { private CheckbookWithRedInk checkbook; public Director() { 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 The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.