Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Similar documents
Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CS111: PROGRAMMING LANGUAGE II

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Java How to Program, 8/e

Chapter 14 Abstract Classes and Interfaces

C09: Interface and Abstract Class and Method

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

CS111: PROGRAMMING LANGUAGE II

What is Inheritance?

C18a: Abstract Class and Method

Polymorphism. return a.doublevalue() + b.doublevalue();

Polymorphism and Interfaces. CGS 3416 Spring 2018

COE318 Lecture Notes Week 9 (Week of Oct 29, 2012)

OVERRIDING. 7/11/2015 Budditha Hettige 82

Object Oriented Design

type conversion polymorphism (intro only) Class class

COMP 250. Lecture 32. polymorphism. Nov. 25, 2016

Check out Polymorphism from SVN. Object & Polymorphism

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object. OutputStream write(int) write(byte[]) write(byte[], int, int) FilterOutputStream write(int) write(byte[]) write(byte[], int, int)

Lesson 10. Work with Interfaces and Abstract Classes. Copyright all rights reserved

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Lecture 5: Inheritance

index.pdf January 21,

ITI Introduction to Computing II

ITI Introduction to Computing II

COE318 Lecture Notes Week 8 (Oct 24, 2011)

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

More On inheritance. What you can do in subclass regarding methods:

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

25. Generic Programming

Lecture 9 : Basics of Reflection in Java

JAVA MOCK TEST JAVA MOCK TEST II

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Introduction to Object-Oriented Programming

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Java Magistère BFA

Inheritance (Outsource: )

CH. 2 OBJECT-ORIENTED PROGRAMMING

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

VIRTUAL FUNCTIONS Chapter 10

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Unit 4 - Inheritance, Packages & Interfaces

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

ITI Introduction to Computing II

Abstract Classes and Interfaces

final Methods and Classes

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Chapter 9 Inheritance

Programming Exercise 14: Inheritance and Polymorphism

Polymorphism. Arizona State University 1

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

ITI Introduction to Computing II

Inheritance & Polymorphism

Inheritance, Polymorphism, and Interfaces

Chapter 9 - Object-Oriented Programming: Polymorphism

INHERITANCE. Spring 2019

Practice for Chapter 11

Inheritance (continued) Inheritance

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

OBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616

Object Oriented Programming: Based on slides from Skrien Chapter 2

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

CLASS DESIGN. Objectives MODULE 4

CMSC330 Fall 2013 Practice Problems 6 Solutions

CS112 Lecture: Inheritance and Polymorphism

Object Model. Object Oriented Programming Spring 2015

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

CMSC 132: Object-Oriented Programming II

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Subclasses, Superclasses, and Inheritance

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Object Orientated Analysis and Design. Benjamin Kenwright

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

The class Object. Lecture CS1122 Summer 2008

Class object initialization block destructor Class object

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

First IS-A Relationship: Inheritance

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

MIT Semantic Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

CS 251 Intermediate Programming Inheritance

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

More on Inheritance. Interfaces & Abstract Classes

Inheritance. Transitivity

Inheritance and Interfaces

Object-Oriented Programming

Class, Variable, Constructor, Object, Method Questions

Programming Languages and Techniques (CIS120)

Informatik II Tutorial 6. Subho Shankar Basu

Inheritance and object compatibility

Media Computation. Lecture 16.1, December 8, 2008 Steve Harrison

Informatik II (D-ITET) Tutorial 6

Transcription:

Polymorphism, Dynamic Binding and Interface 2 4 pm Thursday 7/31/2008 @JD2211 1 Announcement Next week is off The class will continue on Tuesday, 12 th August 2 Agenda Review Inheritance Abstract Array Polymorphism Dynamic binding Interface 3 1

Review - Inheritance public class SalariedEmployee extends Employee { } 4 Review - Abstract public abstract class Employee{ public abstract double earnings(); } 5 Review - Array Employee[6] refers to the seventh Employee reference in the array Employee In Java, an array is an object that must be instantiated Employee employees[] = new Employee[4]; 6 2

Polymorphism The term polymorphism can be defined as having many forms. A polymorphic reference is a reference variable that can refer to different types of objects at different points in time. 7 Polymorphism (cont d) For example, if the reference employee is polymorphic, it can refer to different types of employee at different time. If the employee reference is in a loop, that line of code could call a different version of earnings method each time it is invoked 8 Binding At some point, the commitment is made to execute certain code to carry out a method invocation. This commitment is referred to as binding a method invocation to a method definition In many situations, the binding of a method invocation to a method definition can occur at compile time 9 3

Static Binding In many situations, ti the binding of a method invocation to a method definition can occur at compile time 10 Dynamic Binding For polymorphic references, however, the decision cannot be made until run time. The method definition iti that t is used is based on the object that is being referred to by the reference variable at that moment This deferred commitment is called dynamic binding 11 Static and Dynamic Binding Dynamic binding is less efficient than binding at compile time because the decision must be made during the execution of the program This overhead is generally acceptable in light of flexibility that a polymorphic reference provides 12 4

instanceof It checks an object reference if that defined by a class or its subclasses and returns a boolean value; <object-reference> instanceof ClassName 13 getclass() public final Class getclass() Returns the runtime class of an object http://java.sun.com/j2se/1.5.0/docs/api/java/l ang/object.html getname() String getname() Returns the fully-qualified name of the entity represented by this Class object, as a String. void printclassname(object obj) { System.out.println("The class of " + obj + " is " + obj.getclass().getname()); } http://java.sun.com/j2se/1.3/docs/api/java/la ng/class.html 15 5

Exercise 1 Fill the blanks in each of the following statements: Polymorphism helps eliminates logic. If a class contains at least one abstract method, it is a(n) abstract. Classes from which objects can be instantiated are called classes. 16 Exercise 2 State whether each of the statements that follows is true of false. If false, explain why. It is possible to treat superclass objects and subclass objects similarly. All methods in an abstract class must be declared as abstract methods Referring to a subclass object with a superclass variable is dangerous 17 Exercise 3 State whether each of the statements that follows is true of false. If false, explain why. A class is made abstract by declaring it abstract If a superclass declares an abstract method, a subclass must implement that method to become a concrete class 18 6

Interface A Java interface is a collection class constants and abstract methods. An interface is typically used in place of an abstract class when there is NO default implementation to inherit i.e. no instance variables and no default method implementation 19 Interface UML Notation «interface>> Shape getarea() : double getvolume() : double getname() : String 20 Interface Class in Java public interface Shape { public double getarea(); public double getvolume(); public String getname(); } 21 7

Interface Class in Java public interface Constants { public static fianl int ONE = 1; public static fianl int TWO = 2; public static fianl int THREE = 3; } 22 Lab Assignment http://www.csun.edu/ edu/~twang/java/lab/7-31-lab.pdf 23 8