-
Polymorphism
-
basics
- Polymorphism
-
superclass var can refer to an obj in sub
- but you cannot call subclass-only methods
- you can call if you cast it of a compatible type
- System.out.println(e); print obj e, if we have tostring method, it will convert to string
- Polymorphism Mechanics
- Interpreting inheritance code(in the note folder)
-
Inheritance and Design
-
basics
- be aware of similarities between classes
- think of "if I were to making the change"=>finding the relationship
-
A Misuse of Inheritance
-
Substitutability
- obj of sub can be successfully used anywhere an obj of superclass is expected
- equals in between Point and Point3D does not work is because
- equals method needs a symmetrical
-
Is-a versus Has-a
- is a is like is a lawyer
- has a is like has a lawyer
-
backward compatibility
- new code work correctly with old code
-
Interface
-
basic
- do not share code
-
Interface
- consists many methods declarations
- treat classes similarly
- when a class impl infc, it promises to provide all methods in this interface
- like a prof certificate
- e.g. Comparable, Cloneable, ActionListener, Serializable, Formattable, Runnable, Iterator
-
An interface for shapes
- public interface Shape {}
- header of methods, no body of methods
-
Abstract Method
- method is declared but not implemented
- need to implement in the class that implement this interface
- cannot instantiated, but declare interface type that can refer to the objects
-
Implementing an Interface
- declare "implements"
- implement EACH of methods
- public class <name> implements <interface> {}
-
Benefits of Interfaces
- additive not invasive
- public class <name> extends <superclass> implements <interface>, <interface>...
-
Interacting with the Superclass
-
Calling Overridden Methods
- super.<method name>(<expression>, <expression>)
- because of overriding, this is the right way to interact with method in super class
-
Accessing Inherited Fields
- when the fields are private
- using accessor or mutator to return the value, use that method in sub
-
Calling a Superclass's Constructor
- sub must call super to build a construction
- super(<expression>, <expression>)
- This action will auto initialize the fields
- superclass constructor must be the first statement in the sub constr
-
DividentStock Behavior
- super keywords only for accessing overridden methods or constructors
-
The object class
- no extends: auto extends Object
- you can declare a parameter as Object o
-
the equals method
- ==: refer to same obj
- the default equals method behaves identically to ==
- Object class can refer to any object in Java
- when you use the specific type of obj with a method in it, need cast
- So that the compiler knows it is processing same type
- usually we need to define our own equals method
-
the instanceof keyword
-
instanceof
- whether a variable refer to a given type
- <expression> instanceof <type>
- good for avoid compile error in equals method
-
Inheritance Basics
-
basics
- code reuse
-
Nonprogramming Hierarchies
-
Is a Relationship
- hierarchical connection, one is the specific version of the other
-
Inheritance Hierarchy
- a set of classes
- reuse code between classes
-
Extending a class
- inheritance
-
superclass
-
single inheritance
- only one superC
-
subclass
- extend superC
- public class <name> extends <superC> {}
- inherit copies of super
-
multi-level hierarchy
- it is legal to extend a sub when this sub extends super
-
Overriing methods
-
Override
- new version of method in sub overrides super
- name & signiture must match