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



Similar documents
Inheritance, overloading and overriding

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

History OOP languages Year Language 1967 Simula Smalltalk

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

Introducing Variance into the Java Programming Language DRAFT

Object-Oriented Programming: Polymorphism

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

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

Computing Concepts with Java Essentials

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

Java Interview Questions and Answers

Chapter 1 Fundamentals of Java Programming

Object-Oriented Programming Lecture 2: Classes and Objects

Java Application Developer Certificate Program Competencies

AP Computer Science Java Subset

CS-XXX: Graduate Programming Languages. Lecture 25 Multiple Inheritance and Interfaces. Dan Grossman 2012

Java (12 Weeks) Introduction to Java Programming Language

Java CPD (I) Frans Coenen Department of Computer Science

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

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

Advanced Data Structures

The Interface Concept

Block IQ. Marko Boon Jacques Resing

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

Agile Software Development

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

Building Java Programs

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

LAB4 Making Classes and Objects

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

CS193j, Stanford Handout #10 OOP 3

D06 PROGRAMMING with JAVA

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

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

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

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

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

Fundamentals of Java Programming

Habanero Extreme Scale Software Research Project

Glossary of Object Oriented Terms

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Implementation Aspects of OO-Languages

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Practical Programming Methodology. Michael Buro. Class Inheritance (CMPUT-201)

Introduction to Object-Oriented Programming

Chapter 13 - Inheritance

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

CS170 Lab 11 Abstract Data Types & Objects

CSE 1020 Introduction to Computer Science I A sample nal exam

Programming Languages Featherweight Java David Walker

Object Oriented Software Design

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

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

D06 PROGRAMMING with JAVA

Programmation 2. Introduction à la programmation Java

Java Programming Language

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

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

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

C++ Programming Language

Object Oriented Software Design

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

Sample CSE8A midterm Multiple Choice (circle one)

1. Polymorphism in C++...2

Abstract. a

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

Introduction to Object-Oriented Programming

Introduction to Programming Block Tutorial C/C++

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

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Checking Access to Protected Members in the Java Virtual Machine

Génie Logiciel et Gestion de Projets. Object-Oriented Programming An introduction to Java

Introduction: Abstract Data Types and Java Review

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

Short Introduction to the Concepts of Programming in Java Overview over the most important constructs

Object-Oriented Programming in Java

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Basics of Java Programming Input and the Scanner class

OOP? What is OOP? Why? OOP in a nutshell. Stéphane Ducasse 2.1

Crash Course in Java

PHP Object Oriented Classes and objects

Object Oriented Design

Programming Language Concepts: Lecture Notes

Java Crash Course Part I

More on Objects and Classes

Install Java Development Kit (JDK) 1.8

Object-Oriented Databases

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

DIPLOMADO DE JAVA - OCA

ICOM 4015: Advanced Programming

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

CEC225 COURSE COMPACT

Transcription:

SE 360 Advances in Software Development Object Oriented Development in Java Polymorphism Dr. Senem Kumova Metin Modified lecture notes of Dr. Hüseyin Akcan

Inheritance Object oriented programming languages allow classes to inherit commonly used state and behavior from other classes CODE REUSE How to factor out the commonalities of various classes?? Find out IS-A RELATIONSHIPS A mammal is a vertebrate A dog is a mammal Superclass (parent) contains all the attributes and behaviors that are common to classes that inherit from it (subclasses)

Inheritance : Classification of Vertebrates Arrows represent is-a relationship

Inheritance: A Simple Example

Abstract Classes

Polymorphism Dictionary Definition of Polymorphism: The quality of being able to assume different forms In the context of programming: A program part is polymorphic if it can be used for objects of several types

Polymorphism Polymorphism is tightly coupled to inheritance In inheritance hierarchy, all subclasses inherit the interfaces from their superclass. However each subclass is a separate entitiy, each might require a different response to same message!! Polymorphism supports different responses

Polymorphism : Example public abstract class Shape{ protected double area; public abstract double getarea();} public class Circle extends Shape{ double radius; public Circle(double r) { radius=r;} public double getarea() { area=3.14*radius*radius; return (area) ;} }

Example Paint program Assume you have to write a print program to display the following: circle, rectangle, square You create a class for each of them, and implement a print() method Also you want to keep the runtime objects in a single array, and whenever a refresh is needed, call the print() method of each object in this array How would you do it?

class Shape { Example 1. public void print() { System.out.println("Shape::print"); } } class Triangle extends Shape { public void print(){system.out.println("triangle::print"); } } class Circle extends Shape { public void print(){system.out.println("circle::print"); } } public class Example1 { public static void show(shape s){ s.print(); } public static void main(string[] args) { Triangle t=new Triangle(); Circle c=new Circle(); show(t); show(c); ; } }

Example 1. What happens if print() in class Shape is removed? class Shape {} class Triangle extends Shape { public void print(){system.out.println("triangle::print"); } } class Circle extends Shape { public void print(){system.out.println("circle::print"); } } public class Example1 { public static void show(shape s){ s.print(); } public static void main(string[] args) { Triangle t=new Triangle(); Circle c=new Circle(); show(t); show(c); ; } }

Example 1. What happens if Shape class is an abstract class? abstract class Shape { public abstract void print();} class Triangle extends Shape { public void print(){ System.out.println("Triangle::print"); } } class Circle extends Shape { public void print(){ System.out.println("Circle::print"); } } public class Example1 { } public static void show(shape s){ s.print(); } public static void main(string[] args) { Triangle t=new Triangle(); Circle c=new Circle(); show(t); show(c); }

class Shape { Example 2. public void print() { System.out.println("Shape::print"); } } class Triangle extends Shape { public void print(){system.out.println("triangle::print"); } } class Circle extends Shape { public void print(){system.out.println("circle::print"); } } public class Example2 { public static void show(shape s){ s.print(); } public static void main(string[] args) { } Shape s; s= new Shape(); System.out.println("I am a " +s); show(s); s= new Triangle(); System.out.println("I am a " +s); show(s); s= new Circle(); System.out.println("I am a " +s); show(s); }

Example 3. class Shape { public void print() { System.out.println("Shape::print"); } } class Triangle extends Shape { public void print(){ System.out.println("Triangle::print"); } } class Circle extends Shape { public void print(){ System.out.println("Circle::print"); } } public class Example3 { public static void show(shape[] s){ for (int i=0; i<s.length; i++) { System.out.println("I am a" +s); s[i].print(); } } public static void main(string[] args) { Shape [] sarray= new Shape[2]; sarray[0]= new Triangle(); sarray[1]= new Circle(); show(sarray); }

Method binding Method binding means connecting a method call to the method body Static binding C/C++ Dynamic binding C++/Java/Python Also called polymorphism, late-binding, runtime binding

Method binding (2) Java uses dynamic binding for all method calls Except final (private) and static methods Has a slight performance penalty compared to static binding

Stages of Binding in Java 1. Compiler looks at the declared type of the object and the method name 2. Compiler uses overload resolution policy to determine candidates 3. If the method is private, final or a constructor then static binding is used. 4. When program uses dynamic binding to call a method, then the virtual machine must call the version of the method that is appropriate for the actual type of the object

Dynamic Binding in Java 1. Virtual machine fetches the method table for the actual type of the object 2. Virtual machine looks up the defining class for the method signature 3. Virtual machine calls the method

Advantages Lets you express your design by using the base class and its methods Later you can extend the design as much as you like, add new objects etc. As long as they use the base class methods, they work with methods that accept base class type You can extend your programs without modifying existing code. Assume you add an Executive class later The existing e.getdescription() calls Executive.getDescription method automatically without needing to recompile the existing code

Advantages(2)

Design decisions In order to use the advantages of polymorphism write methods that communicate with the base class directly Lets you extend the design without changing the interface of the base class, or the methods Extend your design by inheriting new types from the base class The base class methods, and methods that communicate with the base class do not change

Base class methods In polymorphism, base class methods are for expressing design They should not be called in a proper design They should redirect the call to the appropriate derived class You can also define base class as abstract class!

Downcasting Converting the base class type to the original type Upcasting is always safe A triangle is a shape How about downcasting?

Downcasting(2) In C++ you use RTTI to find the original type dynamic_cast<...> In Java each cast is safe, so downcasting is also safe Otherwise you get ClassCastException

Example. Downcasting Try to call a subclass method which is not a inherited by a superclass type reference to an object of subclass!! class Shape1 { public void A() { System.out.println("Shape1::A()"); } } class Triangle1 extends Shape1 { public void A() { System.out.println("Triangle1::A()"); } public void B() { System.out.println("Triangle1::B()"); } } public class Downcast { public static void main(string[] args) { Shape1[] sarray= new Shape1[2]; sarray[0]= new Triangle1(); sarray[1]= new Shape1(); sarray[0].a(); //sarray[0].b(); // NOT VALID Every shape is not a triangle ((Triangle1)sArray[0]).B(); } }