Java Concepts Chapter 13: Inheritance
tests whether two references are to the same object
== method
mechanism for extending existing classes by adding methods and fields
Inheritance
in enumeration classes, the toString method returns
a string that equals the objects name
class for which you cannot create obects
abstract class
how do abstract classes differ from interfaces
abstract classes can have instance fields and they can have concrete methods and constructors
protected features can be accessed by
all subclasses and all classes in the same package
The subclass inherits these from the superclass
behavior and state
when you call the clone method you must use a
cast ex. BankAccount clonedAccount = (BankAccount) account.clone();
how to wrtie equals method
cast the otherObject parameter to the type of your class, and then compare the fields of the implicit parameter and the other parameter
makes a new object with the same state as an existing object
clone method
One important reason got inheritance
code reuse
a class which you can create objects for
concrete class
makes it so that no one can extend the class
declaring the class final (you can also declare methods as final so that they can't be overridden)
a type with a finite number of values
enumerated type (enum)
tests whether two objects have the same contents
equals method
Subclasses inherit all
fields from the superclass
testing whether two objects belong to the same class
if (otherObject == null) return false; if (getClass() != otherObject.getClass()) return false;
inheriting methods
if you do not explicitly override a superclass method you automatically inherit it; the superclass methods can be applied to the subclass obects
overriding a method
if you specify a method with the same signature (same name and same parameter types) it overrides the method of the same name in the superclass
failing to invoke the superclass method results in
infinite recursion
sets of classes can form complex
inheritance hierarchies
You can never override
instance fields
must be defined in the subclass
new methods and instance fields
things you can do to methods in a subclass
override, inherit, define new
all methods of classes in the same package can access the feature
package access
the default access when no access modifier is given
package access
can only be accessed by the methods of their own class
private features
a subclass has no access to
private fields of its superclass
four levels of controlling access to fields
public access, private access, protected access, package access
can be accessed by methods of all classes
public features
If you want your toString method to be usable by a subclass
return getClass().getName() + desired print info
Denotes inheritance
solid line with hollow triangle tip
more specialized class that inherits from the superclass
subclass
Any new instance fields that you define in the subclass are present only in
subclass objects
can be converted into superclass references
subclass references
use this keyword to call a method of the superclass
super ex. super.deposit(amount);
more general class that forms the basis for inheritance
superclass
instanceof operator
tests whether an object belongs to a particular type
In Java, every class that does not specifically extend another class extends this
the class Obect
in enumeration classes, the clone method returns
the given object WITHOUT making a copy
If you define a method that does not exist in the superclass then,
the new method can only be applied to subclass obects
method calls are always determined by
the type of the actual object (it doesn't matter if the object reference is stored in a different field type)
calling a superclass constructor
use the super keyword in the first statement of the subclass constructor super(initialBalance)
define the toString method to
yield a string that describes the object state