Classes In Java (6) - COP 3330: Object-Oriented Programming...

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

View Full Document Right Arrow Icon
COP 3330: Classes In Java – Part 2 Page 1 © Dr. Mark Llewellyn COP 3330: Object-Oriented Programming Summer 2011 Classes In Java – Part 2 Inheritance and Polymorphism Department of Electrical Engineering and Computer Science Computer Science Division University of Central Florida Instructor : Dr. Mark Llewellyn [email protected] HEC 236, 407-823-2790 http://www.cs.ucf.edu/courses/cop3330/sum2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
COP 3330: Classes In Java – Part 2 Page 2 © Dr. Mark Llewellyn The Object Class and its Methods Every class in Java is descended from the java.lang.Object class. If no inheritance is specified when a class is defined, the superclass of the class is Object by default. For example, the following two class declarations are equivalent: public class Circle { ... } public class Circle extends Object { ... } equivalent Classes like String , Loan , GeometricObject are implicitly subclasses of Object (as are all of the classes we have constructed so far in this course). It is important to be familiar with the methods provided by the Object class so that you can use them in your classes.
Background image of page 2
COP 3330: Classes In Java – Part 2 Page 3 © Dr. Mark Llewellyn
Background image of page 3

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

View Full DocumentRight Arrow Icon
COP 3330: Classes In Java – Part 2 Page 4 © Dr. Mark Llewellyn The Object Class and its Methods For right now, we want to focus on two of these methods, the toString and equals . The signature of the toString() method is: public String toString() Invoking toString() on an object returns a string that describes the object. By default, it returns a string consisting of the class name of which the object is an instance, an at sign (@), and the object’s memory address in hexadecimal. The following page, shows a slight modification to the TestLoan class we wrote in the previous section to illustrate the use of the toString() method.
Background image of page 4
COP 3330: Classes In Java – Part 2 Page 5 © Dr. Mark Llewellyn Invoke the Object class toString() method on the newly created loan object. Result shown below in default format.
Background image of page 5

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

View Full DocumentRight Arrow Icon
COP 3330: Classes In Java – Part 2 Page 6 © Dr. Mark Llewellyn The Object Class and its Methods Obviously, this message is not very helpful or informative. Usually, you should override the toString method so that it returns a descriptive string representation of the object. This is what we did in the GeometricObject class as shown below: /** Return a string representation of this object */ public String toString() { return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled; } NOTE: You can also pass an object to invoke System.out.println(object). This is equivalent to invoking System.out.println(object.toString()).
Background image of page 6
COP 3330: Classes In Java – Part 2 Page 7 © Dr. Mark Llewellyn The Object Class and its Methods The signature of the equals method is: public boolean equals(Object o) This method test whether two objects are equal. The syntax for invoking this method is:
Background image of page 7

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

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

This note was uploaded on 07/04/2011 for the course COP 3330 taught by Professor Staff during the Summer '08 term at University of Central Florida.

Page1 / 41

Classes In Java (6) - COP 3330: Object-Oriented Programming...

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

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