More Implications of Inheritance COMP204, Bernhard Pfahringer.

Slides:



Advertisements
Similar presentations
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Advertisements

Identity and Equality Based on material by Michael Ernst, University of Washington.
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Written by: Dr. JJ Shepherd
Final Review.
Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”
15-Jun-15 Lists in Java Part of the Collections Framework.
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
12-Jul-15 Lists in Java Part of the Collections Framework.
From Theory to Practice 1 OOP Overview Reminder – some OOD principles from previous lessons: –Class Inheritance vs. Object Composition –Program.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Intro to OOP with Java, C. Thomas Wu
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
04/29/ Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.
CS 2430 Day 9. Announcements Quiz on Friday, 9/28 Prog1: see , see me as soon as possible with questions/concerns Prog2: do not add any public methods.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
A Singleton Puzzle: What is Printed? 1 public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
CSSE501 Object-Oriented Development. Some OO Design Principles  Majority principles here come from: Design Principles in Java, Bob Tarr.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Implications of Inheritance COMP206, Geoff Holmes and Bernhard Pfahringer.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Java 2 Collections Bartosz Walter Software Engineering II.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
Java for C++ Programmers A Brief Tutorial. Overview Classes and Objects Simple Program Constructors Arrays Strings Inheritance and Interfaces Exceptions.
Copyright 2010 by Pearson Education Building Java Programs Chapter 10, 11 Lecture 22: 143 Preview optional reading: 10.1,
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
Effective Java, Chapter 4: Classes and Interfaces Last Updated: Fall 2011.
Subtype Polymorphism, cont. Parametric Polymorphism and Java Generics.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight.
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Building Java Programs Generics, hashing reading: 18.1.
The Class ArrayLinearList
Design Principles.
Implementing ArrayList Part 1
CSE 331 Subtyping slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Programming in Java Lecture 11: ArrayList
Effective Java: Classes and Interfaces
L5. Necessary Java Programming Techniques
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Web Design & Development Lecture 6
Part of the Collections Framework
Presentation transcript:

More Implications of Inheritance COMP204, Bernhard Pfahringer

Immutable objects cannot be changed after “construction” how to: do not allow modification by not providing “mutators” (methods that change state) making data fields “private” (always good idea) (most likely) prevent against subclassing useful for “value” types, like Integer, String, and so on; safe to use as keys for HashMaps (see code example for explanation) hashCode “rule”: a.equals(b) == true => a.hashCode() == b.hashCode() [only this direction, different objects can have the same hashCode]

Object construction no explicit constructor: system adds “default constructor”: public A() { super(); } At least one constructor given explicitly: no “default constructor” will be added first line in the constructor can explicitly pass on to constructor of the super-class or same class (if not, system will add “super();”) public A() { super(“info”); /* more code */ } public A() { this(0); /* more code */ }

Construction order Data fields are allocated and initialized to default values (0, null, …) *before any* code blocks or constructors; Then the process is top-down (most general class first to most specific last), each time: Data fields are initialized to their actual values (e.g. 1 in the code example), in order Local code blocks are run, in order The respective constructor is run

Bad Inheritance example import java.util.*; public class InstrumentedArrayList extends ArrayList { // The number of attempted element additions private int addCount = 0; public InstrumentedArrayList() { } public InstrumentedArrayList(Collection c) { super(c); } public InstrumentedArrayList(int initialCapacity) { super(initialCapacity); }

cont. public void add(int index, Object o) { addCount++; super.add(index,o); } public boolean addAll(Collection c) { addCount += c.size(); return super.addAll(c); } public int getAddCount() { return addCount; } public static void main(String[] args) { InstrumentedArrayList s = new InstrumentedArrayList(); s.addAll(Arrays.asList(new String[] {”A",”B",”C"})); System.out.println(s.getAddCount()); }

Better: use Composition import java.util.*; public class InstrumentedList implements List { private final List s; private int addCount = 0; public InstrumentedList(List s) { this.s = s; } public void add(int index, Object o) { addCount++; s.add(index, o); } public boolean addAll(Collection c) { addCount += c.size(); return s.addAll(c); }

Composition, cont. public int getAddCount() { return addCount; } // plus all necessary forwarding methods public void clear() { s.clear(); } public boolean contains(Object o) { return s.contains(o); } public boolean isEmpty() { return s.isEmpty(); } … and so on …

Utilities: Arrays class, Collections lots of useful utilities, have a look at the Javadoc, e.g. Arrays.sort(…) Arrays.binarySearch(..) Arrays.toString(..) Arrays.fill(..) Arrays.hashCode(..) Arrays.equals(..) Arrays.asList(..) Arrays.deepEquals(..), deepToString(..) deepHashCode(..) Collections.shuffle(..)

Utilities: System class again lots of useful utilities, have a look at the Javadoc, e.g. System.arraycopy(..) System.nanoTime() System.identityHashCode(..) System.getenv(..) …