// File: NoOverriding.java // Shows what is returned by the toString method inherited from superclass // Object when called (explicitly and implicitly) for a subclass object. // Also shows that the inherited "equals" method works exactly like // the "==" operator. I.e., it compares the object references themselves and // NOT the actual objects "pointed to." So, it returns true when the object // variables contain the same reference (i.e., point to the same object), and // false otherwise. // Demonstrates why you should always override these methods in your classes. class Car { private String make ; private String color ; public Car(String make, String color) // constructor { this.make = make ; this.color = color ; } public String getMake() { return make ; } public String getColor() { return color ; } } public class NoOverriding { public static void main(String args[]) { Car myCar = new Car("Hummer", "yellow") ; Car yourCar = new Car("Ferrari", "black") ; System.out.println("Initially, myCar is a " + myCar.getColor() + " " + myCar.getMake() ) ; System.out.println("Initially, yourCar is a " + yourCar.getColor() + " " + yourCar.getMake() ) ; System.out.print("\nOutput from " + "System.out.println( myCar.toString() ) : ") ; // calling inherited toString method explicitly System.out.println( myCar.toString() ) ; System.out.print("\nOutput from " + "System.out.println( yourCar ) : ") ; // calling inherited toString method implicitly System.out.println( yourCar ) ; if (myCar == yourCar) System.out.println("\n\"myCar == yourCar\" is true") ; else System.out.println("\n\"myCar == yourCar\" is false") ; if (myCar.equals(yourCar)) System.out.println("\n\"myCar.equals(yourCar)\" is true") ; else System.out.println("\n\"myCar.equals(yourCar)\" is false") ; myCar = yourCar ; System.out.println("\nafter assigning \"myCar = yourCar\" ...") ; if (myCar == yourCar) System.out.println("\n\"myCar == yourCar\" is true") ; else System.out.println("\n\"myCar == yourCar\" is false") ; if (myCar.equals(yourCar)) System.out.println("\n\"myCar.equals(yourCar)\" is true") ; else System.out.println("\n\"myCar.equals(yourCar)\" is false") ; myCar = new Car("Ferrari", "black") ; System.out.println("\nafter assigning " + "\"myCar = new Car(\"Ferrari\", \"black\")\" ...") ; if (myCar == yourCar) System.out.println("\n\"myCar == yourCar\" is true") ; else System.out.println("\n\"myCar == yourCar\" is false") ; if (myCar.equals(yourCar)) System.out.println("\n\"myCar.equals(yourCar)\" is true") ; else System.out.println("\n\"myCar.equals(yourCar)\" is false") ; } } /* Program output: Initially, myCar is a yellow Hummer Initially, yourCar is a black Ferrari Output from System.out.println( myCar.toString() ) : Car@ba34f2 Output from System.out.println( myCar ) : Car@ea2dfe "myCar == yourCar" is false "myCar.equals(yourCar)" is false after assigning "myCar = yourCar" ... "myCar == yourCar" is true "myCar.equals(yourCar)" is true after assigning "myCar = new Car("Ferrari", "black")" ... "myCar == yourCar" is false "myCar.equals(yourCar)" is false */