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

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

IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Computer Science A 9: 3/11. Inheritance Today: Inheritance (JC – CCJ ) I have to leave at 11am (but you can stay)
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
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.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Object Oriented Design CSC 171 FALL 2001 LECTURE 12.
Chapter 10  Inheritance 1 Chapter 10 Inheritance.
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.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 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.
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.
Inheritance Abstract Classes Check out Inheritance from SVN.
ITM 352 Class inheritance, hierarchies Lecture #.
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.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Tutorial 10 COMPSCI 230 Software Design & Construction Muhammad Sulyaman.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Topic 4 Inheritance.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Some Important topics. Working with Files Working with a file involves three steps: – Open the file – Perform operations on the file – Close the file.
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.
1 TCSS 143, Autumn 2004 Lecture Notes Inheritance.
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
Problem 1 Bank.  Manage customers’ bank account using the following operations: Create a new account given a customer’s name and initial account. Deposit.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 13 - Inheritance.
Data Structures and Algorithms revision
CSC 222: Object-Oriented Programming Spring 2017
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
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
Subclasses Chapter 11 Copyright © 2000 W. W. Norton & Company.
CSC 205 Java Programming II
Chapter 10 – Inheritance Big Java by Cay Horstmann
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Agenda About Homework for BPJ lesson 36 About practice of last class
CSC 205 – Java Programming II
Adapted from Java Concepts Companion Slides
Presentation transcript:

 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 existing class, changing just what we need  The new class inherits from the existing one: ◦ all methods ◦ all fields  Can add new fields/methods  Or override existing methods Object BankAccount SavingsAccountCheckingAccount

public class BankAccount { private double balance; public BankAccount() { this(0.00); } public BankAccount(double initialBalance) { this.balance = initialBalance; } public void deposit(double amount) { this.balance += amount; } public void withdraw(double amount) { this.balance -= amount; } protected final double getBalance() { return this.balance; } Subclasses will inherit this field even though they cannot directly access it Subclasses inherit all fields Calls the one-parameter constructor protected means that subclasses and classes in the same package can access it. public makes more sense here, but I have made it protected just so that you can see an example final means that subclasses are not permitted to override this method We want to count on it working just like this

public class SavingsAccount extends BankAccount { private double interestRate; public SavingsAccount(double rate) { this.interestRate = rate; } public SavingsAccount(double rate, double initBalance) { super(initBalance); this.interestRate = rate; } public void addInterest() { double interest; interest = this.getBalance() * this.interestRate / 100; this.deposit(interest); } Fields: Inherits balance field DON’T put your own balance field here! Adds interestRate field Implicit super(); that calls superclass’ no-parameter constructor Calls superclass’ constructor Must be first statement in constructor Calls inherited getBalance and deposit methods Adds this method to those inherited

public class CheckingAccount extends BankAccount { private int transactionCount; public CheckingAccount(double initialBalance) { super(initialBalance); this.transactionCount = 0; public void withdraw() { super.withdraw(); ++ this.transactionCount; } public void runThisOnFirstDayOfMonth) { if (this.transactionCount > 100) { super.withdraw(10.00); } this.transactionCount = 0; } Overrides inherited withdraw method and also calls inherited withdraw method The class would have, but I have not shown, a similar deposit method. This (rather silly) checking account charges a $10 fee if you do more than 100 transactions in a month Note call to superclass’ withdraw

 class ClickHandler implements MouseListener ◦ ClickHandler promises to implement all the methods of MouseListener  class CheckingAccount extends BankAccount ◦ CheckingAccount inherits all the fields and methods of BankAccount For client code reuse For implementation code reuse

◦ Consider: public void moveTo(Point2D pointToMoveTo) ◦ Point2D is an interface that includes the methods getX() and getY() ◦ Point2D has implementations that include Point2D.Double and Point and Point2D.Float  Your code does not care which implementation it is; it works with any of them.  Your code needs only to know that you can get the X and Y components of pointToMoveTo by using the methods promised by the Point2D interface, e.g. pointToMoveTo.getX()

In superclass: protected Point2D.Double location;... this.location = new Point2D.Double(...,...); In subclass:... this.location.x = this.location.x +...; In superclass: private Point2D location;... this.location = new Point2D.Double(...,...); protected final Point2D getLocation() { return this.location; } protected final void setLocation(Point2D location) { this.location = location; } In subclass: this.setLocation(new Point2D.Double( this.getLocation().getX() +..., this.getLocation().getY() +...); Bad: locks superclass into using a Point2D.Double Good: allows superclass to change the implementation of the Ball’s location. Eclipse types most of this code for you!