tailieunhanh - Lecture Introduction to programming with Java - Chapter 13: Inheritance and polymorphism

This chapter presents the following content: The object class, the equals method, the tostring method, polymorphism, dynamic binding, compilation details, polymorphism with arrays, abstract methods and classes. | 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 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. 2 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; that is, if the two reference variables contain the same address. 3 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; if (((car2) && (car1 == car2)) { ("cars are equal - first time"); } car2 = new Car("Honda"); if (((car2) || (car1 == car2)) { ("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. 4 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. 5 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; } // . | 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 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. 2 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; that is, if the two reference variables contain the same address. 3 The equals Method Assuming that the Car class does not have its own equals method, what .