Polymorphism.

Slides:



Advertisements
Similar presentations
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Advertisements

Polymorphism. 3 Fundamental Properties of OO 1. Encapsulation 2. Polymorphism 3. Inheritance These are the 3 building blocks of object-oriented design.
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
23-May-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
15-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
19-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
More about inheritance Exploring polymorphism 5.0.
1 Topic 10 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.”
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
Lecture 8: Object-Oriented Design. 8-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Good object-oriented programming is really.
1 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.” CS Computer Science II.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Inheritance  Inheritance is a fundamental object-oriented technique  it enhances software design and promotes reuse  We will focus on:  deriving new.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Chapter 13 - Inheritance.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Lecture 12 Inheritance.
Object-oriented Programming in Java
Overriding Method.
public class Doubler extends Base {
More about inheritance
Lecture 17: Polymorphism (Part II)
COS 260 DAY 19 Tony Gauvin.
Object-Oriented Programming: Polymorphism
Polymorphism 11-Nov-18.
Variables as Remote Control
Computer Science II Exam 1 Review.
Polymorphism 15-Nov-18.
Extending Classes.
Polymorphism and access control
Polymorphism 28-Nov-18.
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance.
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Building Java Programs
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 1020:Inheritance Mark Shtern.
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Introduction to Inheritance
Lecture 18: Polymorphism (Part II)
Final Jim Brucker.
Chapter 14 Abstract Classes and Interfaces
Polymorphism 15-Apr-19.
Lecture 25: Inheritance and Polymorphism
Polymorphism 21-Apr-19.
Chapter 11 Inheritance and Polymorphism Part 1
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Chapter 11 Inheritance and Encapsulation and Polymorphism
CIS 110: Introduction to computer programming
Presentation transcript:

Polymorphism

Poly - morph = many forms Polymorphism We can invoke a behavior (method) without knowing what kind of object will perform the behavior. Many kinds of objects can perform the same behavior. Poly - morph = many forms Object x = null; x = new Double(3.14); x.toString(); // calls toString() of Double class x = new Date( ); x.toString(); // calls toString() of Date class Polymorphism

How does println( ) work? System.out.println( Object x ) can print any kind of object. Even object types we define ourselves (like Student). How is it possible for one method to print any kind of object? any kind of Object Object x = new Something( ); System.out.println( x ); // prints String form of x

println( ) uses Polymorphism println(x) calls x.toString(). println(x) doesn't know the type of object that x refers to. Using polymorphism, x.toString() always invokes toString() of the correct class. Object x = new Date( ); System.out.println( x ); // calls x.toString() // invokes toString of Date class x = new Student("Bill Gates"); System.out.println( x ); // invokes toString of Student class

Enabling Polymorphism The key to polymorphism asking an object to do something (call its method) without knowing the kind of object. Object a = .?.; a.toString( ); a.run( ); How can we invoke an object's method without knowing what class it belongs to? a.toString() always works for any kind of object. Why? This is an error. a might not have a "run" method. Why?

Enabling Polymorphism The compiler has to "know" that x will always have the requested method... regardless of the actual type of x. x.run( ) // huh? Does x have a run()? We must guarantee that different kinds of objects will have the method we want to invoke.

Two Ways to Enable Polymorphism In Java there are two ways to "guarantee" that a class has some behavior (method): 1. Inheritance If a superclass has a method, then all its subclasses are guaranteed to have the method, too. Subclasses can use the parent's method, or override it with their own implementation. 2. Interface An interface specifies one or more methods. A class that implements an interface must have the method(s) of the interface.

Inheritance and Polymorphism Every class is a subclass of Object. Therefore, every object is guaranteed to have all the methods from the Object class. Object equals(Object): boolean toString(): String ... Every object is guaranteed to have equals(Object) and toString( ) methods. We can invoke them for any kind of object. X equals(Object): boolean toString(): String ...

Methods from Object Every Java class is a subclass of the Object class. Therefore, every object has the public methods from Object. Usually, classes will override these methods to provide useful implementations. Object equals(Object): boolean getClass(): Class hashCode(): int toString(): String notify() wait() ... Every class inherits these methods automatically. So, we can always use obj.toString() or obj.equals(obj2) for any kind of object.

Interface Interface is a specification for some required behavior, without an implementation. A Java interface specifies behavior which will be provided by classes that implement the interface. Example: USB interface specifies (a) connector size, (b) electrical properties, (c) communications protocol, ... Anyone can implement the USB interface on their device. We can use any USB port the same way, without knowing the actual type (manufacturer) of the device.

java.lang.Runnable interface public interface Runnable { /** * The method to invoke. It doesn't * return anything. * @see java.lang.Runnable#run() */ public void run( ); } abstract method = method signature only, no implementation

Runnable example Declare that this class has the run( ) behavior. public class MyTask implements Runnable { /** The required method. */ public void run() { System.out.println("I'm running!"); } Implement the required method.

Use the interface in an app public class TaskRunner { /** * Run a task n times. * @param task a Runnable to perform * @param count number of time to do it. */ public void repeat(Runnable task, int count) { while(count > 0) { task.run( ); count--; }

Example: print message 5 times TaskRunner runner = new TaskRunner(); Runnable mytask = new MyTask( ); runner.repeat(mytask, 5); I'm running.

How does Interface enable Polymorphism? We can define many tasks that implement Runnable. TaskRunner can use any task without knowing its type. Every task is guaranteed to have a run( ) method. Runnable task = null; if (time < 1700) task = new StudyTask( ); else task = new PlayTask( ); runner.repeat( task, 3 );

UML for interface UML class diagram for this example. Notice that TaskRunner does not depend on MyTask.

Make MyTask more flexible Modify MyTask so we can use it to print any message. public class MyTask implements Runnable { ??? /** @see java.lang.Runnable#run() */ public void run() { System.out.println("_โฆษณาที่นี่: 02-9428555_"); }

Solution Modify MyTask so we can use it to print any message. public class MyTask implements Runnable { private String message; /** @param message is the message to print */ public MyTask(String message) { this.message = message; } /** @see java.lang.Runnable#run() */ public void run() { System.out.println( message );

Summary Polymorphism in OOP means that many kinds of objects can provide the same behavior (method), and we can invoke that behavior without knowing which kind of object will perform it.

Enabling Polymorphism To use polymorphism, we must guarantee that the object x refers to has the method we want to invoke. x.toString( ) // x must have a toString method How can we guarantee this?? 1. Use inheritance: a subclass has all the methods of its superclass. 2. Use an Interface: an interface specifies a required behavior without implementing it. Every class that implements the interface must provide the interface's behavior.

Don't ask "what type?" Don't ask "what type" With polymorphism, we can invoke a behavior without knowing the type (class) of object that will perform the behavior. We don't test for the type of object. So, polymorphism has the nickname: Don't ask "what type" Anti-polymorphism example: public void repeat(Object task, int count) { if (task instanceof MyTask) { MyTask my = (MyTask) task; while(count-- > 0) my.run( ); }