Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism



Similar documents
SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Inheritance, overloading and overriding

Java Interview Questions and Answers

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

The Interface Concept

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Polymorphism. Why use polymorphism Upcast revisited (and downcast) Static and dynamic type Dynamic binding. Polymorphism.

Computing Concepts with Java Essentials

Introducing Variance into the Java Programming Language DRAFT

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Java CPD (I) Frans Coenen Department of Computer Science

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

How To Design Software

Part I. Multiple Choice Questions (2 points each):

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Building Java Programs

Java Programming Language

History OOP languages Year Language 1967 Simula Smalltalk

LAB4 Making Classes and Objects

D06 PROGRAMMING with JAVA

Johannes Sametinger. C. Doppler Laboratory for Software Engineering Johannes Kepler University of Linz A-4040 Linz, Austria

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

An Introduction To UML Class Diagrams Classes

Chapter 13 - Inheritance

AP Computer Science Java Subset

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Abstract Class & Java Interface

AP Computer Science A - Syllabus Overview of AP Computer Science A Computer Facilities

Classifying Lesson 1 Triangles

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Using Inheritance and Polymorphism

Advanced Data Structures

Using Files as Input/Output in Java 5.0 Applications

java Features Version April 19, 2013 by Thorsten Kracht

Compile-time type versus run-time type. Consider the parameter to this function:

5. Advanced Object-Oriented Programming Language-Oriented Programming

CS 111 Classes I 1. Software Organization View to this point:

Instance Creation. Chapter

Manual For Using the NetBeans IDE

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

Approach of Unit testing with the help of JUnit

CS193j, Stanford Handout #10 OOP 3

Programming Methods & Java Examples

Object-Oriented Programming: Polymorphism

Specialized Programme on Web Application Development using Open Source Tools

Java Application Developer Certificate Program Competencies

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

Surface Area Quick Review: CH 5

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Lab Experience 17. Programming Language Translation

Introduction to Object-Oriented Programming

STATIC VARIABLE/ METHODS, INHERITANCE, INTERFACE AND COMMAND LINE ARGUMENTS

Introduction: Abstract Data Types and Java Review

Java Basics: Data Types, Variables, and Loops

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Description of Class Mutation Mutation Operators for Java

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

11 November

Measurement. Volume It All Stacks Up. Activity:

ECE 122. Engineering Problem Solving with Java

a. Inheritance b. Abstraction 1. Explain the following OOPS concepts with an example

Introduction to Java. CS 3: Computer Programming in Java

Habanero Extreme Scale Software Research Project

15-214: Principles of Software Construction 8 th March 2012

LAB 1. Familiarization of Rational Rose Environment And UML for small Java Application Development

Object Oriented Programming and the Objective-C Programming Language 1.0. (Retired Document)

AP Computer Science Static Methods, Strings, User Input

Software Engineering Techniques

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

J a v a Quiz (Unit 3, Test 0 Practice)

8 th Grade Task 2 Rugs

Construction of classes with classes

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

16.1 DataFlavor DataFlavor Methods. Variables

Object-Oriented Programming in Java

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

Java SE 8 Programming

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

ATM Case Study OBJECTIVES Pearson Education, Inc. All rights reserved Pearson Education, Inc. All rights reserved.

Software. Programming Language. Software. Instructor Özgür ZEYDAN. Bülent Ecevit University Department of Environmental Engineering

ICOM 4015: Advanced Programming

Object Oriented Design

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Sample CSE8A midterm Multiple Choice (circle one)

Konzepte objektorientierter Programmierung

Fundamentals of Java Programming

1.1.3 Syntax The syntax for creating a derived class is very simple. (You will wish everything else about it were so simple though.

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Entity-Relationship Model. Purpose of E/R Model. Entity Sets

Programming Languages Featherweight Java David Walker

1. Kyle stacks 30 sheets of paper as shown to the right. Each sheet weighs about 5 g. How can you find the weight of the whole stack?

Transcription:

Polymorphism 1

Agenda What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism 2

What is & Why Polymorphism? 3

What is Polymorphism? Generally, polymorphism refers to the ability to appear in many forms Polymorphism in a Java program The ability of a reference variable to change behavior according to what object instance it is holding. This allows multiple objects of different subclasses to be treated as objects of a single super class, while automatically selecting the proper methods to apply to a particular object based on the subclass it belongs to 4

Polymorphism Example For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area method to it will return the correct results. 5

Examples of Polymorphic Behavior in Java Programs 6

Example #1: Polymorphism Given the parent class Person and the child class Student, we add another subclass of Person which is Employee. Below is the class hierarchy 7

Example #1: Polymorphism In Java, we can create a reference that is of type super class, Person, to an object of its subclass, Student. public static main( String[] args ) { Student studentobject = new Student(); Employee employeeobject = new Employee(); Person ref = studentobject; // Person reference points // to a Student object } // Calling getname() of the Student object instance String name = ref.getname(); 8

Example #1: Polymorphism Now suppose we have a getname method in our super class Person, and we override this method in both Student and Employee subclass's public class Student { public String getname(){ System.out.println( Student Name: + name); return name; } } public class Employee { public String getname(){ System.out.println( Employee Name: + name); return name; } } 9

Example #1: Polymorphism Going back to our main method, when we try to call the getname method of the reference Person ref, the getname method of the Student object will be called. Now, if we assign ref to an Employee object, the getname method of Employee will be called. 10

Example #1: Polymorphism 1 public static main( String[] args ) { 2 3 Student studentobject = new Student(); 4 Employee employeeobject = new Employee(); 5 6 Person ref = studentobject; //Person ref. points to a 7 // Student object 8 9 // getname() method of Student class is called 10 String temp= ref.getname(); 11 System.out.println( temp ); 12 13 ref = employeeobject; //Person ref. points to an 14 // Employee object 15 16 //getname() method of Employee class is called 17 String temp = ref.getname(); 18 System.out.println( temp ); 19 } 11

Example #2: Polymorphism Another example that illustrates polymorphism is when we try to pass a reference to methods as a parameter Suppose we have a static method printinformation that takes in a Person reference as parameter. public static printinformation( Person p ){ // It will call getname() method of the // actual object instance that is passed p.getname(); } 12

Example #2: Polymorphism We can actually pass a reference of type Employee and type Student to the printinformation method as long as it is a subclass of the Person class. public static main( String[] args ){ Student Employee studentobject = new Student(); employeeobject = new Employee(); printinformation( studentobject ); } printinformation( employeeobject ); 13

Benefits of Polymorphism 14

Benefits of Polymorphism Simplicity If you need to write code that deals with a family of types, the code can ignore type-specific details and just interact with the base type of the family Even though the code thinks it is using an object of the base class, the object's class could actually be the base class or any one of its subclasses This makes your code easier for you to write and easier for others to understand 15

Benefits of Polymorphism Extensibility Other subclasses could be added later to the family of types, and objects of those new subclasses would also work with the existing code 16

3 Forms of Polymorphism 17

3 Forms of Polymorphism in Java program Method overriding Methods of a subclass override the methods of a superclass Method overriding (implementation) of the abstract methods Methods of a subclass implement the abstract methods of an abstract class Method overriding (implementation) through the Java interface Methods of a concrete class implement the methods of the interface 18

Polymorphism 19