Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
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.
1. 2 Introduction to Inheritance  Access Modifiers  Methods in Subclasses  Method Overriding  Converting Class Types  Why up-cast?  Why down-cast?
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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:
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.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
LECTURE 07 Programming using C# Inheritance
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
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.
Intro to OOP with Java, C. Thomas Wu
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Computer Science [3] Java Programming II - Laboratory Course Lab 1 + 2: Review inheritance & abstract Review inheritance & abstractPolymorphism Faculty.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided.
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.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
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.,
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
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.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
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.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
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
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Chapter 11 Inheritance and Polymorphism
Interface.
Week 8 Lecture -3 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Polymorphism 11-Nov-18.
Polymorphism 11-Nov-18.
OOP’S Concepts in C#.Net
CSC 205 Java Programming II
Polymorphism 15-Nov-18.
Extending Classes.
Polymorphism 28-Nov-18.
Week 6 Object-Oriented Programming (2): Polymorphism
Inherited Classes in Java
Object-Oriented Programming
Method Overriding in Java
Java Inheritance.
Object Oriented Programming in JAVA
class PrintOnetoTen { public static void main(String args[]) {
Polymorphism 15-Apr-19.
Polymorphism 21-Apr-19.
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

Polymorphism

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

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

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)

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) { System.out.println("double d = " + d); } } int i = 5 double d = 5.0

Why overload a method? Sometimes so you can supply defaults for the parameters int increment() { return increment(1); } Sometimes so you can supply additional information: int printResult(String message) { System.out.println(message); printResult(); }

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 (which you have just seen) Two or more methods with different signatures –Overriding (which you will see shortly) Replacing an inherited method with another having the same signature

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!

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

Overriding This is called overriding a method Method print in Dog overrides method print in Animal class Animal { public static void main(String args[]) { Animal animal = new Animal(); Dog dog = new Dog(); animal.print(); dog.print(); } static void print() { System.out.println("Superclass Animal"); } } public class Dog extends Animal { static void print() { System.out.println("Subclass Dog"); } } Superclass Animal Subclass Dog

How to override a method Create a method in a subclass having the same name and the same number and types of parameters –Parameter names don’t matter –The return type should be the same The overriding method cannot be more private than the method it overrides

Why override a method? Dog dog = new dog(); System.out.println(dog); –Prints something like Add to class Dog the following: public String toString() { return name; } Now System.out.println(dog); prints something like: Fido

The End