Programming Language Concepts: Lecture 5

Similar documents
Programming Language Concepts: Lecture 5

Polymorphic (Generic) Programming in Java

Programming Language Concepts: Lecture 7

Programming Language Concepts: Lecture 6

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.

The list abstract data type defined a number of operations that all list-like objects ought to implement:

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

Properties of an identifier (and the object it represents) may be set at

Programming Language Concepts: Lecture 10

CSE 143 Lecture 20. Circle

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Java Object Oriented Design. CSC207 Fall 2014

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

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

Design to interfaces. Favor composition over inheritance Find what varies and encapsulate it

Programming Language Concepts: Lecture 7

Polymorphism: Interfaces and Iteration. Fundamentals of Computer Science

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Advanced Programming Generics Collections

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

25. Generic Programming

Chapter 10 Classes Continued. Fundamentals of Java

Chapter 14 Abstract Classes and Interfaces

Programming Language Concepts: Lecture 2

CS111: PROGRAMMING LANGUAGE II

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Goals of the Lecture OO Programming Principles

Introducing Java. Introducing Java. Abstract Classes, Interfaces and Enhancement of Polymorphism. Abstract Classes

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Polymorphism. Agenda

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

Inheritance and Overloading. Week 11

CS 320 Introduction to Software Engineering Spring March

C++ Inheritance and Encapsulation

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

MIT AITI Lecture 18 Collections - Part 1

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Programming Language Concepts: Lecture 1

Object-oriented Programming. Object-oriented Programming

CS 520 Theory and Practice of Software Engineering Fall 2017

CS445 Week 9: Lecture

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

Polymorphism and Interfaces. CGS 3416 Spring 2018

Programming Language Concepts: Lecture 9

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism

CS-202 Introduction to Object Oriented Programming

CST242 Object-Oriented Programming Page 1

Inheritance, Polymorphism, and Interfaces

Java interface Lecture 15

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

More on Inheritance. Interfaces & Abstract Classes

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

Generics in Java. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Motivating Example: Observations (1) Generics in Java. Motivating Example: A Book of Objects. Motivating Example: Observations (2)

Lecture 4: Extending Classes. Concept

ECE 3574: Dynamic Polymorphism using Inheritance

Inheritance and Polymorphism

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

OBJECT ORIENTED PROGRAMMING

COS 226 Midterm Exam, Spring 2009

C# Adds Useful Features

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Exercise 8 Parametric polymorphism November 18, 2016

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

import java.util.*; private String name; private double salary; private int hireday; private int hiremonth; private int hireyear;

Inheritance and Polymorphism

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Interfaces and itera-on. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

What is Inheritance?

Quiz. Programming Languages. CSE 130 : Fall Lecture 16: Static Types for Objects. Ranjit Jhala UC San Diego

Functional Programming in Haskell Part I : Basics

Topics. Modularity and Object-Oriented Programming. Dijkstra s Example (1969) Stepwise Refinement. Modular program development

CS 520 Theory and Practice of Software Engineering Fall 2018

COMP 250. inheritance (cont.) interfaces abstract classes

Lecture Outline. Parametric Polymorphism and Java Generics. Polymorphism. Polymorphism

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Inheritance, and Polymorphism.

OBJECT ORIENTED PROGRAMMING. Abstract Class And Interface

Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Parametric types. aka: what s up with those a???

Parametric polymorphism and Generics

Practice for Chapter 11

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Programmieren II. Polymorphism. Alexander Fraser. June 4, (Based on material from T. Bögel)

11/2/09. Code Critique. What goal are we designing to? What is the typical fix for code smells? Refactoring Liskov Substitution Principle

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 6, January 28, 2016

Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Thursday, April 25, 13

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

G Programming Languages - Fall 2012

Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

1/ COP 3503 FALL 2012 SHAYAN JAVED LECTURE 16. Programming Fundamentals using Java

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

On Polymorphism and the Open-Closed Principle

Example: Count of Points

Advanced Placement Computer Science. Inheritance and Polymorphism

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Programming Languages and Techniques (CIS120)

Transcription:

Programming Language Concepts: Lecture 5 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in PLC 2011, Lecture 5, 18 January, 2011

Abstract classes and interfaces Use abstract functions to specify common properties Abstract definition of perimeter() for all Shapes public abstract double perimeter(); Classes with abstract functions must themselves be abstract Cannot create objects of abstract type but we can define and use variables of abstract type Shape sarr[] = new Shape[3]; Circle c = new Circle(); sarr[0] = c; Square s = new Square(); sarr[1] = s; Rectangle r = new Rectangle(); sarr[2] = r; for (i = 0; i < 2; i++){ size = sarr[i].perimeter();

Generic functions Use abstract classes to specify capabilities abstract class Comparable{ public abstract int cmp(comparable s); // return -1 if this < s, 0 if this == 0, // +1 if this > s Now we can sort any array of objects that extend Comparable class Sortfunctions{ public static void quicksort(comparable[] a){ // Usual code for quicksort, except that // to compare a[i] and a[j] we use a[i].cmp(a[j])

Mutiple inheritance and interfaces How do can we sort Circle objects? Circle already extends Shape Java does not allow Circle to also extend Comparable! An interface is an abstract class with no concrete components interface Comparable{ public abstract int cmp(comparable s); A class that extends an interface is said to implement it: class Circle extends Shape implements Comparable{ public double perimeter(){ public int cmp(comparable s){ Can implement multiple interfaces

Generic programming Java s tree-like hierarchy with Object at root allows polymorphic functions public int find (Object[] objarr, Object o){ int i; for (i = 0; i < objarr.length; i++){ if (objarr[i] == o) {return i; return (-1);

Generic programming Java s tree-like hierarchy with Object at root allows polymorphic functions public int find (Object[] objarr, Object o){ int i; for (i = 0; i < objarr.length; i++){ if (objarr[i] == o) {return i; return (-1); What if we wanted to swap two objects? public static void swap (Object x, Object y){ Object temp = x; x = y; y = temp;

Generic programming What happens if we write the following? Date d = new Date(); Circle c = new Circle ();.. swap(c,d);

Generic programming What happens if we write the following? Date d = new Date(); Circle c = new Circle ();.. swap(c,d); Type error at run time!

Generic programming A generic function to copy arrays public static void arraycopy (Object[] src, Object[] tgt){ int i,limit; limit = Math.min(src.length,tgt.length); for (i = 0; i < limit; i++){ tgt[i] = src[i];

Generic programming A generic function to copy arrays public static void arraycopy (Object[] src, Object[] tgt){ int i,limit; limit = Math.min(src.length,tgt.length); for (i = 0; i < limit; i++){ tgt[i] = src[i]; Given the following definitions Ticket[] ticketarr = new Ticket[10]; ETicket[] elecarr = new ETicket[10]; arraycopy(elecarr,ticketarr); arraycopy(ticketarr,elecarr);

Polymorphic data structures Polymorphic lists public class Node { public Object data; public Node next;

Polymorphic data structures Polymorphic lists public class Node { public Object data; public Node next; public class LinkedList{ private int size; private Node first; public Object head(){ Object returnval = null; if (first!= null){ returnval = first.data; first = first.next; return returnval; public void insert(object newdata){

Two problems Type information is lost, need casts LinkedList list = new LinkedList(); Ticket t1,t2; t1 = new Ticket(); list.insert(t1); t2 = (Ticket)(list.head()); // head() returns an Object

Two problems Type information is lost, need casts LinkedList list = new LinkedList(); Ticket t1,t2; t1 = new Ticket(); list.insert(t1); t2 = (Ticket)(list.head()); // head() returns an Object List need not be homogenous! LinkedList list = new LinkedList(); Ticket t = new Ticket(); Date d = new Date(); list.insert(t); list.insert(d);

Java generics Use type variables public class Node<T> { public T data; public Node next;

Java generics Use type variables public class Node<T> { public T data; public Node next; public class LinkedList<T>{ private int size; private Node first; public T head(){ T returnval = null; if (first!= null){ returnval = first.data; first = first.next; return returnval; public void insert(t newdata){

Java generics Not quite! public class Node<T> { public T data; public Node next; public class LinkedList<T>{ private int size; private Node first; public T head(){ T returnval = null; if (first!= null){ returnval = first.data; first = first.next; return (T) returnval; // Cast!! public void insert(t newdata){

Java generics Instantiate generic classes using concrete type LinkedList<Ticket> ticketlist = new LinkedList<Ticket>(); LinkedList<Date> datelist = new LinkedList<Date>(); Ticket t = new Ticket(); Date d = new Date(); ticketlist.insert(t); datelist.insert(d);

Polymorphic functions A better arraycopy public <T> void arraycopy (T[] src, T[] tgt){ int i,limit; limit = min(src.length,tgt.length); // No overflows! for (i = 0; i < limit; i++){ tgt[i] = src[i];

Polymorphic functions A better arraycopy public <T> void arraycopy (T[] src, T[] tgt){ int i,limit; limit = min(src.length,tgt.length); // No overflows! for (i = 0; i < limit; i++){ tgt[i] = src[i]; Beware a type variable may get hidden public <T> T head(){ T returnval; return returnval;

Dependent type variables Can we copy arrays of one type to another? public <S,T> void arraycopy (S[] src, T[] tgt){ int i,limit; limit = min(src.length,tgt.length); // No overflows! for (i = 0; i < limit; i++){ tgt[i] = src[i];

Dependent type variables Can we copy arrays of one type to another? public <S,T> void arraycopy (S[] src, T[] tgt){ int i,limit; limit = min(src.length,tgt.length); // No overflows! for (i = 0; i < limit; i++){ tgt[i] = src[i]; Instead public <S extends T,T> void arraycopy (S[] src, T[] tgt){

Dependent type variables Can we copy arrays of one type to another? public <S,T> void arraycopy (S[] src, T[] tgt){ int i,limit; limit = min(src.length,tgt.length); // No overflows! for (i = 0; i < limit; i++){ tgt[i] = src[i]; Instead public <S extends T,T> void arraycopy (S[] src, T[] tgt){ A more generous polymorphic list public <S extends T> void insert(s newdata){

The covariance problem If S is compatible with T, S[] is compatible with T[] ETicket[] elecarr = new ETicket[10]; Ticket[] ticketarr = elecarr; // OK. ETicket[] is a subtype of Ticket[]

The covariance problem If S is compatible with T, S[] is compatible with T[] ETicket[] elecarr = new ETicket[10]; Ticket[] ticketarr = elecarr; // OK. ETicket[] is a subtype of Ticket[] But ticketarr[5] = new Ticket(); // Not OK. ticketarr[5] refers to an ETicket!

The covariance problem If S is compatible with T, S[] is compatible with T[] ETicket[] elecarr = new ETicket[10]; Ticket[] ticketarr = elecarr; // OK. ETicket[] is a subtype of Ticket[] But ticketarr[5] = new Ticket(); // Not OK. ticketarr[5] refers to an ETicket! Again a type error at run time!

The covariance problem If S is compatible with T, S[] is compatible with T[] ETicket[] elecarr = new ETicket[10]; Ticket[] ticketarr = elecarr; // OK. ETicket[] is a subtype of Ticket[] But ticketarr[5] = new Ticket(); // Not OK. ticketarr[5] refers to an ETicket! Again a type error at run time! Generic classes are not covariant LinkedList<String> is not compatible with LinkedList<Object>

Problems with generics The following does not work if (s instanceof T){ // T a type variable Cannot use a type variable wherever a type is expected!

Problems with generics The following does not work if (s instanceof T){ // T a type variable Cannot use a type variable wherever a type is expected! Cannot define generic arrays T[] newarray; // Not allowed!

Problems with generics The following does not work if (s instanceof T){ // T a type variable Cannot use a type variable wherever a type is expected! Cannot define generic arrays T[] newarray; // Not allowed! Type erasure Java does not keep record all versions of LinkedList<T> as separate types Cannot write if (s instanceof LinkedList<String>){