© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.

Slides:



Advertisements
Similar presentations
OOP: Inheritance By: Lamiaa Said.
Advertisements

Inheritance Java permits you to use your user defined classes to create programs using inheritance.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Two 2x2 (block) matrices can be multiplied C==AB
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Inheritance in the Java programming language J. W. Rider.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
© 2011 Pearson Addison-Wesley. All rights reserved 9 B-1 Chapter 9 (continued) Advanced Java Topics.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Inheritance and Access Control CS 162 (Summer 2009)
Object-Oriented Programming Chapter Chapter
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Advanced C++ Topics Chapter 8. CS 308 2Chapter 8 -- Advanced C++ Topics This chapter describes techniques that make collections of reusable software components.
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
ISBN Object-Oriented Programming Chapter Chapter
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
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.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Modern Programming Tools And Techniques-I
Sections Inheritance and Abstract Classes
Advanced Java Topics Chapter 9
Inheritance and Polymorphism
Types of Programming Languages
Advanced C++ Topics Chapter 8.
Object Oriented Programming
Advanced Java Topics Chapter 9 (continued)
Figure 8.1 Inheritance: Relationships among timepieces.
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Polymorphism Polymorphism - Greek for “many forms”
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 8: Class Relationships
Chapter 11 Class Inheritance
Figure 8.1 Inheritance: Relationships among timepieces.
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007

© 2006 Pearson Addison-Wesley. All rights reserved9 A-2 Inheritance Revisited Inheritance –Allows a class to derive the behavior and structure of an existing class

© 2006 Pearson Addison-Wesley. All rights reserved9 A-3 Inheritance Revisited Figure 9-1 Inheritance: Relationships among timepieces

© 2006 Pearson Addison-Wesley. All rights reserved9 A-4 Inheritance Revisited Superclass or base class –A class from which another class is derived Subclass, derived class, or descendant class –A class that inherits the members of another class Benefits of inheritance –It enables the reuse of existing classes –It reduces the effort necessary to add features to an existing object

© 2006 Pearson Addison-Wesley. All rights reserved9 A-5 Inheritance Revisited A subclass –Can add new members to those it inherits –Can override an inherited method of its superclass A method in a subclass overrides a method in the superclass if the two methods have the same declarations

© 2006 Pearson Addison-Wesley. All rights reserved9 A-6 Inheritance Revisited Figure 9-2 The subclass Ball inherits members of the superclass Sphere and overrides and adds methods

© 2006 Pearson Addison-Wesley. All rights reserved9 A-7 Inheritance Revisited A subclass inherits private members from the superclass, but cannot access them directly Methods of a subclass can call the superclass’s public methods Clients of a subclass can invoke the superclass’s public methods An overridden method –Instances of the subclass will use the new method –Instances of the superclass will use the original method

© 2006 Pearson Addison-Wesley. All rights reserved9 A-8 Inheritance Revisited Figure 9-3 An object invokes the correct version of a method

© 2006 Pearson Addison-Wesley. All rights reserved9 A-9 Java Access Modifiers Figure 9-4 Access to public, protected, package access, and private members of a class by a client and a subclass

© 2006 Pearson Addison-Wesley. All rights reserved9 A-10 Java Access Modifiers Membership categories of a class –Public members can be used by anyone –Members declared without an access modifier (the default) are available to Methods of the class Methods of other classes in the same package –Private members can be used only by methods of the class –Protected members can be used only by Methods of the class Methods of other classes in the same package Methods of the subclass

© 2006 Pearson Addison-Wesley. All rights reserved9 A-11 Is-a and Has-a Relationships Two basic kinds of relationships –Is-a relationship –Has-a relationship

© 2006 Pearson Addison-Wesley. All rights reserved9 A-12 Is-a Relationship Inheritance should imply an is-a relationship between the superclass and the subclass Example: –If the class Ball is derived from the class Sphere A ball is a sphere Figure 9-5 A ball “is a” sphere

© 2006 Pearson Addison-Wesley. All rights reserved9 A-13 Is-a Relationship Object type compatibility –An instance of a subclass can be used instead of an instance of the superclass, but not the other way around

© 2006 Pearson Addison-Wesley. All rights reserved9 A-14 Has-a Relationships Figure 9-6 A pen “has a” or “contains a” ball

© 2006 Pearson Addison-Wesley. All rights reserved9 A-15 Has-a Relationships Has-a relationship –Also called containment –Cannot be implemented using inheritance Example: To implement the has-a relationship between a pen and a ball –Define a data field point – whose type is Ball – within the class Pen

© 2006 Pearson Addison-Wesley. All rights reserved9 A-16 Dynamic Binding and Abstract Classes A polymorphic method –A method that has multiple meanings –Created when a subclass overrides a method of the superclass Late binding or dynamic binding –The appropriate version of a polymorphic method is decided at execution time

© 2006 Pearson Addison-Wesley. All rights reserved9 A-17 Dynamic Binding and Abstract Classes Figure 9-7a area is overridden: a) mySphere.DisplayStatistics( ) calls area in Sphere

© 2006 Pearson Addison-Wesley. All rights reserved9 A-18 Dynamic Binding and Abstract Classes Figure 9-7b area is overridden: b) myBall.displayStatistics( ) calls area in Ball

© 2006 Pearson Addison-Wesley. All rights reserved9 A-19 Dynamic Binding and Abstract Classes Controlling whether a subclass can override a superclass method –Field modifier final Prevents a method from being overridden by a subclass –Field modifier abstract Requires the subclass to override the method Early binding or static binding –The appropriate version of a method is decided at compilation time –Used by methods that are final or static

© 2006 Pearson Addison-Wesley. All rights reserved9 A-20 Dynamic Binding and Abstract Classes Overloading methods –To overload a method is to define another method with the same name but with a different set of parameters –The arguments in each version of an overloaded method determine which version of the method will be used

© 2006 Pearson Addison-Wesley. All rights reserved9 A-21 Please open file carrano_ppt09_B.ppt to continue viewing chapter 9.