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

Similar documents
Programmieren II. Collections. Alexander Fraser. May 28, (Based on material from T. Bögel)

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

Defining an interface is similar to creating a new class: // constant declarations, if any

Inheritance & Polymorphism

Object-Oriented Programming More Inheritance

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

Pieter van den Hombergh Richard van den Ham. February 8, 2018

Software Development (cs2500)

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. June 8, 2017

(SE Tutorials: Learning the Java Language Trail : Interfaces & Inheritance Lesson )

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

Inheritance & Polymorphism. Object-Oriented Programming

Chapter 14 Abstract Classes and Interfaces

C18a: Abstract Class and Method

Java Session. Day 2. Reference: Head First Java

Object-Oriented ProgrammingInheritance & Polymorphism

C09: Interface and Abstract Class and Method

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

What is Inheritance?

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

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

Collections, Maps and Generics

Abstract data types (again) Announcements. Example ADT an integer bag (next) The Java Collections Framework

Core Java Contents. Duration: 25 Hours (1 Month)

Practical Session 3 Java Collections

Computer Science 210: Data Structures

CS2110: Software Development Methods. Maps and Sets in Java

5/23/2015. Core Java Syllabus. VikRam ShaRma

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics

Java Data Structures Collections Framework BY ASIF AHMED CSI-211 (OBJECT ORIENTED PROGRAMMING)

Java Collections Framework

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Java Collection Framework

Inheritance and Polymorphism

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

Inheritance and Substitution (Budd chapter 8, 10)

CS2110: Software Development Methods. Maps and Sets in Java

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Computer Science II (Spring )

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 6

CONTAİNERS COLLECTİONS

Java Object Oriented Design. CSC207 Fall 2014

36. Collections. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Algorithms. Produced by. Eamonn de Leastar

Inheritance and Polymorphism

CMSC 132: Object-Oriented Programming II

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

PIC 20A Collections and Data Structures

CS 251 Intermediate Programming Inheritance

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

EPITA Première Année Cycle Ingénieur. Atelier Java - J2

Java Collections Framework reloaded

First IS-A Relationship: Inheritance

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

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

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

Class 32: The Java Collections Framework

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

Collections (Java) Collections Framework

CS61BL Summer 2013 Midterm 2

Sets and Maps. Part of the Collections Framework

CS Programming Language Java. Fall 2004 Sept. 29

Inheritance. Transitivity

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

JAVA. Duration: 2 Months

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

Object-Oriented Programming

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Chapter 5 Object-Oriented Programming

Software Architecture (Lesson 2) Object-Oriented Paradigm (1)

Object Oriented Programming. Java-Lecture 11 Polymorphism

Collections Framework: Part 2

Chapter 10 Classes Continued. Fundamentals of Java

17. Java Collections. Organizing Data. Generic List in Java: java.util.list. Type Parameters ( Parameteric Polymorphism ) Data Structures that we know

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 4 09/03/2009

Collections. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

What is the Java Collections Framework?

More Relationships Between Classes

Introduction to Programming Using Java (98-388)

Complete Java Contents

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

Generics Collection Framework

Core Java SYLLABUS COVERAGE SYLLABUS IN DETAILS

Java Magistère BFA

40) Class can be inherited and instantiated with the package 41) Can be accessible anywhere in the package and only up to sub classes outside the

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

An Interface with Generics

Generic classes & the Java Collections Framework. *Really* Reusable Code

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

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

CS-202 Introduction to Object Oriented Programming

Tutorial #11 SFWR ENG / COMP SCI 2S03. Interfaces and Java Collections. Week of November 17, 2014

Fundamental language mechanisms

Object Fundamentals Part Two. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 3 09/01/2009

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Transcription:

Programmieren II Polymorphism Alexander Fraser fraser@cl.uni-heidelberg.de (Based on material from T. Bögel) June 4, 2014 1 / 50

Outline 1 Recap - Collections 2 Advanced OOP: Polymorphism Polymorphism Interfaces Abstract classes Interfaces vs. Abstract Classes 2 / 50

Outline 1 Recap - Collections 2 Advanced OOP: Polymorphism Polymorphism Interfaces Abstract classes Interfaces vs. Abstract Classes 3 / 50

Collections Containers for Objects Convenient for storing, accessing and manipulating elements Learn the Java Core Collections hierarchy by heart! You should know common wrapper types that capture primitive data types as objects 4 / 50

Core Collections hierarchy source: http://docs.oracle.com/javase/tutorial/collections/interfaces/index.html Set is special kind of Collection etc. Map is separate from Collection All collection interfaces are generic: ArrayList<E> list = new ArrayList<E>; You should specify type of objects within a collection 5 / 50

Concrete implementations List: ArrayList (insertion-order, allows duplicates) LinkedList (insertion-order, allows duplicates) Set: HashSet (unordered, no duplicates) LinkedHashSet (insertion-order, no duplicates) Later: TreeSet (ordered, no duplicates) Map: HashMap (unordered keys, no duplicate keys) LinkedHashMap (insertion-ordered keys, no duplicate keys) Later: TreeMap (ordered keys, no duplicate keys) 6 / 50

Concrete implementations List: ArrayList (insertion-order, allows duplicates) LinkedList (insertion-order, allows duplicates) Set: HashSet (unordered, no duplicates) LinkedHashSet (insertion-order, no duplicates) Later: TreeSet (ordered, no duplicates) Map: HashMap (unordered keys, no duplicate keys) LinkedHashMap (insertion-ordered keys, no duplicate keys) Later: TreeMap (ordered keys, no duplicate keys) 6 / 50

Concrete implementations List: ArrayList (insertion-order, allows duplicates) LinkedList (insertion-order, allows duplicates) Set: HashSet (unordered, no duplicates) LinkedHashSet (insertion-order, no duplicates) Later: TreeSet (ordered, no duplicates) Map: HashMap (unordered keys, no duplicate keys) LinkedHashMap (insertion-ordered keys, no duplicate keys) Later: TreeMap (ordered keys, no duplicate keys) 6 / 50

Concrete implementations List: ArrayList (insertion-order, allows duplicates) LinkedList (insertion-order, allows duplicates) Set: HashSet (unordered, no duplicates) LinkedHashSet (insertion-order, no duplicates) Later: TreeSet (ordered, no duplicates) Map: HashMap (unordered keys, no duplicate keys) LinkedHashMap (insertion-ordered keys, no duplicate keys) Later: TreeMap (ordered keys, no duplicate keys) 6 / 50

Concrete implementations List: ArrayList (insertion-order, allows duplicates) LinkedList (insertion-order, allows duplicates) Set: HashSet (unordered, no duplicates) LinkedHashSet (insertion-order, no duplicates) Later: TreeSet (ordered, no duplicates) Map: HashMap (unordered keys, no duplicate keys) LinkedHashMap (insertion-ordered keys, no duplicate keys) Later: TreeMap (ordered keys, no duplicate keys) 6 / 50

Concrete implementations List: ArrayList (insertion-order, allows duplicates) LinkedList (insertion-order, allows duplicates) Set: HashSet (unordered, no duplicates) LinkedHashSet (insertion-order, no duplicates) Later: TreeSet (ordered, no duplicates) Map: HashMap (unordered keys, no duplicate keys) LinkedHashMap (insertion-ordered keys, no duplicate keys) Later: TreeMap (ordered keys, no duplicate keys) 6 / 50

Concrete implementations List: ArrayList (insertion-order, allows duplicates) LinkedList (insertion-order, allows duplicates) Set: HashSet (unordered, no duplicates) LinkedHashSet (insertion-order, no duplicates) Later: TreeSet (ordered, no duplicates) Map: HashMap (unordered keys, no duplicate keys) LinkedHashMap (insertion-ordered keys, no duplicate keys) Later: TreeMap (ordered keys, no duplicate keys) 6 / 50

Concrete implementations List: ArrayList (insertion-order, allows duplicates) LinkedList (insertion-order, allows duplicates) Set: HashSet (unordered, no duplicates) LinkedHashSet (insertion-order, no duplicates) Later: TreeSet (ordered, no duplicates) Map: HashMap (unordered keys, no duplicate keys) LinkedHashMap (insertion-ordered keys, no duplicate keys) Later: TreeMap (ordered keys, no duplicate keys) 6 / 50

Concrete implementations List: ArrayList (insertion-order, allows duplicates) LinkedList (insertion-order, allows duplicates) Set: HashSet (unordered, no duplicates) LinkedHashSet (insertion-order, no duplicates) Later: TreeSet (ordered, no duplicates) Map: HashMap (unordered keys, no duplicate keys) LinkedHashMap (insertion-ordered keys, no duplicate keys) Later: TreeMap (ordered keys, no duplicate keys) 6 / 50

Concrete implementations List: ArrayList (insertion-order, allows duplicates) LinkedList (insertion-order, allows duplicates) Set: HashSet (unordered, no duplicates) LinkedHashSet (insertion-order, no duplicates) Later: TreeSet (ordered, no duplicates) Map: HashMap (unordered keys, no duplicate keys) LinkedHashMap (insertion-ordered keys, no duplicate keys) Later: TreeMap (ordered keys, no duplicate keys) 6 / 50

Concrete implementations List: ArrayList (insertion-order, allows duplicates) LinkedList (insertion-order, allows duplicates) Set: HashSet (unordered, no duplicates) LinkedHashSet (insertion-order, no duplicates) Later: TreeSet (ordered, no duplicates) Map: HashMap (unordered keys, no duplicate keys) LinkedHashMap (insertion-ordered keys, no duplicate keys) Later: TreeMap (ordered keys, no duplicate keys) 6 / 50

Concrete implementations List: ArrayList (insertion-order, allows duplicates) LinkedList (insertion-order, allows duplicates) Set: HashSet (unordered, no duplicates) LinkedHashSet (insertion-order, no duplicates) Later: TreeSet (ordered, no duplicates) Map: HashMap (unordered keys, no duplicate keys) LinkedHashMap (insertion-ordered keys, no duplicate keys) Later: TreeMap (ordered keys, no duplicate keys) 6 / 50

Concrete implementations List: ArrayList (insertion-order, allows duplicates) LinkedList (insertion-order, allows duplicates) Set: HashSet (unordered, no duplicates) LinkedHashSet (insertion-order, no duplicates) Later: TreeSet (ordered, no duplicates) Map: HashMap (unordered keys, no duplicate keys) LinkedHashMap (insertion-ordered keys, no duplicate keys) Later: TreeMap (ordered keys, no duplicate keys) 6 / 50

Description of core collection interfaces Collection Root of collection hierarchy Collection contains elements Methods for each Collection size(): int Number of elements in a collection isempty(): boolean contains(object element): boolean add(e element): boolean remove(object element): boolean iterator(): Iterator<E> toarray(): Object[] equals(): boolean 7 / 50

Collection bulk operations Methods that operate on entire collection Methods containsall: does the collection contain all elements specified in another collection? addall: add all elements of one collection to another collection removeall: remove all elements that are elements of a second collection clear: removes all elements from the collection 8 / 50

Additional collections Interface List Queue Dequeue implementation/description Stack (LIFO) holding elements prior to processing supports element insertion and removal at both ends 9 / 50

Outline 1 Recap - Collections 2 Advanced OOP: Polymorphism Polymorphism Interfaces Abstract classes Interfaces vs. Abstract Classes 10 / 50

Outline 1 Recap - Collections 2 Advanced OOP: Polymorphism Polymorphism Interfaces Abstract classes Interfaces vs. Abstract Classes 11 / 50

Motivation: Polymorphism Imagine we need to filter Twitter messages We define a Message class (think of this as a string for now) Then we can define different types of filters which operate on messages We will chain these filters (apply them in a sequence) 12 / 50

Example: Filter Each Filter filters... Sub classes inherit methods of super class This allows for Polymorphism 13 / 50

Polymorphism I Objects of a concrete sub class can be used where super classes are expected All sub classes have complete functionality of super class But: special functionality implemented in the sub class cannot be accessed via super class 14 / 50

Polymorphism II Example public Message filtermessage ( Message m, GeneralFilter f) { f. apply (m); // f. printfilterregex () would not work }... public void runfiltering ( Message m) { LinkFilter f = new LinkFilter (); this. filtermessage (m,f); } filtermessage() expects GeneralFilter LinkFilter is also a GeneralFilter Each sub class of GeneralFilter has a apply() method filtermessage() does not need to know which filter s method it is calling! 15 / 50

Polymorphism: Arrays and Collections I Polymorphic Collections List < GeneralFilter > filters = new ArrayList < GeneralFilter >(); filters. add (new LinkFilter ()); filters. add (new GeneralFilter ()); Each sub-class of GeneralFilter is also a GeneralFilter Collections can be filled with sub-classes We can only access methods in GeneralFilter List ArrayList itself is polymorphic 16 / 50

Polymorphism: Arrays and Collections II Polymorphic Arrays GeneralFilter [] filters = new GeneralFilter [2]; filters [0] = new LinkFilter (); filters [1] = new GeneralFilter (); Each sub-class of GeneralFilter is also a GeneralFilter Arrays can be filled with sub-classes 17 / 50

Polymorphism: Arrays and Collections III Accessing array elements GeneralFilter f = filters [0]. filter ( m); For each element of the array, all functionality of the super class can be used 18 / 50

Polymorphic Methods The return type of a method can also be polymorphic public GeneralFilter returnfilter () { FormattingFilter f = new FormattingFilter (); return f; } Method returns GeneralFilter Each sub-class of GeneralFilter is also a GeneralFilter 19 / 50

Polymorphism Polymorphism Using Polymorphism allows extension of code New sub-classes do not require changes in the client code But: if a class overrides inherited methods, which method is called? Answer: the most specific one is called 20 / 50

Polymorphism Polymorphism Using Polymorphism allows extension of code New sub-classes do not require changes in the client code But: if a class overrides inherited methods, which method is called? Answer: the most specific one is called 20 / 50

Dynamic Method Lookup Dynamic Method Lookup If a class overrides inherited methods, which method is called? polymorphic classes should keep their specific properties, even if they seem to be objects of the super class Decision which method to call is made during runtime The fact that the method is looked up at runtime is called Dynamic Method Lookup 21 / 50

Outline 1 Recap - Collections 2 Advanced OOP: Polymorphism Polymorphism Interfaces Abstract classes Interfaces vs. Abstract Classes 22 / 50

Motivation for Interfaces I Multiple developers: need for programming contract People should be able to write code independently Knowledge about behavior of classes should be known early and without knowledge about implementation Example Future: automatic driving Automobile manufacturers write software to operate an automobile GPS company writes code to use GPS to drive the car Manufacturer needs to explicitly state specification about car operation Which methods+parameters does a car have (that can be used by the GPS company)? GPS company not interested how operation methods are implemented 23 / 50

Interfaces in Java Interfaces in Java Interface: reference type (similar to classes) Specifies only constants and method signatures Does not contain method bodies Interfaces cannot be instantiated (i.e. no new Interface) Interfaces can be extended Using an interface: implements keyword 24 / 50

Interface for an automobile public interface OperateCar { // constant declarations, if any // method signatures int turn ( Direction direction, double radius, double startspeed, double endspeed ); int changelanes ( Direction direction, double startspeed, double endspeed ); int signalturn ( Direction direction, boolean signalon ); // more method signatures } 25 / 50

Concrete car implementation I public class OperateBMW760i implements OperateCar { // the OperateCar method signatures, with implementation -- // for example : int signalturn ( Direction direction, boolean signalon ) { // code to turn BMW s LEFT turn indicator lights on // code to turn BMW s LEFT turn indicator lights off // code to turn BMW s RIGHT turn indicator lights on // code to turn BMW s RIGHT turn indicator lights off } } // other members, as needed -- for example, helper classes not // visible to clients of the interface 26 / 50

Concrete car implementation II Implementing an interface OperateBMW760i implements the OperateCar interface All methods specified in the interface need to be implemented Each car manufacturer can individually implement all methods GPS company receives concrete implementation of different companies GPS company is able to invoke OperateCar methods without knowing about their implementation By implementing an interface, you specify that your class has certain functionality 27 / 50

General Interface definition public interface Interface extends Interface1, Interface2, Interface3 { // constant declarations // base of natural logarithms double E = 2.718282; } // method signatures void dosomething ( int i, double x); int dosomethingelse ( String s); Interfaces can extend multiple interfaces All methods in an interface are public All constant values are public, static and final 28 / 50

Interfaces as types Using interfaces as types Interface is a reference type Interface name can be used just like any other data type Reference variable with interface type must always point to instance that implements interface 29 / 50

Example: Relatable interface Interface that provides a method to determine the size of two Relatable interfaces Example: Rectangle implements Relatable public interface Relatable { // this ( object calling islargerthan ) // and other must be instances of // the same class returns 1, 0, -1 // if this is greater than, equal // to, or less than other public int islargerthan ( Relatable other ); } 30 / 50

Example: Interfaces as types I Goal: find largest object in a pair of objects Works for any objects that implement Relatable public Object findlargest ( Object object1, Object object2 ) { Relatable obj1 = ( Relatable ) object1 ; Relatable obj2 = ( Relatable ) object2 ; if ( obj1. islargerthan ( obj2 ) > 0) return object1 ; else return object2 ; } Object is casted to Relatable islargerthan can be called Concrete implementation is irrelevant 31 / 50

Example: Interfaces as types II Similar to multiple inheritance: each object implementing Relatable is simultaneously an Object and Relatable 32 / 50

Checking the type of a class isinstance Access to the underlying class for an instance:.class property Class is an object Method.isInstance(Object o) checks whether an object is an instance of the class Class provides other useful methods that allow querying information about an object s class 33 / 50

Substitution for multiple inheritance source: HFJ, p. 226 34 / 50

How you implement the Relatable interface This depends on the relationship of your classes For instance, suppose we can only compare the size of Rectangles. 35 / 50

Implementing the Relatable interface public int islargerthan ( Relatable other ) { Rectangle otherrect = ( Rectangle ) other ; if ( this. getarea () < otherrect. getarea ()) return -1; else if ( this. getarea () > otherrect. getarea ()) return 1; else return 0; } This works fine for Squares It throws a casting exception if other is a non-rectangle How should we change this so we can also compare with, e.g., Circles? 36 / 50

Summary Summary: Interfaces Interfaces define protocols for communication between objects Interface declarations only contain method signatures & constants, no implementation A class implementing an interface must implement all of its methods Interfaces can be used just like other (reference) types 37 / 50

Outline 1 Recap - Collections 2 Advanced OOP: Polymorphism Polymorphism Interfaces Abstract classes Interfaces vs. Abstract Classes 38 / 50

Abstract classes Motivation Super classes represent an abstraction of sub classes Sometimes, however, instantiating the super class does not make sense Examples: Animal Shape Person University library software knows about two kinds of Persons: Student and Teacher Instantiating Person would be strange 39 / 50

Motivation: HFJ, pp. 198 199 designing with inheritance interfaces and polymorphism Did we forget about something when we designed this? Feline Animal picture food hunger boundaries location makenoise() eat() sleep() roam() We know we can say: Wolf awolf = new Wolf(); A Wolf reference to a Wolf object. And we know we can say: awolf Wolf Animal ahippo = new Hippo(); These two are the same type. Wolf object roam() Hippo size picture makenoise() food eat() prey roam() Canine Animal reference to a Hippo object. ahippo Animal Hippo object These two are NOT the same type. Lion size picture food makenoise() prey eat() But here s where it gets weird: Animal anim = new Animal(); Cat size Tiger picture size makenoise() food picture eat() prey food makenoise() prey eat() Wolf size picture makenoise() food eat() prey Dog size picture makenoise() food eat() prey Animal reference to an Animal object. anim Animal? Animal object These two are the same type, but... what the heck does an Animal object look like? 198 chapter 8 you are here4 199 40 / 50

Abstract classes I Abstract classes Keyword abstract Declare commonalities in super class, enforce implementation in sub-class abstract classes cannot be instantiated (new) Sub-class needs to implement (i.e. override) all abstract methods of the abstract class... or it needs to be abstract itself 41 / 50

Abstract classes Rules for abstract classes Each class with abstract methods needs to be abstract abstract classes cannot be instantiated (new)! abstract class can contain abstract methods and implemented methods Methods that are private, static or final cannot be abstract as they can t be overridden 42 / 50

Example: GeometricShape public abstract class GeometricShape { public abstract double getarea ();... } GeometricShapes provide a getarea() method (implementation hidden) Concrete implementation in GeometricShape not possible in this case abstract methods abstract class abstract methods define signature only 43 / 50

Concrete implementation public class Circle extends GeometricShape { public static final double PI = 3. 1415926536; private double r; public Circle ( double r ) { this. r = r; } public double getarea () { return PI* r* r; }... } public class Rectangle extends GeometricShape {... } Concrete implementation of abstract super class Implementation of abstract methods in GeometricShape Additional elements (specific to Circle) 44 / 50

Abstract classes and Polymorphism new GeometricShape() is not allowed Nevertheless, GeometricShape can be used as reference type (polymorphism) Example GeometricShape can be used just like any other data type GeometricShape s = new Circle ( 1.0 ); GeometricShape [] shapearray = new GeometricShape [1]; shapearray [0] = s; 45 / 50

Outline 1 Recap - Collections 2 Advanced OOP: Polymorphism Polymorphism Interfaces Abstract classes Interfaces vs. Abstract Classes 46 / 50

Abstract classes vs. Interfaces Flexibility vs. reusability Interfaces allow more flexibility by multiple inheritance But: code duplication very likely if multiple classes implement the same interface Abstract class: possibility to partially implement common methods 47 / 50

Abstract classes vs. Interfaces Compatibility Adding new methods to an interface: all implementing classes need to be changed Abstract class can also add non-abstract methods that are automatically inherited by sub-classes 48 / 50

Combining Interfaces and abstract classes Combination of Interfaces & abstract classes Usually: Interface + implementing abstract class (skeleton implementation) Concrete class can implement interface or extend abstract skeleton class Example: Java Collections 49 / 50

Literature Sierra, K. & Bates, B. Head First Java. (end of Chapter 7, Chapter 8) O Reilly Media, 2005. Ullenboom, Ch. Java ist auch eine Insel. (Sections 5.11, 5.12 & 5.13) Galileo Computing, 2012. The Java tutorials http://docs.oracle.com/javase/tutorial/java/concepts Eckel, B. (For Reference) Thinking in Java. (Ch. 7 & 8) Prentice Hall, 2006. 50 / 50