28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
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.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
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.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CMSC 202 Inheritance II. Version 10/102 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
What is inheritance? It is the ability to create a new class from an existing class.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 7.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
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.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
BY:- TOPS Technologies
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Lecture 12 Inheritance.
Chapter 11 Inheritance and Polymorphism
Week 8 Lecture -3 Inheritance and Polymorphism
Polymorphism 11-Nov-18.
Polymorphism 11-Nov-18.
Polymorphism 15-Nov-18.
Inheritance 2nd Lecture
Polymorphism 28-Nov-18.
Inheritance 2nd Lecture
Inheritance 2nd Lecture
Java Inheritance.
Polymorphism 15-Apr-19.
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Polymorphism 21-Apr-19.
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
CMSC 202 Inheritance II.
Presentation transcript:

28-Dec-04polymorhism.ppt1 Polymorphism

28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function or method from another in C, every function has to have a different name in Java, two methods have to differ in their names, or in the number or types of their parameters foo(int i) and foo(int i, int j) are different foo(int i) and foo(int k) are the same foo(int i, double d) and foo(double d, int i) are different in C++, the signature also includes the return type but not in Java!

28-Dec-04polymorhism.ppt3 poly + morphism poly is Latin for “many”; morph is Latin for “form” in Java, a method or constructor may have multiple signatures this is called overloading EXAMPLE: Cat c1 = new Cat(); Cat c2 = new Cat(“Felix”); // overloaded constructor c1.speak(); c1.speak(3); // overloaded method overloading permits multiple ways to construct an object, or multiple ways to call a method

28-Dec-04polymorhism.ppt4 polymorphism in inheritance a method or constructor may be modified in a subclass during inheritance this is called overriding EXAMPLE: // assuming both Cat and Dog extend Animal, and Animal has // a speak() method defined: Cat c = new Cat(); c.speak(); // overridden to speak specifically like a Cat Dog d = new Dog(); d.speak(); // overridden to speak specifically like a Dog if you override a superclass variable, you may get unintended consequences this is called shadowing it is generally to be avoided

28-Dec-04polymorhism.ppt5 an example of overloading class Test { public static void main(String args[]) { myPrint(5); myPrint(5.0); } static void myPrint(int i) { System.out.println("int i = " + i); } static void myPrint(double d) { // same name, different parameters System.out.println("double d = " + d); } } int i = 5 double d = 5.0

28-Dec-04polymorhism.ppt6 DRY (Don’t Repeat Yourself) when you overload a method, only one of the similar methods should do most of the work: void debug() { System.out.println("first = " + first + ", last = " + last); for (int i = first; i <= last; i++) { System.out.print(dictionary[i] + " "); } System.out.println(); } void debug(String s) { System.out.println("At checkpoint " + s + ":"); debug(); }

28-Dec-04polymorhism.ppt7 another reason for polymorphism you may want to do “the same thing” with different kinds of data: class Student extends Person {... void printInformation() { printPersonalInformation(); printGrades(); } } class Professor extends Person() {... void printInformation() { printPersonalInformation(); printResearchInterests(); } } Java’s print and println methods are heavily overloaded if Person has a method called “printInformation”, it is overridden if Person has no such method, it’s still polymorphism—2 different implementations (in different classes) that bear the same name

28-Dec-04polymorhism.ppt8 legal assignments widening a number is legal narrowing a number is illegal (unless you cast) class Test { public static void main(String args[]) { double d; int i; d = 5; // legal i = 3.5; // illegal i = (int) 3.5; // legal } }

28-Dec-04polymorhism.ppt9 legal method calls legal because parameter transmission is equivalent to assignment myPrint(5) is like double d = 5; System.out.println(d); class Test { public static void main(String args[]) { myPrint(5); } static void myPrint(double d) { System.out.println(d); } } 5.0

28-Dec-04polymorhism.ppt10 illegal method calls illegal because parameter transmission is equivalent to assignment myPrint(5.0) is like int i = 5.0; System.out.println(i); class Test { public static void main(String args[]) { myPrint(5.0); } static void myPrint(int i) { System.out.println(i); } } myPrint(int) in Test cannot be applied to (double)

28-Dec-04polymorhism.ppt11 Java uses the most specific method class Test { public static void main(String args[]) { myPrint(5); myPrint(5.0); } static void myPrint(double d) { System.out.println("double: " + d); } static void myPrint(int i) { System.out.println("int: " + i); } } int:5 double: 5.0

28-Dec-04polymorhism.ppt12 multiple constructors I You can “overload” constructors as well as methods: Counter() { count = 0; } Counter(int start) { count = start; }

28-Dec-04polymorhism.ppt13 multiple constructors II one constructor can “call” another constructor in the same class, but there are special rules you call the other constructor with the keyword this the call must be the very first thing the constructor does Point(int x, int y) { this.x = x; this.y = y; sum = x + y; } Point() { this(0, 0); } a common reason for overloading constructors is (as above) to provide default values for missing parameters

28-Dec-04polymorhism.ppt14 superclass construction I the very first thing any constructor does, automatically, is call the default constructor for its superclass class Foo extends Bar { Foo() { // constructor super(); // invisible call to superclass constructor... you can replace this with a call to a specific superclass constructor use the keyword super this must be the very first thing the constructor does class Foo extends Bar { Foo(String name) { // constructor super(name, 5); // explicit call to superclass constructor...

28-Dec-04polymorhism.ppt15 superclass construction II Unless you specify otherwise, every constructor calls the default constructor for its superclass class Foo extends Bar { Foo() { // constructor super(); // invisible call to superclass constructor... You can use this(...) to call another constructor in the same class: class Foo extends Bar { Foo(String message) { // constructor this(message, 0, 0); // your explicit call to another constructor... You can use super(...) to call a specific superclass constructor class Foo extends Bar { Foo(String name) { // constructor super(name, 5); // your explicit call to some superclass constructor... Since the call to another constructor must be the very first thing you do in the constructor, you can only do one of the above

28-Dec-04polymorhism.ppt16 shadowing This is called shadowing— name in class Dog shadows name in class Animal class Animal { String name = "Animal"; public static void main(String args[]) { Animal animal = new Animal(); Dog dog = new Dog(); System.out.println(animal.name + " " + dog.name); } } public class Dog extends Animal { String name = "Dog"; } Animal Dog

28-Dec-04polymorhism.ppt17 overriding this is called overriding a method method print in Dog overrides method print in Animal a subclass variable can shadow a superclass variable, but a subclass method can override a superclass method class Animal { public static void main(String args[]) { Animal animal = new Animal(); Dog dog = new Dog(); animal.print(); dog.print(); } void print() { System.out.println("Superclass Animal"); } } public class Dog extends Animal { void print() { System.out.println("Subclass Dog"); } } superclass Animal subclass Dog

28-Dec-04polymorhism.ppt18 how to override a method create a method in a subclass having the same signature as a method in a superclass that is, create a method in a subclass having the same name and the same number and types of parameters parameter names don’t matter, just their types restrictions: the return type must be the same the overriding method cannot be more private than the method it overrides

28-Dec-04polymorhism.ppt19 why override a method? Dog dog = new Dog(); System.out.println(dog); prints something like the println method calls the toString method, which is defined in Java’s top-level Object class hence, every object can be printed (though it might not look pretty) Java’s method public String toString() can be overridden if you add to class Dog the following: public String toString() { return name; } then System.out.println(dog); will print the dog’s name, which may be something like: Fido

28-Dec-04polymorhism.ppt20 equality consider these two assignments: Thing thing1 = new Thing(); Thing thing2 = new Thing(); are these two “Things” equal? that’s up to the programmer! but consider: Thing thing3 = new Thing(); Thing thing4 = thing3; are these two “Things” equal? yes, because they are referring to the same Thing in memory!

28-Dec-04polymorhism.ppt21 the equals method primitives can always be tested for equality with == for objects, == tests whether the two are the same object two strings "abc" and "abc" may or may not be == ! objects can be tested with the method public boolean equals(Object o) in java.lang. unless overridden, this method just uses == it is overridden in the class String it is not overridden for arrays; == tests if its operands are the same array morals: never use == to test equality of Strings or arrays or other objects use equals for String s, java. util.Arrays.equals(a1, a2) for arrays if you test your own objects for equality, override equals

28-Dec-04polymorhism.ppt22 calling an overridden method when your class overrides an inherited method, it basically “hides” the inherited method within this class (but not any other), you can still call the overridden method, by prefixing the call with super. example: super.printEverything(); you would most likely do this in order to observe the DRY principle the superclass method will do most of the work, but you add to it or adjust its results this isn’t a call to a constructor, and can occur anywhere in your class (it doesn’t have to be first)

28-Dec-04polymorhism.ppt23 summary You should overload a method when you want to do essentially the same thing, but with different parameters You should override an inherited method if you want to do something slightly different than in the superclass It’s almost always a good idea to override public void toString() - - it’s handy for debugging, and for many other reasons To test your own objects for equality, override public void equals(Object o) There are special methods (in java.util.Arrays ) that you can use for testing array equality You should never intentionally shadow a variable

28-Dec-04polymorhism.ppt24 the end