{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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.
Image of page 2
COP 3330: Classes In Java – Part 2 Page 3 © Dr. Mark Llewellyn
Image of page 3

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

View Full Document Right 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.
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.
Image of page 5

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

View Full Document Right 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()).
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.
Image of page 7

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern