Liang Chapter 11- Inheritance and Polymorphism 11.1 Introduction Object-oriented programming allows you to define new classes from existing classes. This is called inheritance. 11.2 Superclasses and Subclasses Inheritance enables you to define a general class (superclass) and later extend it to more specialized classes (subclasses) Shape class (superclass) vs. circle class (subclass) example SimpleGeometicShape Extends keyword, public methods in superclass are inherited by its subclasses. Get and set methods are needed to modify private data fields in the superclass. SimpleGeometicObject example is excellent for inheritance. 5 Points regarding inheritance – pg. 414 11.3 Using the super keyword The keyword super refers to the superclass and can be used to invoke the superclass’s methods and constructors. The constructors of a superclass are not inherited by a subclass. They can only be invoked from the constructors of the subclasses using the keyword super. The statement super() or super(parameters) must appear in the first line of the subclass’s constructor. This is the only way to explicitly invoke a superclass constructor. Constructor Chaining – If a class is designed to be extended, it is better to provide a no-arg constructor to avoid programming errors. (Fruit example on page 416) Calling superclass Methods – The keyword super can be used to reference a method of its superclass. 11.4 Overriding Methods To override a method, the method must be defined in the subclass using the same signature and the same return type as in its superclass. An instance method can only be overridden if it is accessible. Private methods cannot be overriedden. Static methods can be inherited, however they cannot be overridden. (and it is hidden) 11.5 Overriding vs. Overloading Overloading means to define multiple methods with the same name but different signatures. Overriding means to provide a new implementation for a method in the subclass. To override a method, the method must be defined in the subclass using the same signature and the same return type. Comparison – page 419 Overridden methods are in different classes related by inheritance; overloaded methods can be either in the same class or different classes related by inheritance. Overriden methods have the same signature and return type; overloaded methods have the same name but different parameter list. @Override is special Java syntax 11.6 The Object Class and its toString() Method Every class in Java is descended from the java.lang.Object class. Public class Blah{..} is the same as public class Blah extends Object{..} toString works with all objects and returns class name @ object’s memory address. Example: Loan@15037e5 11.7 Polymorphism Polymorphism means that a variable of a supertype can refer to a subtype object. Subtype vs. supertype – Circle is a subtype of GeometricObject and GeometricObject is a supertype of Circle. Every instance of a subclass is an instance of its superclass, but not vice versa. You can always pass an instanced of a subclass to a parameter of its superclass type. Example on pg. 422 11.8 Dynamic Binding A method can be implemented in several classes along the inheritance chain. The JVM decides which method is invoked at runtime. Declared type vs. actual type …. Object o = new GeometricObject(); (The actual type is GeometicObject) pg.422 Dynamic binding, DynamicBindingDemo program Matching a method signature and binding a method implementation are two separate issues. 11.9 Casting Objects and the instanceOf Operator One object reference can be typecast into another object reference. This is called casting object. M(new Student()); is the same as Object o = new Student(); m(o); (Implicit Casting) Student b = o; This doesn’t work because o is of type Object and is the superclass of Student. To make this work, try: Student b = (Student)0; (Explicit Casting, like with primitive data types) Upcasting vs. downcasting, ClassCastException, Object myObject = new Circle(); If(myObject instanceof Circle) – Makes sure that myObject is a circle before running the rest of the condition statement. It is good practice to define a variable with a supertype, which can accept a value of any subtype. CastingDemo 11.10 The Object’s equals Method Like the toString() method, the equals(Object) method is another method defined in the Object class If (Object1.equals(Object2)){…} equals is overridden in the String class (subtype of Object) 11.11 The ArrayList Class An ArrayList object can be used to store a list of objects. ArrayList<String> cities = new ArrayList<String>(); ArrayList methods are listed on page 431. (add, size, remove, getIndex, toString, clear, set, get) TestArrayList, DistinctNumbers 11.12 Case Study – A Custom Stack Class This section designs a stack class for holding objects. 11.13 The protected Data and Methods A protected member of a class can be accessed from a subclass. Used when we want to allow subclasses to access a superclass’s data fields or methods, but not allow nonsubclasses access. Use private modifier to hide the members of the class completely so that they cannot be access directly from outside the class. Use protected to enable the members of the class to be accessed by the subclasses in any package or classes in the same package. Use public to enable the members of the class to be accessed by any class. If a method is defined as public in the superclass, it must be defined as public in the subclass. 11.14 Preventing Extending and Overriding Neither a final class nor a final method can be extended. A final data field is a constant. The Math class is an example. public final class Math{….}