chapter13 - Chapter13InheritanceandPolymorphism 1 1...

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

View Full Document Right Arrow Icon
1 Chapter 13 - Inheritance and Polymorphism The  Object  Class The  equals  Method The  toString  Method Polymorphism Dynamic Binding Compilation Details Polymorphism with Arrays Abstract Methods And Classes 1
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 The  Object  class is a superclass for all other  classes. When declaring your own classes, you don't have to  specify the  Object  class as a superclass - it's  automatically a superclass. We're covering just two of  Object 's methods. The  equals  and  toString  methods are the most  important  Object  methods.…
Background image of page 2
3 The  equals  Method For a class that doesn't have its own  equals  method,  if an object from that class calls the  equals  method,  it inherits and uses the  Object  class's  equals   method. The  Object   class's   equals  method returns  true  if  the two reference variables that are being compared  point to the same object; i.e., if the two reference  variables contain the same address.
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  equals  Method Assuming that the  Car  class does not have its own  equals   method, what does this code fragment print? Car car1 = new Car("Honda"); Car car2 = car1; { System.out.println("cars are equal - first time"); } car2 = new Car("Honda"); if ((car1.equals(car2) || (car1 == car2)) { System.out.println("cars are equal - second time"); } Aside: the  ==  operator works the same as the  Object  class's  equals  method;  ==  returns  true  if the two reference variables  point to the same object. 1 2
Background image of page 4
5 The  equals  Method Usually, the  Object   class's  equals  method is not  good enough. You'll usually want to compare the  contents  of two objects rather than just whether two  reference variables point to the same object. To do that, you'll need to have an  equals  method in  the object's class definition that compares the  contents of the two objects. 1
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 Defining Your Own  equals  Method Write an  equals  method for a  Car  class. Use this skeleton: public class Car { private String make; private int year; private String color; <equals method goes here> } // end class Car public class CarDriver { public static void main(String[] args) { Car car1 = new Car(); Car car2 = new Car(); if (car1.equals(car2)) { System.out.println("cars have identical features"); } ... 1 2 4 3 5 6
Background image of page 6
7 The  equals  Method Note that  equals  methods are built into lots of Java's API  classes. For example, the 
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 10/22/2010 for the course COMPSCI CS201 taught by Professor Sorenson during the Spring '10 term at Wisconsin Milwaukee.

Page1 / 33

chapter13 - Chapter13InheritanceandPolymorphism 1 1...

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