Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
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.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Chapter 8 User-Defined Classes and ADTs. Chapter Objectives Learn about classes Learn about private, protected, public, and static members of a class.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
UML Class Diagram: class Rectangle
Chapter 10: Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
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.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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 Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Coming up: Inheritance
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
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.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
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.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
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,
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism
UML Class Diagram: class Rectangle
Inheritance, Polymorphism, and Interfaces. Oh My
User-Defined Classes and ADTs
Computer Science II Exam 1 Review.
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Advanced Java Topics Chapter 9
Chapter 8: User-Defined Classes and ADTs
User-Defined Classes and ADTs
Java Inheritance.
Chapter 8 Classes User-Defined Classes and ADTs
Chapter 8 Class Inheritance and Interfaces
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism

Java Programming: Guided Learning with Early Objects2 Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the methods of a superclass Examine how constructors of superclasses and subclasses work together

Java Programming: Guided Learning with Early Objects3 Objectives (continued) Become familiar with protected members of a class Learn about polymorphism Examine abstract classes Become familiar with interfaces Learn about composition (aggregation)

Java Programming: Guided Learning with Early Objects4 Inheritance Mechanism that allows class definition to be extended –No changes to existing class –Implies “is-a” relationship Subclass: new class derived from existing class –Also called derived class Superclass: existing class that other classes are derived from –Also called base class

Java Programming: Guided Learning with Early Objects5 Figure 9-1 Inheritance hierarchy

Java Programming: Guided Learning with Early Objects6 Inheritance (continued) Inheritance has a hierarchical structure Syntax: modifiers class ClassName extends BaseClass modifiers { memberList } private members of the superclass cannot be accessed directly by the subclass

Java Programming: Guided Learning with Early Objects7 Inheritance (continued) Subclass directly accesses public members of superclass Subclass can include additional members Subclass can override public methods of superclass –Subclass can shadow public variables of superclass Members of superclass are also members of subclass

Java Programming: Guided Learning with Early Objects8 Inheritance (continued) Single inheritance: subclass derived from a single superclass Multiple inheritance: subclass derived from more than one superclass Java supports only single inheritance Constructor of subclass cannot directly access private data members of the superclass

Java Programming: Guided Learning with Early Objects9 Using Methods of the Superclass in a Subclass Data members of subclass include its own data members and members of superclass Overriding: subclass method may have same signature as method in superclass –Also called redefining a method –Corresponding method in subclass must have same name and formal parameters Overloading: same name, different formal parameter list

Java Programming: Guided Learning with Early Objects10 Figure 9-2 UML class diagram of the class Rectangle

Java Programming: Guided Learning with Early Objects11 Figure 9-3 UML class diagram of the class Box and the inheritance hierarchy

Java Programming: Guided Learning with Early Objects12 Using Methods of the Superclass in a Subclass (continued) Write method definitions of a subclass to specify a call to a public method of superclass: –Subclass overrides public method Must call method of using super, dot operator, method name, parameter list –Subclass does not override public method Call public method using method name, parameter list

Java Programming: Guided Learning with Early Objects13 Constructors of the Superclass and Subclass Subclass may have its own private data members –Should have its own constructors Constructors initialize instance variables

Java Programming: Guided Learning with Early Objects14 Constructors of the Superclass and Subclass (continued) Subclass inherits instance variables of superclass –Cannot directly access private instance variables of superclass –Cannot directly access private methods of superclass

Java Programming: Guided Learning with Early Objects15 Constructors of the Superclass and Subclass (continued) Subclass constructors should initialize only the instance variables of the subclass Call superclass constructor to instantiate superclass instance variables Reserved word super calls superclass constructor Superclass constructor parameters passed to super

Java Programming: Guided Learning with Early Objects16 Figure 9-4 Objects myRectangle and myBox

Java Programming: Guided Learning with Early Objects17 Protected Members of a Class private class members cannot be accessed directly outside the class Subclass cannot access private members of superclass directly Making private members public violates encapsulation

Java Programming: Guided Learning with Early Objects18 Protected Members of a Class (continued) protected members can be accessed directly by subclasses UML diagram: # means protected

Java Programming: Guided Learning with Early Objects19 Figure 9-6 UML class diagram of the class BaseClass

Java Programming: Guided Learning with Early Objects20 Figure 9-7 UML class diagram of the class DerivedClass and the inheritance hierarchy

Java Programming: Guided Learning with Early Objects21 class Object Recall that class Clock included the method toString Every Java class includes method toString –Method toString comes from Java class Object Defining a class without using extends derives the class from Object class Object is the superclass of every Java class

Java Programming: Guided Learning with Early Objects22 Table 9-1 A Constructor and Some Methods of the class Object

Java Programming: Guided Learning with Early Objects23 class Object (continued) Every public member of class Object can be overridden or invoked by every object –Method toString is a public member of every Java class Default definition returns class name and hash code of object as a string –Method equals determines whether two objects are aliases –Method clone makes shallow copy of an object

Java Programming: Guided Learning with Early Objects24 Java Stream Classes Recall that class Scanner is used for inputting data from standard input device –I/O performed using FileReader and PrintWriter class InputStreamReader derived from class Reader class FileReader derived from class InputStreamReader class PrintWriter derived from class Writer

Java Programming: Guided Learning with Early Objects25 Figure 9-8 Java stream classes hierarchy

Java Programming: Guided Learning with Early Objects26 Polymorphism via Inheritance Methods in different classes may have same name and same formal parameter list Reference variable of a class can refer to object of its own class or a subclass Reference variable can invoke method of its own class or of its subclasses

Java Programming: Guided Learning with Early Objects27 Polymorphism via Inheritance (continued) Binding: associating a method definition with its invocation Early binding: method’s definition associated with its invocation at compile time Late binding: method’s definition associated with its invocation at run time

Java Programming: Guided Learning with Early Objects28 Polymorphism via Inheritance (continued) Java uses late binding for all methods Polymorphism: associating multiple meanings with the same method name Polymorphic reference variables can point to own class or objects of subclasses

Java Programming: Guided Learning with Early Objects29 Operator instanceof Object of subclass can be considered object of superclass Using appropriate cast operator can treat object of superclass as an object of subclass Operator instanceof determines class type of an object reference

Java Programming: Guided Learning with Early Objects30 Abstract Methods and Classes Abstract method has only heading but no body Heading contains reserved word abstract –Ends with a semicolon Example: public void abstract print ();

Java Programming: Guided Learning with Early Objects31 Abstract Methods and Classes (continued) Abstract class declared with reserved word abstract in heading Abstract class contains instance variables, constructors, and non abstract methods Abstract class contains one or more abstract methods

Java Programming: Guided Learning with Early Objects32 Abstract Methods and Classes (continued) If class contains abstract method, class must be declared abstract Cannot instantiate an object of an abstract class –Can declare a reference variable of abstract class type

Java Programming: Guided Learning with Early Objects33 Abstract Methods and Classes (continued) Can instantiate an object of a subclass of an abstract class –Must define all abstract methods of the superclass Used as superclasses from which other subclasses can be derived –Placeholders to store common members

Java Programming: Guided Learning with Early Objects34 Figure 9-9 Inheritance hierarchy of banking accounts

Java Programming: Guided Learning with Early Objects35 Interfaces Recall that class ActionListener is an interface Other interfaces: –interface WindowListener –interface MouseListener Java does not support multiple inheritance –Class can extend definition of only one class Java program may contain variety of GUI components –Generate many events

Java Programming: Guided Learning with Early Objects36 Interfaces (continued) Java program can implement many interfaces Inner class implements appropriate interface Interface contains only abstract methods and named constants Interfaces defined using reserved word interface

Java Programming: Guided Learning with Early Objects37 Polymorphism via Interfaces Interfaces allow GUI programs to handle more than one type of event Events handled by separate interfaces Interface used in implementation of abstract data types Hides implementation details from user

Java Programming: Guided Learning with Early Objects38 Polymorphism via Interfaces (continued) Can create polymorphic references using interfaces –Interface name as type of reference variable Cannot create object of an interface –Interface contains only method headings and named constants

Java Programming: Guided Learning with Early Objects39 Polymorphism via Interfaces (continued) Interface name can declare a parameter to a method Any reference variable of class that implements an interface can be passed as parameter

Java Programming: Guided Learning with Early Objects40 Composition (Aggregation) Composition: one or more members of a class are objects of one or more other classes “Has-a” relationship Example: –“Every person has a birthday.”

Java Programming: Guided Learning with Early Objects41 Figure 9-17 UML class diagram of the class Date

Java Programming: Guided Learning with Early Objects42 Figure 9-18 UML class diagram of the class PersonalInfo and composition (aggregation)

Java Programming: Guided Learning with Early Objects43 Summary Inheritance and composition are meaningful ways to relate two or more classes Inheritance is an “is-a” relationship Composition (aggregation) is a “has-a” relationship Single inheritance: subclass derived from only one superclass Multiple inheritance: subclass derived from one or more superclasses

Java Programming: Guided Learning with Early Objects44 Summary (continued) Java allows only single inheritance private members of a superclass are private to the superclass –Subclass cannot access them directly Subclass can override methods of a superclass –Available only to objects of subclass

Java Programming: Guided Learning with Early Objects45 Summary (continued) If subclass overrides public method of superclass –Specify a call to public method of superclass Use reserved word super, dot operator, method name, and parameter list Dot operator and method name not required for constructor If subclass does not override a public method of superclass –Specify method call using name of method, parameter list

Java Programming: Guided Learning with Early Objects46 Summary (continued) Call to constructor of superclass uses reserved word super with appropriate parameter list –Call to super must be first statement Modifier protected gives subclass direct access to members –protected members may be accessed by any class in the same package Classes not explicitly derived from other classes derived from class Object

Java Programming: Guided Learning with Early Objects47 Summary (continued) class Object directly or indirectly becomes superclass of every class in Java Reference variable of superclass can point to object of subclass type Several methods can have same name and same formal parameter list Reference variable of a class can refer to object of its own class or object of its subclass

Java Programming: Guided Learning with Early Objects48 Summary (continued) Early binding: method’s definition associated with its invocation at compile time Late binding: method’s definition associated with invocation at run time Java uses late binding for all methods –Few exceptions Polymorphism: assign multiple meanings to same method –Implemented using late binding

Java Programming: Guided Learning with Early Objects49 Summary (continued) Cannot automatically make reference variable of subclass type point to object of superclass Abstract method has only the heading with reserved word abstract Abstract class declared with reserved word abstract in its heading –Contains instance variables, constructors, and non abstract methods –Can contain abstract methods

Java Programming: Guided Learning with Early Objects50 Summary (continued) If a class contains abstract method, it must be declared abstract Cannot instantiate an object of an abstract class –Can instantiate object of subclass if you define all abstract methods Interface contains only abstract methods, named constants Class may implement more than one interface