CS111: PROGRAMMING LANGUAGE II

Similar documents
Java How to Program, 8/e

CS111: PROGRAMMING LANGUAGE II

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS111: PROGRAMMING LANGUAGE II

Object-Oriented Programming: Polymorphism

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Programming. Java-Lecture 11 Polymorphism

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Design

BBM 102 Introduction to Programming II Spring Inheritance

CS111: PROGRAMMING LANGUAGE II

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

Inheritance and Polymorphism

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

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Inheritance and object compatibility

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

OVERRIDING. 7/11/2015 Budditha Hettige 82

Java Programming Lecture 7

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

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Inheritance and Polymorphism. CS180 Fall 2007

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

final Methods and Classes

What is Inheritance?

First IS-A Relationship: Inheritance

Inheritance -- Introduction

Chapter 7. Inheritance

Introduction to Object-Oriented Programming

Introduction to Computation and Problem Solving

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

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Chapter 5 Object-Oriented Programming

Polymorphism. Arizona State University 1

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Lecture 18 CSE11 Fall 2013 Inheritance

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Polymorphism and Interfaces. CGS 3416 Spring 2018

C++ Polymorphism. Systems Programming

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

25. Generic Programming

Classes and Inheritance Extending Classes, Chapter 5.2

Chapter 10 Classes Continued. Fundamentals of Java

Chapter 14 Abstract Classes and Interfaces

More Relationships Between Classes

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

Inheritance (Outsource: )

Object-Oriented Programming

Programming in C# Inheritance and Polymorphism

CS1020: DATA STRUCTURES AND ALGORITHMS I

Big software. code reuse: The practice of writing program code once and using it in many contexts.

BBM 102 Introduction to Programming II

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

CS105 C++ Lecture 7. More on Classes, Inheritance

Polymorphism. Agenda

25. Interfaces. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Inheritance. Transitivity

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Chapter 6 Reflection

Cpt S 122 Data Structures. Inheritance

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Inheritance Introduction. 9.1 Introduction 361

CSCE3193: Programming Paradigms

VIRTUAL FUNCTIONS Chapter 10

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. February 10, 2017

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

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

C09: Interface and Abstract Class and Method

[ L5P1] Object-Oriented Programming: Advanced Concepts

Starting Out with Java: From Control Structures Through Objects Sixth Edition

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

Example: Count of Points

Lecture 4: Extending Classes. Concept

Check out Polymorphism from SVN. Object & Polymorphism

Java Object Oriented Design. CSC207 Fall 2014

Subtype Polymorphism

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

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

26. Interfaces. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

ECE 122. Engineering Problem Solving with Java

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Inheritance and Polymorphism

CS 112 Introduction to Programming

Admin. q Admin and recap q Class inheritance

Inheritance and Polymorphism

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

WEEK 13 EXAMPLES: POLYMORPHISM

QUESTIONS FOR AVERAGE BLOOMERS

Inheritance. Overview. Chapter 15 & additional topics. Inheritance Introduction. Three different kinds of inheritance

CS 61B Data Structures and Programming Methodology. July 3, 2008 David Sun

Inheritance (continued) Inheritance

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. January 11, 2018

type conversion polymorphism (intro only) Class class

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Transcription:

1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 8(b): Abstract classes & Polymorphism

Lecture Contents 2 Abstract base classes Concrete classes Polymorphic processing Dr. Amal Khalifa, 2014

Case Study: Payroll System 3 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid a fixed weekly salary regardless of the number of hours worked, hourly employees are paid by the hour and receive overtime pay (i.e., 1.5 times their hourly salary rate) for all hours worked in excess of 40 hours, commission employees are paid a percentage of their sales and base-salaried commission employees receive a base salary plus a percentage of their sales. For the current pay period, the company has decided to reward salaried-commission employees by adding 10% to their base salaries. The company wants to write a Java application that performs its payroll calculations polymorphically.

4 Case Study: Payroll System

5 Concrete Subclasses

Polymorphic Processing 6 Array of references to the base class Each reference is instantiated as an object of a concrete class. Dynamic binding different method calls according to actual type!!

7

8

9

10

11

Demonstrating Polymorphic 12 Behavior A superclass object cannot be treated as a subclass object, because a superclass object is not an object of any of its subclasses. The is-a relationship applies only up the hierarchy from a subclass to its direct (and indirect) superclasses, and not down the hierarchy. The Java compiler does allow the assignment of a superclass reference to a subclass variable if you explicitly cast the superclass reference to the subclass type A technique known as downcasting that enables a program to invoke subclass methods that are not in the superclass.

13 Programming Pitfalls

Class Information 14 Every object in Java knows its own class and can access this information through the getclass method, which all classes inherit from class Object. The getclass method returns an object of type Class (from package java.lang), which contains information about the object s type, including its class name. The result of the getclass call is used to invoke getname to get the object s class name.

Abstract Classes and Methods 15 Programmers often write client code that uses only abstract superclass types to reduce client code s dependencies on a range of subclass types. You can write a method with a parameter of an abstract superclass type. When called, such a method can receive an object of any concrete class that directly or indirectly extends the superclass specified as the parameter s type.

Polymorphism Examples (1) 16 Example: Suppose we create a program that simulates the movement of several types of animals for a biological study. Classes Fish, Frog and Bird represent the three types of animals under investigation. Each class extends superclass Animal, which contains a method move and maintains an animal s current location as x-y coordinates. Each subclass implements method move. A program maintains an Animal array containing references to objects of the various Animal subclasses. To simulate the animals movements, the program sends each object the same message once per second namely, move.

Polymorphism Examples (1) 17 Each specific type of Animal responds to a move message in a unique way: a Fish might swim three feet a Frog might jump five feet a Bird might fly ten feet. The program issues the same message (i.e., move) to each animal object, but each object knows how to modify its x-y coordinates appropriately for its specific type of movement. Relying on each object to know how to do the right thing in response to the same method call is the key concept of polymorphism. The same message sent to a variety of objects has many forms of results hence the term polymorphism.

Polymorphism Examples (2) 18 Example: Quadrilaterals If Rectangle is derived from Quadrilateral, then a Rectangle object is a more specific version of a Quadrilateral. Any operation that can be performed on a Quadrilateral can also be performed on a Rectangle. These operations can also be performed on other Quadrilaterals, such as Squares, Parallelograms and Trapezoids. Polymorphism occurs when a program invokes a method through a superclass Quadrilateral variable at execution time, the correct subclass version of the method is called, based on the type of the reference stored in the superclass variable.

Benefits of Polymorphism 19 With polymorphism, we can design and implement systems that are easily extensible New classes can be added with little or no modification to the general portions of the program, as long as the new classes are part of the inheritance hierarchy that the program processes generically. The only parts of a program that must be altered to accommodate new classes are those that require direct knowledge of the new classes that we add to the hierarchy.

final Methods and Classes 20 A final method in a superclass cannot be overridden in a subclass. Methods that are declared private are implicitly final, because it s not possible to override them in a subclass. Methods that are declared static are implicitly final. A final method s declaration can never change, so all subclasses use the same method implementation, and calls to final methods are resolved at compile time this is known as static binding.

final Methods and Classes (Cont.) 21 A final class cannot be a superclass (i.e., a class cannot extend a final class). All methods in a final class are implicitly final. Class String is an example of a final class. If you were allowed to create a subclass of String, objects of that subclass could be used wherever Strings are expected. Since class String cannot be extended, programs that use Strings can rely on the functionality of String objects as specified in the Java API. Making the class final also prevents programmers from creating subclasses that might bypass security restrictions.

22 Programming Pitfalls

23 That s all for today.. Working on Chapter 10..