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

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

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.
OOP: Inheritance By: Lamiaa Said.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
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:
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
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.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Inheritance in Java CS 3331 Fall Outline  Overloading  Inheritance and object initialization  Subtyping  Overriding  Hiding.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
LECTURE 07 Programming using C# Inheritance
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. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
1 Software Construction Lab 4 Classes and Objects in Java Basics of Classes in Java.
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.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
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.
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.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
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 –
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Polymorphism 11-Nov-18.
Polymorphism 11-Nov-18.
Polymorphism 15-Nov-18.
Building Java Programs
Polymorphism 28-Nov-18.
Inherited Classes in Java
Method Overriding in Java
Chapter 11 Inheritance and Polymorphism
Java Inheritance.
Object Oriented Programming in JAVA
class PrintOnetoTen { public static void main(String args[]) {
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
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

26-Jun-15 Polymorphism

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

3 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

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

5 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

6 Why overload a method? Sometimes so you can supply defaults for the parameters int increment() { return increment(1); } Notice that one method can call another of the same name Sometimes so you can supply additional information: int printResult(String message) { System.out.println(message); printResult(); }

7 Multiple constructors You can “overload” constructors as well as methods Counter() { count = 0; } Counter(int start) { count = start; } 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); System.out.println("At origin"); } A common reason for overloading constructors is (as above) to provide default values for missing parameters

8 Superclass construction 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...

9 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

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

11 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

12 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(); } void print() { System.out.println("Superclass Animal"); } } public class Dog extends Animal { void print() { System.out.println("Subclass Dog"); } } Superclass Animal Subclass Dog

13 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 must be the same The overriding method cannot be more private than the method it overrides

14 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

15 The End