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

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

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

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

Programming Language Concepts: Lecture 10

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

Java Object Oriented Design. CSC207 Fall 2014

CSE 143 Lecture 20. Circle

C++ Inheritance and Encapsulation

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

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

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

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

Chapter 14 Abstract Classes and Interfaces

25. Generic Programming

Advanced Programming Generics Collections

Programming Language Concepts: Lecture 2

CS111: PROGRAMMING LANGUAGE II

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

Inheritance and Overloading. Week 11

Polymorphism: Interfaces and Iteration. Fundamentals of Computer Science

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

Programming Language Concepts: Lecture 1

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

CS 320 Introduction to Software Engineering Spring March

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

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

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

Programming Language Concepts: Lecture 7

Chapter 10 Classes Continued. Fundamentals of Java

Exercise 8 Parametric polymorphism November 18, 2016

Programming Language Concepts: Lecture 9

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

CST242 Object-Oriented Programming Page 1

CS-202 Introduction to Object Oriented Programming

Inheritance, Polymorphism, and Interfaces

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

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

ECE 3574: Dynamic Polymorphism using Inheritance

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)

Functional Programming in Haskell Part I : Basics

C# Adds Useful Features

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

What is Inheritance?

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

COMP 250. inheritance (cont.) interfaces abstract classes

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

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Practice for Chapter 11

Lecture 4: Extending Classes. Concept

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

COMP322 - Introduction to C++

MIT AITI Lecture 18 Collections - Part 1

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

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

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

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

INHERITANCE WITH JAVA INTERFACES

CS445 Week 9: Lecture

CS 520 Theory and Practice of Software Engineering Fall 2017

Object-oriented Programming. Object-oriented Programming

Inheritance and Polymorphism

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

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

CS 251 Intermediate Programming Inheritance

Polymorphism and Interfaces. CGS 3416 Spring 2018

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

CSE 331. Generics (Parametric Polymorphism)

Programming Languages and Techniques (CIS120)

OBJECT ORIENTED PROGRAMMING

Lecture 5: Inheritance

Parametric polymorphism and Generics

Object Oriented Programming: Based on slides from Skrien Chapter 2

Announcements/Follow-ups

Example: Count of Points

Java interface Lecture 15

COMP1008 An overview of Polymorphism, Types, Interfaces and Generics

More on Inheritance. Interfaces & Abstract Classes

Introduction to Programming Using Java (98-388)

OBJECT ORIENTED PROGRAMMING

Advanced Placement Computer Science. Inheritance and Polymorphism

COS 226 Midterm Exam, Spring 2009

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Generic types. Announcements. Raw ArrayLists. Generic types (cont.) Creating a raw ArrayList: Accessing a raw ArrayList:

Closed book but one sheet, both sides, of A4 paper is allowed. Section 2.5 of the text Generics in the Java Programming Languages by Gilad Bracha

Exercise: Singleton 1

Inheritance and Polymorphism

CS 11 java track: lecture 3

Array. 9 January 2015 OSU CSE 1

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); 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); 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]; 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; 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){

Polymorphic data structures 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; 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 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]; 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]; 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[] 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! Type erasure Java does not keep record all versions of LinkedList<T> as separate types Cannot write if (s instanceof LinkedList<String>){

Problems with generics We cannot define generic arrays T[] newarray; // Not allowed! We cannot even have LinkedList<Date>[] newarray; Main issue is that arrays are covariant S subtype of T means S[] is compatible with T[] while generic types not S subtype of T does not imply LinkedList<S> is compatible with LinkedList<T> This could create run time type errors ETicket[] elecarr = new ETicket[10]; Ticket[] ticketarr = elecarr; // OK ticketarr[5] = new Ticket(); // Not OK!

Problems with generics The real problem is not but T[] newarray; // Not allowed! T[] newarray; // OK newarray = new T[100]; // Cannot create! An ugly workaround T[] newarray; newarray = (T[]) new Object[100]; that generates a compiler warning but works!