COMP1008 An overview of Polymorphism, Types, Interfaces and Generics

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

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

CS 251 Intermediate Programming Inheritance

CS 520 Theory and Practice of Software Engineering Fall 2018

Java Brand Generics. Advanced Topics in Java. Khalid Azim Mughal Version date:

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Inheritance and object compatibility

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

CS-202 Introduction to Object Oriented Programming

Inheritance, Polymorphism, and Interfaces

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Inheritance and Interfaces

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

COP 3330 Final Exam Review

Rules and syntax for inheritance. The boring stuff

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Introduction to Inheritance

Polymorphism & A Few Java Interfaces

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Abstract Classes and Interfaces

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

CSE 143 Lecture 20. Circle

First IS-A Relationship: Inheritance

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

More Relationships Between Classes

Error Handling. public int divide(double a, double b) { if (b==0) return -1; // error double result = a/b; return 0; // success }

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

CS 520 Theory and Practice of Software Engineering Fall 2017

Inheritance and Polymorphism

Self-review Questions

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CS 320 Introduction to Software Engineering Spring 2017

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Introduction to Programming Using Java (98-388)

Practice for Chapter 11

Super-Classes and sub-classes

Chapter 14 Abstract Classes and Interfaces

Java Object Oriented Design. CSC207 Fall 2014

Introduction to Object-Oriented Programming

Chapter 5 Object-Oriented Programming

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

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

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

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

Chapter 11 Classes Continued

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

Last class. -More on polymorphism -super -Introduction to interfaces

CH. 2 OBJECT-ORIENTED PROGRAMMING

Interfaces. Relatedness of types. Interface as a contract. The area and perimeter of shapes 7/15/15. Savitch ch. 8.4

CO Java SE 8: Fundamentals

Object- Oriented Analysis, Design and Programming

Inheritance. Transitivity

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Polymorphism. Agenda

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

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

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

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

Operators and Expressions

Inheritance, cont. Notes Chapter 6 and AJ Chapters 7 and 8

Inheritance and Polymorphism

Collections, Maps and Generics

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

Type Analysis. Type Checking vs. Type Inference

Advanced Placement Computer Science. Inheritance and Polymorphism

Object-Oriented Concepts and Design Principles

CS 320 Introduction to Software Engineering Spring March 06, 2017

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

PIC 20A Number, Autoboxing, and Unboxing

ITI Introduction to Computing II

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

Parametric polymorphism and Generics

CS 215 Software Design Sample midterm solutions

Java for Non Majors Spring 2018

ITI Introduction to Computing II

Java Language Features

Chapter 13 Abstract Classes and Interfaces

On the Algorithm for Specializing Java Programs with Generic Types

Object Oriented Programming: Based on slides from Skrien Chapter 2

Distributed Systems Recitation 1. Tamim Jabban

Motivations. Chapter 13 Abstract Classes and Interfaces

Example: Count of Points

Object-Oriented Programming More Inheritance

COURSE 2 DESIGN PATTERNS

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

CSE 331. Generics (Parametric Polymorphism)

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

C# Programming for Developers Course Labs Contents

PROGRAMMING FUNDAMENTALS

Chapter 13 Abstract Classes and Interfaces

public Candy() { ingredients = new ArrayList<String>(); ingredients.add("sugar");

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

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle

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

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

Transcription:

COMP1008 An overview of Polymorphism, Types, Interfaces and Generics Being Object-Oriented Exploiting the combination of: objects classes encapsulation inheritance dynamic binding polymorphism pluggability 2 Polymorphism Where something has multiple forms. A single function that can be applied to multiple types. Generic methods/classes. Ability of objects of different types to respond to same messages (method calls). Allows one section of code to work with multiple values and objects. Share rather than duplicate. Wikipedia has some good articles on polymorphism. 3 Forms of Polymorphism Parametric Polymorphism - generic classes and methods. Subtyping Polymorphism - inheritance Ad-hoc polymorphism Overloading Coercion 4

Polymorphism and Inheritance A superclass can define a common interface. Subclasses inherit the common interface and specialise the corresponding methods. A subclass object can be used where a superclass object has been specified. Remember shapes: Shape myshape = new Square(4,4,10); 5 Pluggability User of common methods User only assumes interface provided by abstract superclass Objects with a common interface. All have a common superclass. 6 Old Code can Call New Code New pluggable components can be added without changing the users of the components. Code designed to use the common interfaces remains unchanged. For example, BankAccount. And specific kinds of bank account. 7 Objects and Types An Object has a state. The values of its instance variables. The overall value of an object is determined by its state. An object also has a type. An object's class determines its type. A class is a user defined type. An object reference has a reference type. Determines what kind of objects it can refer to. 8

Type Conformance But all classes are subclasses of Object (except Object), and a class can have other superclasses (the inheritance chain), so an object can have multiple types. Or to be precise an object can conform to multiple types. Type conformance means that any method declared by a type can be called on an object that conforms to the type. Object <- Shape <- Square Square conforms to both type Shape and type Object. Any public methods declared in Shape and Object can be called on a Square object (and may be overridden). 9 All types conform to type Object Hence, a reference of type reference to Object can refer to any object that conforms to type Object. i.e., all objects And all objects inherit (and may override) the methods declared in class Object. For example, tostring overridden in class Square: public String tostring() return This is a square of size + size;... System.out.println(square); // tostring called here 10 Type Hierarchies Types also have supertype and subtype relationships (like superclass/subclass). The class hierarchy defines part of the type hierarchy. But it gets more interesting... 11 Enter the Interface Types can also be declared using an interface: public interface ShapeIF void draw(graphics g); void move(int x, int y); Specifies the methods a type declares. No method bodies, no instance variables. 12

Implements A class can implement an interface. class MyShape implements ShapeIF // Class must override draw and move methods // or be abstract. Circle denotes interface. Can also use «interface» below name. Dashed line with open triangle denotes implements. 13 Combining Interface and Abstract Class Interface (common methods) Abstract class (shared code) Concrete class (complete code) 14 Using an interface Can now use the interface type with Shape objects: public void drawpicture (ArrayList<ShapeIF> shapes, Graphics g) for (ShapeIF shape : shapes) shape.draw(g); Majority of code written using interface type(s). Can use any object of a class that implements the interface. 15 Interfaces and Class hierarchy Interfaces allow types to be declared independently of classes and the class hierarchy. Class Object Interface 16

Example (from class libraries) interface Comparable int compareto(object o); Remember String compareto? Return value <0, 0 or >0, for less than, equals, greater than. Any class that implements the Comparable interface must provide a compareto method (unless abstract). Objects of the class can be compared. Specifying ability to be compared is independent of class inheritance. 17 Example (2) Classes that implement Comparable: Authenticator.RequestorType, BigDecimal, BigInteger, Boolean, Byte, ByteBuffer, Calendar, Character, CharBuffer, Charset, CollationKey, CompositeName, CompoundName, Date, Date, Double, DoubleBuffer, ElementType, Enum, File, Float, FloatBuffer, FormSubmitEvent.MethodType, GregorianCalendar, IntBuffer, Integer, JTable.PrintMode, KeyRep.Type, LdapName, Long, LongBuffer, MappedByteBuffer, MemoryType, ObjectStreamField, Proxy.Type, Rdn, RetentionPolicy, RoundingMode, Short, ShortBuffer, SSLEngineResult.HandshakeStatus, SSLEngineResult.Status, String, Thread.State, Time, Timestamp, TimeUnit, URI, UUID + any that you write. 18 Example (3) Using Comparable: public void sort(comparable[] a) // Sorting algorithm if (a[i].compareto(a[i+1]) < 0)... //... Method can sort any array of objects that conform to Comparable (where all objects in the array are instances of the same class). 19 Programming to an Interface Use interfaces to declare types needed. Write code using interface types. Use objects of any class that implements interface. Implementing classes can be added, edited, removed independently of code using the interface types. Commonly used and important design/implementation strategy. Decouples concrete representations from abstract specifications. 20

Remember ArrayList... Object Iterable AbstractCollection Collection List Serializable AbstractList Cloneable ArrayList RandomAccess ArrayList conforms to many types. 21 ArrayList declaration class ArrayList extends AbstractList implements Serializable, Cloneable, List, RandomAccess... A class can both extend and implement. One superclass only (extend). But multiple implements. ArrayList is a concrete class so must override all inherited abstract methods and all methods declared in the interfaces. 22 List and ArrayList Often see this: List<String> mylist = new ArrayList<String>(); Create an ArrayList object but access it via the List type. Code using list does not depend on ArrayList directly. Can substitute different concrete class: List<String> mylist = new LinkedList<String>(); Discover linked list is a better data structure for current application. Create different object but code using List type remains same. 23 Generic Interface Actually many of the classes/interfaces associated with ArrayList are generic. public interface List<E> // A generic interface boolean add(e obj); E get(int index); boolean isempty(); // etc... E is a type variable, instantiated during type checking. 24

Generic ArrayList class ArrayList<E> extends etc... private E[] elementdata; private int size; public E get(int index) RangeCheck(index); return elementdata[index]; public boolean add(e o) ensurecapacity(size + 1); elementdata[size++] = o; return true; // And so on... Uses array to store data. Methods like get and add do checking and manipulate array. 25 Compiling a generic class Compiling and type checking generic classes is more subtle than it might seem at first sight... When a generic class is compiled the compiler does not know which real types the type variables will be instantiated to. So cannot type check things like most method calls and new expressions depending on type variables: E avar;... e.f(); // compiler doesn t know if E has method f. E[] e = new E[size]; // compiler doesn t know what type of array might be created at runtime. Hence, significant restrictions on what can be written. 26 Compiling code using generic classes When ArrayList<String> is declared: Compiler instantiates type variable E to String. Then type checks code, to ensure that only Strings are added/removed. But does not re-compile ArrayList<E> or create an ArrayList<String>.class. When compiling ArrayList<E> the compiler actually generates one.class file where Object is substituted for E. Called Type Erasure. For using ArrayList<String> compiler does the type checking but inserts cast expressions when generating code. 27 Generic Methods Another form of polymorphism (parametric polymorphism). A generic method can use/return values of different types. An alternative to overloading. A way of avoiding duplication of code. 28

An example public <T extends Comparable> T max(t t1, T t2) if (t1.compareto(t2) > 0) return t1; else return t2; max( hello, world ); max(20,10); max( a, z ); Extends means that T must be a subtype of Comparable. Comparable is an interface that defines one method: int compareto(t). 29 More complex example static <T, V extends T> boolean isin(t x, V[] y) for(int i=0; i < y.length; i++) if(x.equals(y[i])) return true; return false; public static void main(string args[]) Integer nums[] = 1, 2, 3, 4, 5 ; if(isin(2, nums)) System.out.println("2 is in nums"); if(!isin(7, nums)) System.out.println("7 is not in nums"); System.out.println(); String strs[] = "one", "two", "three", "four", "five" ; if(isin("two", strs)) System.out.println("two is in strs"); Equals method is declared in class Object, so all objects must have it. if(!isin("seven", strs)) System.out.println("seven is not in strs"); 30 What do you need to know about generics for now? Not any more than already covered and that the generic library classes are available to be used. Whole subject is a lot more complicated than seen so far. More syntax. More mechanisms. All about type safety. 31