Lecture13

Lecture13 - The Object Class Defined in the java.lang package of the Java standard class library All classes are derived from the Object class If a

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

View Full Document Right Arrow Icon
1 The Object Class • Defined in the java.lang package of the Java standard class library • All classes are derived from the Object class • If a class is not explicitly defined to be the child of an existing class, it is assumed to be the child of the Object class • The Object class is the ultimate root of all class hierarchies
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 The Object class contains a few useful methods, which are inherited by all classes The equals method of the Object class returns true if two references are aliases Dog a = new Dog(); Dog c = new Dog() if (a.equals (c)) { System.out.println(“True”); } else { System.out.println(“false”); } Object boolean equals() getClass() int hashCode() String toString()
Background image of page 2
3 We can override equals in any class to define equality in more appropriate way String class defines the equals method to return true if two String objects contain the same characters String a = “Toronto”; String b = “Toronto”; if (a.equals(b)) { System.out.println (“True”); else System.out.println (“false”); } The getClass() method returns the runtime class of the object. Cat c = new Cat(); System.out.println(c.getClass());
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 The hashCode method returns a hash code value for the object. This is typically implemented by converting the internal address of the object into an integer. Cat c = new Cat(); System.out.println(c.hashCode()); % java TestObject 8202111 The toString method in the Object class is defined to return a string that contains the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. Cat c = new Cat(); System.out.println(c.toString()); Output: [email protected] Every time we define the toString method, we are actually overriding an inherited definition.
Background image of page 4
5 The following source code defines a class. public class OrderedPair { private int i; private int j; public OrderedPair(int a, int b) { i = a; j = b; } public void swap() { int temp = i; i = j; j = temp; } public String toString() { p q return i <= j ? "ordered" : "not ordered"; } } r a) Which members are inaccessible outside of the class ? b) Which members are accessible outside of the class ? c) What is the output from the following code fragment: OrderedPair p = new OrderedPair(3, 4); OrderedPair q = new OrderedPair(6, 4); OrderedPair r = new OrderedPair(7, 7); System.out.println("p: " + p); System.out.println("q: " + q); System.out.println("r: " + r); p.swap(); System.out.println("p: " + p); i : 3 j : 4 i: 6 j: 4 i : 7 j: 7
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 “this” and “super” reference Access to overriden methods and variables can be obtained with the super keyword: public class A { public int i=0; public int j=3; //private? //protected?? public void doSomething(){i=5;}
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/12/2010 for the course COE 318 taught by Professor Ken during the Spring '08 term at Ryerson.

Page1 / 26

Lecture13 - The Object Class Defined in the java.lang package of the Java standard class library All classes are derived from the Object class If a

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

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