250 likes | 399 Views
Chapter 11. Inheritance. Superclass/subclass. Superclass contains common properties and methods that a class inherits Subclass is a specialization of a superclass that inherits the common properties and methods and adds specialized properties and methods Vertebrates would be a superclass
E N D
Chapter 11 Inheritance
Superclass/subclass • Superclass contains common properties and methods that a class inherits • Subclass is a specialization of a superclass that inherits the common properties and methods and adds specialized properties and methods • Vertebrates would be a superclass • Reptiles and mammals would be the subclasses
No multiple inheritance • Java only allows a subclass to extend one superclass • The lineage of a class is linear • An alternative way to implement multiple inheritance is by implementing multiple interfaces, which we will discuss later • The intention is to avoid the C++ problem of superclasses sharing a common base class
Protected keyword • Superclass properties and methods declared private are inaccessible to subclasses • Superclass properties and methods declared public are accessible to all classes, including subclasses of course • Superclass properties and methods declared protected are accessible to subclasses
super keyword • The super keyword works kind of like the this keyword • The this keyword can access constructors of the class or hidden instance variables out of scope in a method • The super keyword can access constructors of the superclass or hidden instance variables of the superclass out of scope to the subclass
Overriding methods • The subclass provides an alternative implementation of a method in the superclass • The method signature is identical, including the access specifier • The method must be accessible to the subclass (private methods of the superclass cannot be overridden)—but a private method in a superclass can be implemented as a method in the subclass, which creates a completely unrelated new method! (Is this a good idea?)
Overriding vs. Overloading • Overriding refers to the identical method in the superclass and subclass • Overloading refers to methods within a class with the same method name and different sets of parameters • Commonly accepted good practice is to override all overloaded methods if any overloaded method is overriden in a subclass.
java.lang.Object • All classes ultimately inherit from the Object base class in Java • If a class has no explicit inheritance using the extends keyword, it inherits from java.lang.Object by default (implicitly) • java.lang.Object provides a number of methods that can be used or overridden • The toString() method is an important one
The toString() method • Signature is public String toString(); • The default behavior of toString() is to return “className@objectAddress” where objectAddress is the memory location of the object as a hexadecimal number • Classes should provide informative overrides of toString() that can be used for debugging or logging activities (consider both uses) • The toString() method is also called if you use an object as though it were a string
Polymorphism • The third pillar of OOPS—what are the first and second? • Inheritance creates subtypes of a superclass (the supertype) • Polymorphism allows treating subtypes the same through the “interface” of the supertype • Primarily it means calling a method defined for a superclass which can be different for each subclass (implementation/behavior)
Example of polymorphism • The toString() method exists for all Java objects • toString() can be called on any object • The behavior of toString() will depend upon how it is implemented for the object • The actual toString() method called will not be known until run-time • This is called dynamic or late binding
Implicit casting • Variables for superclasses can hold references to subclass objects • person p = new employee(); • vertebrate v = new reptile(); • java.lang.Objectobj = new String(“test”); • Creating and storing an object this way is called implicit casting up the inheritance chain and is legal syntax (assuming it is correct)
Explicit casting • Objects can be cast to a particular type using the cast operator, just like primitive types • The format is var = (class)obj; • Casting a subclass object to a superclass variable is always legal and called upcasting • Casting a superclass object to a subclass variable is only legal if the object actually is an instance of, or descendant of, the subclass, and it is called downcasting
The instanceof keyword • instanceofis a java keyword • instanceof is a comparison operatorthat determines whether the left operand (an object instance) is a descendant of the right operand (a class) • The syntax is: descendant_objectinstanceofancestor_class • The return value is boolean
What is it useful for? • Programming generics (templates) • Creating general purpose methods to support subclasses with a uniform interface • Storing and manipulating classes of object with a standard interface and algorithm • For example, arrays, stacks, queues, lists, sets, sorting and searching algorithms, etc.
The equals() method • java.lang.Object has another useful method named equals() • The syntax is object1.equals(object2); • The default behavior is to whether the references are the same address in memory • Override this method to provide a way to compare two objects as to whether they are the same content*
The ArrayList Class • The size of arrays is fixed • ArrayList allows creating a variable size array of objects • ArrayList has methods for editing, searching, and manipulating the array elements • Understand what the methods listed on the next slide do (functional use, parameters, return value)
ArrayList() //creates an empty list • add(Object obj) //appends to end • add(int index, Object obj) //adds obj at index • clear() //removes all elements • contains(Object obj) //true if obj in list • get(int index) //returns obj at index • indexOf(Object obj) //returns 1st match index • isEmpty() //true if no elements • lastIndexOf(Object obj) //last match index • remove(Object obj) //removes obj from list • size() //returns total elements • remove(int index) //removes obj at index • set(int index, Object obj) //sets obj at index
The protected keyword • Declaring something with the protected modifier allows access to it by subclasses in any package and by classes in the same package. • Classes, methods, and properties can be declared using the protected modifier • A subclass may override a protected method in the superclass and make it public, but a public method in the superclass cannot be made less accessible (e.g. protected or private in subclass)
The final keyword • Declaring a class using the final keyword prevents extending the class to create subclasses • Declaring a method using the final keyword prevents overriding the class in subclasses • A final local variable is a local constant within the scope in which it is created • Static methods cannot be overridden ever