(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 27 - Java Object-Oriented Programming Outline.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Evan Korth New York University abstract classes and casting Professor Evan Korth New York University.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
© 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.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Object-Oriented Programming: Polymorphism
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
COP 3003 Object-Oriented Programming - Polymorphism Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Lecture 9 Polymorphism Richard Gesick.
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
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.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Inheritance ndex.html ndex.htmland “Java.
Object-Oriented Programming: Inheritance and Polymorphism.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
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
Inheritance and Polymorphism
Object-Oriented Programming: Polymorphism
Lecture 23 Polymorphism Richard Gesick.
Object-Oriented Programming: Polymorphism
Based on slides from Deitel & Associates, Inc.
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Java – Inheritance.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Java Inheritance.
Fundaments of Game Design
Chapter 9 Carrano Chapter 10 Small Java
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
C++ Object Oriented 1.
Presentation transcript:

(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e

 Polymorphism  Use overridden methods in polymorphism  Distinguish b/w abstract and concrete classes  Benefits of Polymorphism  Declare and Implement Interfaces (C) 2010 Pearson Education, Inc. All rights reserved.

 Enables you to “program in the general” rather than “program in the specific.”  Polymorphism enables you to write programs that process objects that share the same superclass as if they’re all objects of the superclass; this can simplify programming.

(C) 2010 Pearson Education, Inc. All rights reserved.  Example: Quadrilaterals  If Rectangle is derived from Quadrilateral, then a Rectangle object is a more specific version of a Quadrilateral.  Any operation that can be performed on a Quadrilateral can also be performed on a Rectangle.  These operations can also be performed on other Quadrilateral s, such as Square s, Parallelogram s and Trapezoid s.  Polymorphism occurs when a program invokes a method through a superclass Quadrilateral variable—at execution time, the correct subclass version of the method is called, based on the type of the reference stored in the superclass variable.

(C) 2010 Pearson Education, Inc. All rights reserved.  When a superclass variable contains a reference to a subclass object, and that reference is used to call a method, the subclass version of the method is called.  The Java compiler allows this “crossover” because an object of a subclass is an object of its superclass (but not vice versa).  When the compiler encounters a method call made through a variable, the compiler determines if the method can be called by checking the variable’s class type.  At execution time, the type of the object to which the variable refers determines the actual method to use.  This process is called dynamic binding.

(C) 2010 Pearson Education, Inc. All rights reserved.  Abstract classes  Sometimes it’s useful to declare classes for which you never intend to create objects.  Used only as superclasses in inheritance hierarchies, so they are sometimes called abstract superclasses.  Cannot be used to instantiate objects—abstract classes are incomplete.  Subclasses must declare the “missing pieces” to become “concrete” classes, from which you can instantiate objects; otherwise, these subclasses, too, will be abstract.  An abstract class provides a superclass from which other classes can inherit and thus share a common design.

(C) 2010 Pearson Education, Inc. All rights reserved.  Classes that can be used to instantiate objects are called concrete classes.  Such classes provide implementations of every method they declare

(C) 2010 Pearson Education, Inc. All rights reserved.  Programmers often write client code that uses only abstract superclass types to reduce client code’s dependencies on a range of subclass types.

(C) 2010 Pearson Education, Inc. All rights reserved.  You make a class abstract by declaring it with keyword abstract.  An abstract class normally contains one or more abstract methods.  An abstract method is one with keyword abstract in its declara-tion, as in public abstract void draw(); // abstract method  Abstract methods do not provide implementations.  A class that contains abstract methods must be an abstract class even if that class contains some concrete (nonabstract) methods.  Each concrete subclass of an abstract superclass also must provide concrete implementations of each of the superclass’s abstract methods.  Constructors and static methods cannot be declared abstract.

(C) 2010 Pearson Education, Inc. All rights reserved.  Polymorphism is particularly effective for implementing so- called layered software systems.  Example: Operating systems and device drivers.

(C) 2010 Pearson Education, Inc. All rights reserved.  Every object in Java knows its own class and can access this information through the getClass method, which all classes inherit from class Object.

(C) 2010 Pearson Education, Inc. All rights reserved. 1. Assigning a superclass reference to a superclass variable is straightforward. Animal animal = new Animal(); 2. Assigning a subclass reference to a subclass variable is straightfor-ward. Dog dog = new Dog(); 3. Assigning a subclass reference to a superclass variable is safe, because the subclass object is an object of its superclass. Animal animal = new Dog(); 4. Attempting to assign a superclass reference to a subclass variable is a compilation error. Dog dog = new Animal ();  WRONG!!!! Dog dog = new (Dog) Animal();  Downcasting

(C) 2010 Pearson Education, Inc. All rights reserved.  A final method in a superclass cannot be overridden in a subclass.  private and static methods are implicitly final.

(C) 2010 Pearson Education, Inc. All rights reserved.  Once a class implements an interface, all objects of that class have an is-a relationship with the interface type, and all objects of the class are guaranteed to provide the functionality described by the interface.  This is true of all subclasses of that class as well.  Interfaces are particularly useful for assigning common functionality to possibly unrelated classes.  Allows objects of unrelated classes to be processed polymorphically—objects of classes that implement the same interface can respond to all of the interface method calls.

(C) 2010 Pearson Education, Inc. All rights reserved.  An interface describes a set of methods that can be called on an object, but does not provide concrete implementations for all the methods.  You can declare classes that implement (i.e., provide concrete implementations for the methods of) one or more interfaces.  Each interface method must be declared in all the classes that explicitly implement the interface.

(C) 2010 Pearson Education, Inc. All rights reserved.  Interfaces offer a capability requiring that unrelated classes implement a set of common methods.  Interfaces define and standardize the ways in which things such as people and systems can interact with one another.  Example: The controls on a radio serve as an interface between radio users and a radio’s internal components.  Can perform only a limited set of operations (e.g., change the station, adjust the volume, choose between AM and FM)  Different radios may implement the controls in different ways (e.g., using push buttons, dials, voice commands).

(C) 2010 Pearson Education, Inc. All rights reserved.  The interface specifies what operations a radio must permit users to perform but does not specify how the operations are performed.  A Java interface describes a set of methods that can be called on an object.

(C) 2010 Pearson Education, Inc. All rights reserved.  An interface declaration begins with the keyword interface and contains only constants and abstract methods.  All interface members must be public.  Interfaces may not specify any implementation details, such as concrete method declarations and instance variables.  All methods declared in an interface are implicitly public abstract methods.  All fields are implicitly public, static and final.

(C) 2010 Pearson Education, Inc. All rights reserved.  To use an interface, a concrete class must specify that it implements the interface and must declare each method in the interface with specified signature.  Implementing an interface is like signing a contract with the compiler that states, “I will declare all the methods specified by the interface or I will declare my class abstract.”

(C) 2010 Pearson Education, Inc. All rights reserved.  When there is no default implementation to inherit— that is, no fields and no default method implementations.

(C) 2010 Pearson Education, Inc. All rights reserved.

 Shape classes have many similarities.  Using inheritance, we can “factor out” the common features from all three classes and place them in a single shape superclass.  Then, using variables of the superclass type, we can manipulate objects of all three shape types polymorphically.  Removing the redundancy in the code will result in a smaller, more flexible program that is easier to maintain.

(C) 2010 Pearson Education, Inc. All rights reserved.

 Class MyBoundedShape can be used to factor out the common features of classes MyOval and MyRectangle.

(C) 2010 Pearson Education, Inc. All rights reserved.