1/20
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
polymorphism
subclasses have unique behavior for common superclass methods
overriding superclass methods
declaration matches superclass’ method exactly - often called virtual methods
what type of an object does java understand at runtime?
Java understands the actual type of the object at runtime. When you call a method through a superclass reference, if the actual object has an overridden version of that method, Java will call that overridden version instead.
methods that are virtual by default
all public, or protected methods on a superclass - they can be overridden by subclasses
two cases where superclass method cannot be overidden
the superclass method is private (subclasses can’t access them)
the superclass method is marked with the final keyword
@Override flag
optional - use if you intend to override superclass method and want the compiler to check that this is possible - produces a compile-time error if the method failed to be overriden
abstract method
a method with a signature but no method body - each subclass implements it with its own definition - guarantees that this method is the one implemented by all subclasses
when to mark a class as abstract?
when the class has at least one abstract method. it is also convention to include abstract in the class name when this is the case
concrete class
subclass that implements all required abstract methods
can overrided superclass methods be called by external code
no - only the subclass versions can
how to use overided method in a subclass?
use super.*method()*
can super. for methods be used anywhere in the subclass?
yes - not like constructors
to access superclass property:
add super keywoord in front of property name
object identity
a unique instance of an object - == operator returns true if two references point to the same object (same space in memory)
object equality
java.lang.Object.equals() operator - compares objects to see if they point to the same reference (identity test) - ex. two hammers
why override java.lang.Object.equals()?
to compare objects by properties, not just check identity
method signature:
public boolean equals(Object obj)
how to refer to the object that owns a method?
this. - optional
output of Object.toString() Method?
human-readable string output representing the object ex:
java.lang.Object@7d8a992fwhy override toString()?
to provide more useful and clear description of the object - can select properties to display
Given the SchoolBus class definition below...
class SchoolBus
{
private String color = "yellow";
public boolean equals(Object obj)
{
return ((SchoolBus)obj).color.equals(color);
}
}
...what will be displayed by the following code?
SchoolBus bus1 = new SchoolBus();
SchoolBus bus2 = new SchoolBus();
System.out.println( bus1.equals(bus2) );
true