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

Slides:



Advertisements
Similar presentations
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Advertisements

CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
CS 106 Introduction to Computer Science I 11 / 13 / 2006 Instructor: Michael Eckmann.
ITEC200 – Week03 Inheritance and Class Hierarchies.
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,
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
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 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
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.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
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 and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Advanced Java Programming
Overriding Methods & Class Hierarchies
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Presentation transcript:

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

Michael Eckmann - Skidmore College - CS Fall 2007 Today’s Topics Comments and/or Questions? Last lab's PokerGame class Inheritance revisited Object super class hierarchies overriding methods

Programming assignments Let's use tomorrow's lab as a time for me to meet with each of you individually regarding your program 1. I'll have a lab for you to work on, and one at a time each of you will meet with me for about 10 minutes each in my office where I will discuss your program 1. Also, you can have an extra day for program 2 which is now due by Wednesday 11:59pm.

Let's solve last lab's Problem 2 By way of review, let's start by writing PokerGame and then write all that is required by PokerHand.

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?

Overriding methods A subclass can override a superclass's method by providing a definition for a method that exists in the superclass with the same name and number and type of parameters. Let's add a method to Account and override it in SavingsAccount, but not in CheckingAccount. Then let's call the method with an object of SavingsAccount. And let's call the method with an object of CheckingAccount.

Overriding methods public String toString() What do you think about the toString() method? Can that be overridden by Account? Can it be overridden by a subclass of Account, like SavingsAccount?

equals method In class Object, the header for the equals method is this: public boolean equals(Object obj) If we want to override this method, we must make sure our equals method has the SAME signature. Does anyone recall what a signature contains?

Good design principle It should be apparent that in a case like the one we're doing now, Bank Accounts, we don't ever want the data to be allowed to be invalid. Care must be taken to require that a class's variables cannot have invalid data at any time. e.g. We wouldn't want to allow ATM_Withdrawal_Per_Day_Limit to ever be negative. It doesn't make sense. Also, Account #'s typically have a valid range of possibilities. We certainly wouldn't want account_number to be public and therefore able to be changed to an invalid value. We make the appropriate instance variables private for just that reason. And then only allow them to be changed in a controlled way via methods with some testing in them. We'll put some testing in the code to handle this.

Protected vs. Private vs. Public subclasses have access to public and protected members of their superclass a class has access to all of its own members (whether they are private, protected or public) objects of a class have access only to the public members of the class (and the public members of the parent class(es)). Classes within the same package though are allowed to always access protected members --- but I don't recommend this. What does this mean for our Account program? What about inside the CheckingAccount class, what members of Account can we refer to? What about an object of type CheckingAccount --- does it have access to any of the members in Account?

Overloaded methods Overloaded methods are those that have the same name but different numbers or types of parameters. It has nothing to do with the super / subclass (parent / child) relationships that we've been talking about. Does anyone remember when we used Overloaded methods?

Account Program Let's continue to implement Account, CheckingAccount, SavingsAccount – toString() – equals(Object o) – Add set methods, get methods Deposit, withdraw methods (instead of setBalance) – Create objects

Abstract classes – Can never be instantiated – Can contain both abstract methods and actual (non-abstract) methods – Can contain instance variables as well as constants If a class contains any abstract methods then it MUST BE an abstract class But an abstract class is not required to have abstract methods Abstract classes are different from interfaces which we will see next.

More on overriding methods Suppose we have a class Pet, Dog, and Cat. We provide a speak() method in Pet but with no body. Then, any class that inherits from Pet, must implement this method. Further, let's assume we have a class TalkingDog which doesn't bark when he speaks, instead he speaks English. To implement this stuff...

Class Pet, Dog and Cat Let's suppose all Pets have names, breeds and make sounds, and know how to sleep. Let's also suppose that different Dogs have different skills, but different Cats do not. Data to be stored in Pet is: – Name – Type (cat, dog, etc.) – Breed Additional data to be stored in Dog is: – Skill No additional data is stored in Cat. Let's set up the classes with these data and the relationships among the classes.

More on overriding methods So we have classes Pet (abstract), Dog, and Cat. We can provide a speak() method in Pet with no body (and we make it abstract). Then, any class that inherits from Pet, must implement this method. We can provide an actual method sleeps() in Pet that is not abstract. And the class TalkingDog which doesn't bark when he speaks, instead he speaks English. So, Dog and Cat inherit from Pet. TalkingDog inherits from Dog. Let's implement this stuff (instantiate objects of Dog, Cat, and TalkingDog) Note: because Pet contains an abstract method, the class is not instantiable --- the class itself must be declared as abstract.