© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)

Slides:



Advertisements
Similar presentations
Has-a Relationships A pen “has a” or “contains a” ball.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
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.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
© 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.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 10: Inheritance and Polymorphism
©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.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 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 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
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.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and 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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
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.
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
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.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
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.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
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: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Advanced Java Topics Chapter 9
Inheritance and Polymorphism
Object-Oriented Programming
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
Java – Inheritance.
Polymorphism Polymorphism - Greek for “many forms”
Chapter 9 Carrano Chapter 10 Small Java
Chapter 8: Class Relationships
Chapter 7 Inheritance.
Figure 8.1 Inheritance: Relationships among timepieces.
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)

© 2006 Pearson Addison-Wesley. All rights reserved9 A-2 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-3 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-4 Inheritance Revisited Figure 9-1 Inheritance: Relationships among timepieces

© 2006 Pearson Addison-Wesley. All rights reserved9 A-5 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-6 Inheritance Revisited A subclass –Can add new data 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 –(remember: overriding is not the same as overloading)

© 2006 Pearson Addison-Wesley. All rights reserved9 A-7 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-8 Inheritance Revisited Figure 9-3 An object invokes the correct version of a method

© 2006 Pearson Addison-Wesley. All rights reserved9 A-9 Dynamic Binding 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-10 Dynamic Binding 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-11 Abstract Classes Example –CD player and DVD player Both involve an optical disk Operations –Insert, remove, play, record, and stop such discs

© 2006 Pearson Addison-Wesley. All rights reserved9 A-12 Abstract Classes Figure 9-8 CDP and DVDP have an abstract base class GDP

© 2006 Pearson Addison-Wesley. All rights reserved9 A-13 Abstract Classes Abstract classes –A class that contains at least one abstract method must be declared as an abstract class –A subclass of an abstract class must be declared abstract if it does not provide implementations for all abstract methods in the superclass

© 2006 Pearson Addison-Wesley. All rights reserved9 A-14 Interfaces vs. Abstract Classes A Java interface –Specifies the common behavior of a set of classes –Contains no implemented methods –Contains no instance data Abstract class –Specifies common behaviour AND data of a set of classes –May implement some methods –May contain instance data

© 2006 Pearson Addison-Wesley. All rights reserved9 A-15 Java Interfaces Revisited Inheritance can be used to define a subinterface The Java API provides many interfaces and subinterfaces –Example: java.util.Iterable An iterator is a class that provides access to another class that contains many objects

© 2006 Pearson Addison-Wesley. All rights reserved9 A-16 The ADTs List and Sorted List Revisited BasicADTInterface –Can be used to organize the commonalities between the ADT list and the ADT sorted list –ListInterface A new interface based on BasicADTInterface

© 2006 Pearson Addison-Wesley. All rights reserved9 A-17 Implementations of the ADT Sorted List That Use the ADT List A sorted list is a list –With an additional operation, locateIndex A sorted list has a list as a member

© 2006 Pearson Addison-Wesley. All rights reserved9 A-18 Java Generics: Generic Classes ADT developed in this text relied upon the use of Object class Problems with this approach –Items of any type could be added to same ADT instance –ADT instance returns objects Cast operations are needed May lead to class-cast exceptions Avoid these issues by using Java generics –To specify a class in terms of a data-type parameter

© 2006 Pearson Addison-Wesley. All rights reserved9 A-19 Example – Nodes with a type public class Node { private T item; private Node next;.... contsructors/other method.... public T getIte() { return item; }

© 2006 Pearson Addison-Wesley. All rights reserved9 A-20 Generic Wildcards Generic ? wildcard –Stands for unknown data type Example public void printnode(Node temp) { System.out.println(temp.getItem()); }

© 2006 Pearson Addison-Wesley. All rights reserved9 A-21 Generic Classes and Inheritance You can use inheritance with a generic class or interface It is sometimes useful to constrain the data- type parameter to a class or one of its subclasses or an implementation of a particular interface –To do so, use the keyword extends

© 2006 Pearson Addison-Wesley. All rights reserved9 A-22 Abstract Classes Figure 9-10 Sample class hierarchy

© 2006 Pearson Addison-Wesley. All rights reserved9 A-23 Generic Methods Method declarations can also be generic –Methods can use data-type parameters Generic methods are invoked like regular non-generic methods Example public static > void sort(ArrayList list) { // implementation of sort appears here } // end sort