Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Implications of Inheritance COMP204, Bernhard Pfahringer.

Similar presentations


Presentation on theme: "More Implications of Inheritance COMP204, Bernhard Pfahringer."— Presentation transcript:

1 More Implications of Inheritance COMP204, Bernhard Pfahringer

2 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]

3 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 */ }

4 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

5 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); }

6 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()); }

7 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); }

8 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 …

9 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(..)

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


Download ppt "More Implications of Inheritance COMP204, Bernhard Pfahringer."

Similar presentations


Ads by Google