Lecture 18: Polymorphism (Part II)

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Advertisements

Lecture 11: Polymorphism and Dynamic Binding Polymorphism Static (compile-time) binding Dynamic (run-time) binding.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Peyman Dodangeh Sharif University of Technology Fall 2014.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1 CSE 331 The Object class; Object equality and the equals method slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-3: Polymorphism reading: 9.3.
Lecture 16: Polymorphism (Part I)
Lecture 25: Inheritance and Polymorphism
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Building Java Programs Chapter 9
Lecture 15: More Inheritance
Lecture 17: Polymorphism (Part II)
Building Java Programs
Continuing Chapter 11 Inheritance and Polymorphism
CS 302 Week 11 Jim Williams, PhD.
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
CSE 143 Lecture 22 The Object class; Polymorphism read
Inheritance.
CSE 143 Lecture 22 The Object class; Polymorphism read
Building Java Programs
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Lecture 17: Polymorphism (Part I)
Lecture 15: Inheritance II
Lecture 25: Inheritance and Polymorphism
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Chapter 8 Class Inheritance and Interfaces
Special instance methods: toString
Chapter 11 Inheritance and Polymorphism Part 2
CSE 143 Lecture 23 Polymorphism; the Object class read
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
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
Building Java Programs
Chapter 11 Inheritance and Encapsulation and Polymorphism
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Advanced Programming in Java
Building Java Programs
Presentation transcript:

Lecture 18: Polymorphism (Part II) Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved.

Polymorphism polymorphism: Ability for the same code to be used with different types of objects and behave differently with each.(Part I) System.out.println can print any type of object. Each one displays in its own way on the console. Ability for a method to take on many forms. (Part II)

Overriding If a child class has the same method as the parent class, the method of the child class overrides the method of the parent class. public class ParentClass{ public void method1(int a){…} } public class ChildClass extends ParentClass{

Overriding public class Tester{ public static void main(String[] args) ParentClass a=new ParentClass(); ParentClass b=new ChildClass(); a.method1();//calls method1 of ParentClass b.method1();//calls method1 of ChildClass }} Method overriding is also known as run-time polymorphism or dynamic binding. Java selects the correct method1 at run-time.

Overloading A class can have many forms of the same method. The methods are distinguished by: Number of parameters Type of the parameters Order of the parameters Method overloading is also known as compile-time polymorphism or static binding. Java selects the correct method at compile-time.

Number of Parameters Methods with the same name can be distinguished by the number of parameters. public class Overload{ public void method1(int c) {…} public void method1(int c, double d) }

Type of Parameters Methods with the same name can be distinguished by the type of the parameters. public class Overload{ public void method1(int c) {…} public void method1(double c) }

Sequence of Parameters Methods with the same name can be distinguished by the order of the parameters. public class Overload{ public void method1(int c, double d) {…} public void method1(double d, int c) }

Invalid Overloading Case 1: public void method1(int c, double d) {…} public void method1(int e, double f) Compile error. Same number, data types and sequence. Methods cannot be overloaded with just different variable names. method1(3,4.1);

Invalid Overloading Case 2: public void method1(int c, double d) {…} public boolean method1(int e, double f) Compile error. Same number, data types and sequence. Even though the return type is different, this is not valid. method1(3,4.1);

Ambiguous Call public static void method1(int a, double b) {…} public static void method1(double a, int b) public static void main(String[] args) { method1(3,4.0); method1(3.3,4); method1(3,4); //error, ambiguous call }

Compile-time vs Runtime An error is a compile-time error if it happens when the program compiles. An error is a runtime error if it happens when the program runs. A runtime error compiles without errors.

Compile-time vs Runtime Employee Sean=new Secretary(); Sean.takeDictation(“hi”); //compile-time error, no such method in Employee Sean.fileLegalBriefs(); // compile-time error, no such method in Employee Sean.getHours(); //ok

Compile-time vs Runtime ((LegalSecretary) Sean).sue(); //compile-time error,sue() isn’t in LegalSecretary ((LegalSecretary) Sean).fileLegalBriefs(); //runtime error, cast too far down the tree //the program compiles without errors. ((Lawyer) Sean).sue(); //runtime error, horizontal casting not allowed;

The Cosmic SuperClass Object All types of objects have a superclass named Object. Every class implicitly extends Object The Object class defines several methods: public String toString() Returns a text representation of the object, often so that it can be printed. public boolean equals(Object other) Compare the object to any other for equality. Returns true if the objects have equal state.

Object variables You can store any object in a variable of type Object. Object o1 = new Point(5, -3); Object o2 = "hello there"; Object o3 = new Scanner(System.in); An Object variable only knows how to do general things. String s = o1.toString(); // ok int len = o2.length(); // compile-time error String line = o3.nextLine(); // compile-time error

Recall: comparing objects The == operator does not work well with objects. == compares references to objects, not their state. It only produces true when you compare an object to itself. Point p1 = new Point(5, 3); Point p2 = new Point(5, 3); if (p1 == p2) { // false System.out.println("equal"); } ... x 5 y 3 p1 ... p2 x 5 y 3

The equals method The equals method compares the state of objects. if (str1.equals(str2)) { System.out.println("the strings are equal"); } But if you write a class, its equals method behaves like == if (p1.equals(p2)) { // false :-( System.out.println("equal"); This is the behavior we inherit from class Object. Java doesn't understand how to compare Points by default.

equals method We can change this behavior by writing an equals method. Ours will override the default behavior from class Object. The method should compare the state of the two objects and return true if they have the same x/y position. public boolean equals(Object o) { Point other=(Point) o; if (x == other.x && y == other.y) { return true; } else { return false; } NOTE: Flawed implementation. What if o is NOT a Point?

The instanceof keyword (NOT ON AP Exam) if (variable instanceof type) { statement(s); } Asks if a variable refers to an object of a given type. Used as a boolean test. String s = "hello"; Point p = new Point(); expression result s instanceof Point false s instanceof String true p instanceof Point p instanceof String p instanceof Object s instanceof Object null instanceof String null instanceof Object

Final equals method // Returns whether o refers to a Point object with // the same (x, y) coordinates as this Point. public boolean equals(Object o) { if (o instanceof Point) { // o is a Point; cast and compare it Point other = (Point) o; return x == other.x && y == other.y; } else { // o is not a Point; cannot be equal return false; }

Point Class public class Point{ int x; int y; @Override //overriding toString of Object class. public String toString(){ return “(“+x+”,”+y+”)”;} @Override //overriding equals of Object class. public boolean equals(Object o) { if (o instanceof Point) { // o is a Point; cast and compare it Point other = (Point) o; return x == other.x && y == other.y; } else { // o is not a Point; cannot be equal return false; } }}

Point Tester public class PointTester{ public static void main(String[] args){ Point p1=new Point(2,3); Point p2=new Point(2,3); System.out.println(p1); //(2,3), calls toString() System.out.println(p1.toString()); //(2,3) explicit call //comparing addresses of objects if(p1==p2) //false, different objects, different addresses { … } //comparing x-y coordinates of Point objects if(p1.equals(p2)) //true }}

Lab 1 Complete the lab on polymorphism posted on Classroom. Download the files polymorphismLab, Circle, Rectangle and Shape. Follow the direction given by the comments.