Advanced Java Topics Chapter 9

Slides:



Advertisements
Similar presentations
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Advertisements

(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
© 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.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
© 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.
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.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Inheritance in the Java programming language J. W. Rider.
© 2011 Pearson Addison-Wesley. All rights reserved 9 B-1 Chapter 9 (continued) Advanced Java Topics.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
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.
 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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
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.
 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.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
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)
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.
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.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Sections Inheritance and Abstract Classes
OOP: Encapsulation &Abstraction
Advanced Java Topics Chapter 9
Inheritance and Polymorphism
Types of Programming Languages
Road Map Inheritance Class hierarchy Overriding methods Constructors
Advanced C++ Topics Chapter 8.
Object Oriented Programming
Advanced Java Topics Chapter 9 (continued)
Inheritance, Polymorphism, and Interfaces. Oh My
Figure 8.1 Inheritance: Relationships among timepieces.
Chapter 9 Object-Oriented Programming: Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Java – Inheritance.
Polymorphism Polymorphism - Greek for “many forms”
Overriding Methods & Class Hierarchies
Java Programming, Second Edition
Java Inheritance.
Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 8: Class Relationships
CIS 199 Final Review.
Chapter 11 Class Inheritance
Chapter 7 Inheritance.
Figure 8.1 Inheritance: Relationships among timepieces.
Presentation transcript:

Advanced Java Topics Chapter 9 © 2006 Pearson Addison-Wesley. All rights reserved

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

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

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 reserved

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 reserved

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 reserved

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 reserved

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

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 reserved

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 subclass © 2006 Pearson Addison-Wesley. All rights reserved

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

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 reserved

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 reserved

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

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 reserved

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 reserved

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

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

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 reserved

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 reserved

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