lec05 - CSE 12 The Java Collections Framework Methods...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
04 Methods Inherited from the Object Class Iterators and the Iterator Pattern Developing a test plan for a Collection class CSE 12 The Java Collections Framework
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Methods Required by an Interface A Java interface specifies instance methods that any class implementing the interface must provide The Java code in an interface only shows method headers; together with comments and other documentation this specifies what the methods should do It is then up to the implementing class to provide implementations for those methods The implementing class can inherit method implementations from superclasses; the method definitions do not need to be written inside the implementing class itself Because Object is a superclass of every other class, every class has all those methods However, they may need to be overridden…
Background image of page 2
Methods inherited from Object In Java, the Object class is an ancestor of every other class So, in Java, every object is-a Object This means that every public method of the Object class is inherited by every Java class Some of these methods of the Object class should be overridden in subclasses and customized as appropriate for that subclass The Object class has 9 public instance methods… Three of these should be overridden in every subclass: equals(Object), hashCode(), toString()
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Method inheritance: equals(Object) /** * Show the use of the equals(Object) method inherited from Object. */ public class ShapeEx3 { public static void main ( String [] args ) { Rectangle r1 = new Rectangle(); Rectangle r2 = new Rectangle(); Object r3 = new Rectangle(); Rectangle r4 = new Rectangle(); r4.setLength( 4 ); r4.setHeight( 25 ); System.out.println(r1.equals(r1)); System.out.println(r1.equals(r2)); System.out.println(r1.equals(r3)); System.out.println(r1.equals(r4)); } } true false false false The equals(Object) method as defined in Object: public boolean equals(Object o) { return this == o; } output with inherited equals():
Background image of page 4
Rectangle: Overriding equals() /** * Indicates whether this object is "equal to" the
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/27/2012 for the course CSE 12 taught by Professor Gary during the Fall '08 term at UCSD.

Page1 / 19

lec05 - CSE 12 The Java Collections Framework Methods...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online