CS 106 Introduction to Computer Science I 11 / 28 / 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.
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
CS 106 Introduction to Computer Science I 11 / 13 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 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 03 / 26 / 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.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 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 / 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.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
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.
Inheritance in the Java programming language J. W. Rider.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Advanced Programming in Java
Lecture 12 Inheritance.
CSC 205 Java Programming II
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.
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Inheritance and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

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

Michael Eckmann - Skidmore College - CS Fall 2007 Today’s Topics Comments and/or Questions? overriding methods

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?

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 in Account, CheckingAccount, SavingsAccount – the equals(Object o) method

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.