CSC Inheritance. Fall 2009

Similar documents
CHAPTER 10 INHERITANCE

Inheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13

Intro to Computer Science 2. Inheritance

Inheritance (P1 2006/2007)

Chapter Goals. Chapter 9 Inheritance. Inheritance Hierarchies. Inheritance Hierarchies. Set of classes can form an inheritance hierarchy

Software Design and Analysis for Engineers

C# Programming for Developers Course Labs Contents

Chapter 10 Inheritance. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Introduction to Inheritance

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

EXAM Computer Science 1 Part 1

Handout 9 OO Inheritance.

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Outline CSE 142. Specification vs Implementation - Review. CSE142 Wi03 F-1. Instance Variables

Chapter 10 Defining Classes

Implementing Classes

Agenda: Notes on Chapter 3. Create a class with constructors and methods.

Inheritance in Ruby. You are familiar with the idea of inheritance and how to use this in programming.

Inheritance: Definition

INHERITANCE AND EXTENDING CLASSES

CS1083 Week 3: Polymorphism

Object-Oriented Programming in C++/Handout 01 Page 1 of 8

Implementing Classes (P1 2006/2007)

Programming a Bank Database. We ll store the information in two tables: INTEGER DECIMAL(10, 2)

CSC 111, Test 1 (Fall 2009)

Inheritance and Subclasses

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Arrays Classes & Methods, Inheritance

CS112 Lecture: Defining Instantiable Classes

Java Object Oriented Design. CSC207 Fall 2014

Lesson 35..Inheritance

Inheritance. Transitivity

Encapsulation. Mason Vail Boise State University Computer Science

Where do we stand on inheritance?

Inheritance and Polymorphism. CS180 Fall 2007

Inheritance (Deitel chapter 9)

CPS122 Lecture: Defining a Class

CSC207 Week 3. Larry Zhang

Basic Object-Oriented Concepts. 5-Oct-17

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

POLYTECHNIC OF NAMIBIA SCHOOL OF COMPUTING AND INFORMATICS DEPARTMENT OFCOMPUTER SCIENCE COURSE NAME: OBJECT ORIENTED PROGRAMMING

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Chapter 14 Abstract Classes and Interfaces

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Sorting. Sorting. Selection sort

Everything is an object. Almost, but all objects are of type Object!

Imperative Languages!

CSE 143 Lecture 12 Inheritance

Inheritance -- Introduction

Inheritance. A mechanism for specialization A mechanism for reuse. Fundamental to supporting polymorphism

CSC 1214: Object-Oriented Programming

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

Inheritance & Polymorphism

Chapter 6 Introduction to Defining Classes

CS 170, Section /3/2009 CS170, Section 000, Fall

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Introduction to Computer Science II CS S-10 Inheritance

Rules and syntax for inheritance. The boring stuff

Principles of Software Construction: Objects, Design and Concurrency. Polymorphism, part 2. toad Fall 2012

More on Objects in JAVA TM

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

CSE 143. "Class Relationships" Class Relationships and Inheritance. A Different Relationship. Has-a vs. Is-a. Hierarchies of Organization

Structured Programming

COP 3330 Final Exam Review

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

CS 1302 Chapter 9 (Review) Object & Classes

ECE 122. Engineering Problem Solving with Java

Programming Abstractions

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

COSC This week. Will Learn

1 State, objects, and abstraction

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

C++ Important Questions with Answers

Inheritance, and Polymorphism.

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Check out Polymorphism from SVN. Object & Polymorphism

Assignment 2 - Specifications and Modeling

CSEN401 Computer Programming Lab. Topics: Introduction and Motivation Recap: Objects and Classes

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Final Exam CS 251, Intermediate Programming December 10, 2014

CSC 222: Object-Oriented Programming. Fall Inheritance & polymorphism

CS2102: Lecture on Abstract Classes and Inheritance. Kathi Fisler

Where do objects come from? Good question!

CS-202 Introduction to Object Oriented Programming

Transcription:

CSC 111 - Inheritance Fall 2009

Object Oriented Programming: Inheritance Within object oriented programming, Inheritance is defined as: a mechanism for extending classes by adding variables and methods (functions) Think about the following scenario: Imagine I have a BankAccount class that has as variables double balance, int numberoftransactions, and methods for construction, ti deposit, withdraw, and getbalance. I want a SavingsAccount class, which is essentially a BankAccount, but has an additional variable called interestrate, tr t and a new method called addinterest. t

BankAccount SavingsAccount balance numberoftransactions s construct($) withdraw($) deposit($) getbalance() balance numberoftransactions interestrate construct($) withdraw($) deposit($) getbalance() addinterest() Similar items marked with arrows New items/information circled

I don t want to change BankAccount.java, because it s pretty general and useful - not all accounts need an interest rate. I want to exploit code re-use Don t want to copy all the code from BankAccount.java into SavingsAccount.java, because I m somewhat lazy Any changes to BankAccount activities should affect SavingsAccounts, as SavingsAccounts are just a specialized type of BankAccount If the BankAccount code was just copied into SavingsAccount, then I would have to update both files to ensure that both types of accounts correctly represent the core of how accounts work.

If possible, I would like a simple way to indicate: A particular class (SavingsAccount) inherits all variables and methods from another class (BankAccount), and can add some of its own variables and methods Java allows us to do this but first let s look at more examples

Examples BankAccount BankAccount is the superclass and is general SavingsAccount SavingsAccount is the subclass, inheriting attributes and behaviors from BankAccount, and is more specific/specialized, as it adds it s owns attributes and behaviors

Examples Lion is a subclass of Mammal: A Lion fulfills Mammal properties Live birth Warm-blooded Hair Also has some lion-specific properties Ability to roar Tail Mammal Lion

Examples Can have multiple layers of inheritance: A Lion is a subclass of Cat, which is a subclass of Mammal A Cat fulfills Mammal properties Live birth Warm-blooded Hair A Lion fulfills Cat properties Tail CanPurr Whiskers A Lion has its own properties Large size CanRoar Mammal Cat Lion

Examples Automobile Attributes: year make model transmissiontype i passengercapacity Truck Truck-specific attributes: bedlength

Vehicle Plane Boat Automobile Another example of multi-layer l inheritance it Truck

In a main program, we can call any superclass method on a subclass object: We can call any BankAccount method on a SavingsAccount We can call any SavingsAccount method on a SavingsAccount We can t call a SavingsAccount method on a BankAccount

Superclass BankAccount object constructed handlewithdrawal called Subclass SavingsAccount object constructed handlewithdrawal called Both support getbalance Only SavingsAccount supports addinterest Using subclasses in main looks normal! Blissfully ll unaware are of inheritance it just works.

It is within our definition of a class (datatype), not main, where need to implement inheritance Use normal syntax for indicating new variables and methods New syntax for indicating whom inheriting from New syntax for calling parent s (superclass) methods and for overwriting parent s methods

Syntax: Indicate that your current class extends its superclass At top of file, instead of: public class ClassName use public class ClassName extends AnotherClassName BankAccount - superclass SavingsAccount - subclass

Syntax: Make variables in superclass visible to subclasses Remember, private says only the containing class can see variables protected allows subclasses to also interact with variables Subclasses need to declare only new variables, and they do that normally (private)

A constructor is required to be present for any newly developed subclass, even if it should just do the work of the superclass constructor. Make a call to the superclass constructor by using the syntax super() as the first line of subclass constructor.

New methods can be added to the subclass normally Note old methods don t have to be re- written They are brought in for free from the superclass (parent) class

What if want to overwrite parent s implementation? Just rewrite the method in the subclass In this example, I just added a print statement indicating that I was in the subclass SavingsAccount

What if still need/want to use parent s implementation of a method as part of your own implementation? Use super.method() syntax

Any examples for when would use syntax on last slide combining parents implementation and your own? A savings account that needs to: handle withdrawals by taking money from balance (implemented in superclass), but that also needs to reduce the interest rate when the balance drops below a certain amount (implemented in subclass) A checking account that charges a fee when deposits and withdrawal counts go over a set limit. The deposits and withdrawals are handled d normally, but extra work of charging the fee should be covered in the subclasses.

: Side-by-Side Superclass and Subclass

in BlueJ Large closed triangle arrow indicates inheritance Small open arrow indicates dependency SavingsAccount Inherits from BankAccount BankMain requires SavingsAccount and BankAccount

It is possible to convert variables of: A subclass into a superclass (easy to do) A superclass into a subclass (harder to do) Subclass into a superclass: All SavingsAccounts are BankAccounts, so a BankAccount variable can refer to a SavingsAccount with no problem. SavingsAccount collegefund = new SavingsAccount(10000,0.03); BankAccount anaccount = collegefund;

If a subclass is converted to a superclass and referenced through the superclass, all subclass extras disappear. SavingsAccount collegefund = new SavingsAccount(10000,0.03); BankAccount anaccount = collegefund; anaccount.handledeposit(10000); // legal anaccount.addinterest(); // illegal collegefund.addinterest(); // legal If the collegefund variable is being referenced as a BankAccount, trying to addinterest is illegal (BankAccount doesn t know how to do that).

Idea is similar, in some ways, to converting an int into a double: int myint = 5; double mydouble = myint; // integers are subset of doubles, so easy // to push ints into doubles (without cast) SavingsAccount collegefund = new SavingsAccount(10000,0.03); BankAccount anaccount = collegefund; // SavingsAccounts are subset of BankAccounts, so easy // to push SavingsAccounts into BankAccounts (without // cast) Now that using doubles, can t use integer specific % (remainder) method Now that t using BankAccount, can t use SavingsAccount specific addinterest method