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.

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

Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
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.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
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.
Access to Names Namespaces, Scopes, Access privileges.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
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.
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.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
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.
CSC 142 Computer Science II Zhen Jiang West Chester University
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.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
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.
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.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
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.
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.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
BY:- TOPS Technologies
Coming up Which methods are where? – Overriding Calling super’s methods Coupling and cohesion.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
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.
Polymorphism 28-Nov-18.
Java Inheritance.
Object Oriented Programming in JAVA
Namespaces, Scopes, Access privileges
Chapter 14 Abstract Classes and Interfaces
Inheritance in Java CS 3331 Fall 2009.
Polymorphism 15-Apr-19.
Polymorphism 21-Apr-19.
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Chengyu Sun California State University, Los Angeles
Presentation transcript:

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 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!

3 Polymorphism Polymorphism means many (poly) shapes (morph) In Java, polymorphism refers to the fact that you can have multiple methods with the same name in the same class There are two kinds of polymorphism: Overloading Two or more methods with different signatures Overriding Replacing an inherited method with another having the same signature

4 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

5 Why overload a method? So you can supply defaults for the parameters: int increment(int amount) { count = count + amount; return count; } int increment() { return increment(1); } Notice that one method can call another of the same name So you can supply additional information: void printResults() { System.out.println("myArray = " + Arrays.toString(myArray)); } void printResult(String message) { System.out.println(message + ": "); printResults(); }

6 DRY (Don’t Repeat Yourself) When you overload a method with another, very similar method, only one of them 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(); }

7 Another reason to overload methods 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

8 Legal assignments Widening is legal Narrowing 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 } }

9 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

10 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)

11 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

12 Multiple constructors I You can “overload” constructors as well as methods: Counter() { count = 0; } Counter(int start) { count = start; }

13 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

14 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...

15 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

16 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

17 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

18 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

19 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

20 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)

21 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 You should never intentionally shadow a variable

22 The End