CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Advertisements

Chapter 1 Inheritance University Of Ha’il.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
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.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
CS 106 Introduction to Computer Science I 11 / 13 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 26 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 16 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
Computer Science I Inheritance Professor Evan Korth New York University.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila 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.
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.
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.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
What is inheritance? It is the ability to create a new class from an existing class.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Topic 4 Inheritance.
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)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
AP Computer Science A – Healdsburg High School 1 Inheritance - What is inheritance? - “Is-a” vs. “Has-a” relationship - Programming example “CircleBug”
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.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Data Structures and Algorithms revision
Lecture 12 Inheritance.
Lecture Notes – Inheritance (Ch 9-10)
Computing with C# and the .NET Framework
Subclasses Chapter 11 Copyright © 2000 W. W. Norton & Company.
CSC 205 Java Programming II
Chapter 10 Defining Classes. Chapter 10 Defining Classes.
Inheritance Inheritance is a fundamental Object Oriented concept
Presentation transcript:

CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2007 Today’s Topics Comments and/or Questions? Is-a and Has-a relationships. Account exercise using Inheritance

Is-a vs. Has-a A “has a” relationship is one that corresponds to a class and its data members. For example: – A Card has a suit and has a number – A Deck has 52 Cards – A Car has a color and has a make and has a model,... – An Employee has a hire date and has a salary,... – Can we think of any more?

Is-a vs. Has-a A “has a” relationship is one that corresponds to a class and its data members. public class Card { private int suit; private int number; //... } public class Car { private String color; private String make; //... }

Is-a vs. Has-a A “has a” relationship is one that corresponds to a class and its data members. public class Date { private int year, month, day; //... } public class Employee { private Date hire_date; private double salary; //... }

Is-a vs. Has-a An “is a” relationship is one that corresponds to a class and the class from which it inherits. Also can be said as “is a type of”. For example: – A Faculty member is an Employee – A Staff member is an Employee – A Car is a Vehicle – A Truck is a Vehicle – A Motorcycle is a Vehicle – Can we think of any more?

Other inheritance relationships Can you think of anything else that has a “has a” relationship? – Rooms have doors, windows, etc. Can you think of anything else that has an “is a” relationship? – Bedroom is a Room – Dining room is a Room – How about Animals?

Bank account Exercise What data members describe a bank account? – That is, what does a bank account have. What types are these? We want to handle Checking and Savings Accounts

Bank account Maybe we might want the following data members: – Account number – Balance – Overdrawn Fee – ATM Withdrawal Per Day Limit – Bounced Check Fee – Interest Rate

Bank account So we could have a class named BankAccount that contains the following data: String Account_number; double Balance; double Overdrawn_Fee; int ATM_Withdrawal_Per_Day_Limit; double Bounced_Check_Fee; double Interest_Rate;

Bank account What methods might we need?

Bank account Notice that the data here isn't for all accounts: – Account number (for all accounts) – Balance (for all accounts) – Overdrawn Fee (for all accounts) – ATM Withdrawal Per Day Limit (for all accounts) – Bounced Check Fee (for checking accounts only) – Interest Rate (for savings accounts only)

Bank account We could create an Account class that contained the common stuff among all accounts We then could create a SavingsAccount class that inherits all the stuff about an Account from Account class and adds the things that are specific to Savings Accounts. We also could create a CheckingAccount class that inherits all the stuff about an Account from Account class and adds the things that are specific to Checking Accounts. These ideas are Inheritance ideas

Inheritance If class A inherits from class B, then class A is a subclass of B and class B is a superclass of A. We also talk of class A as being class B's child and class B being class A's parent. We can draw the inheritance relationships hierarchy by having superclass(es) at the top and subclass(es) lower, using lines to connect where there's an inheritance relationship.

Inheritance Class A may inherit from class B which can in turn, inherit from class C. (draw on the board.) However, this is not considered multiple inheritance. Multiple inheritance is an object-oriented concept that allows a subclass to inherit from more than one class --- this is NOT allowed in Java. e.g. Class D cannot inherit from both class E and F directly. (draw on the board.)

Inheritance A subclass inherits all the instance variables in its superclass and has access to (can call) any public methods. Even if there is private instance data in a superclass, the subclass inherits that data but can't refer to the variables. e.g. a SavingsAccount will inherit accountNumber from Account, so an object of type SavingsAccount will have an accountNumber.

Object is the superclass of all All classes inherit from Java's Object class. All classes that do not use the extends keyword directly inherit from Java's Object class. What does that mean for us? Let's visit the Java API for the Object class. – equals() – toString()

super is a reference to the parent class super(); // is a call to the parent's default (no parameter) constructor A call to a parent constructor should be the first thing done in any constructor of a child. If you don't explicitly call it, Java will automatically call the parent's default constructor if it exists.

Hierarchy of the account classes SavingsAccount and CheckingAccount each inherit from Account What about AccountTester? How about the Object class?