Chapter 11.pptx

advertisement
Chapter 11
Inheritance
The “Is A” relationship
• Superclasses, or Base Classes are more
general.
• Subclasses, or derived classes, are created
from superclasses using the extends
keyword.
• Subclasses inherit all the fields and methods
of the superclasses from which they are
derived.
Adding to a superclass
• Subclasses can add new fields
• Subclasses can add new methods.
• Subclasses can override superclass methods.
Accessing things in the superclass
• Although you may be able to ‘reach right in’
and change the parameters in a superclass (if
they are not declared private, as they should
be), best practice is to use the ‘get’ and ‘set’
accessors and mutators.
• We do consider the inherited methods to be
methods of a subclass.
• The constructor methods are a little more
complicated.
Constructors
• The superclass’s constructor is automatically
called when a subclass is created, assuming that
the superclass has a (default or not) no-arg
constructor.
• What if the superclass has a constructor with
arguments?
– You use the keyword super as though it were a
method name, and call it with the desired arguments.
– It can only be called as the first statement in a
subclass’s constructor.
Summary of constructor issues
• The superclass constructor executes before the subclass
constructor.
• You can write a super statement to call a superclass
constructor with arguments (or none) if you want to. If so,
it must be the first statement to execute in the subclass
constructor. And you can’t do it from any non-constructor
method.
• It will be as though Java inserted a super() statement
before the first statement in the subclass’s constructor.
• If the superclass doesn’t have a no-arg constructor, then
there will be an error unless you invoke the super method
with the proper arguments.
Overriding Superclass Methods
• Subclasses may have methods with identical
signature to superclass methods. If so, it
overrides the superclass methods.
• This is how we can add new .toString() and
.equals() methods. (all classes are subclasses
of the Object class, which has these methods.)
Overloading vs. Overriding
• An overload is a different method with the
same name but a different calling sequence
(argument list including their types). The
name plus the calling sequence is referred to
as the signature. Overloads can be in the
same class, or in a subclass.
• An Override is a method with identical
signature in the subclass.
Protected Members
• Protected members (fields or methods) of a
class may be accessed by methods in a
subclass, and by methods in the same package
as the class.
• What is a package? It is a group of related
classes. I’m not familiar with how one creates
a package out of .class files, but I’m sure we
can find out when we decide to try it. 
Four levels of access
• Private – only seen from within the class
• Protected – only seen from the class, subclasses of the
class, or other classes within the same package.
• Public – seen by any program.
• (Package) access – when you don’t supply an access
specifier.
• Protected members may be accessed by subclasses,
even if they are in a different package.
• Package access is apparently a likely side effect of
forgetting to specify access.
Chains of Inheritance
• It is not just Superclass – Subclass. There can
be, and often is, a long chain of inheritance.
• After all, every class is a subclass of Object.
• Animal – Vertebrate – Mammal – Primate …
• We can even develop class hierarchies – just
like taxonomic hierarchies.
The Object Class
• Every class is a subclass of Object.
• “ … extends Object …” is one of those invisible
defaults.
• Every Object has a .toString() method, and a
.equals(…) method.
• These two methods, for which we have seen
good reason use, provide an understanding of
why we want to override.
Polymorphism
• We have seen a lot of statements like:
Rectangle myRectangle = new Rectangle();
• Doesn’t it seem odd that the class name
rectangle appears twice in that statement?
• Why do that? Thinking of an array of objects
starts to make sense.
• A zoo might have an array of animals in it.
• But there is no point in having different types of
animals unless we can figure out how to get at
and use their different characteristics.
instanceof operator
Animal[] a = new Animal[3];
a[0] = new Mammal();
a[1] = new Vertebrate();
a[2] = new Animal();
for (int i = 0; i<a.length; i++){
if (a[i] instanceof Animal) {
System.out.println("Critter " + i + " is an Animal");
}
if(a[i] instanceof Vertebrate) {
System.out.println("Critter " + i + " is a Vertebrate");
}
if (a[i] instanceof Mammal) {
System.out.println("Critter " + i + " is a Mammal");
}
}
11 – 4
Polymorphism II – getting at methods
Abstract Classes and Methods
Polymorphism and Methods
• Which method gets called when a superclass
variable references a subclass object, and the
subclass overrides the method?
– Answer: Dynamic Binding means that at run time,
the Java Virtual Machine resolves the situation
and invokes the Subclass’s version of the method.
How do you invoke a method that is in
the subclass, but not in the superclass?
• Answer: You must cast the object into the
subclass.
Animal a = new Mammal();
((Mammal) a).setTeeth(36);
• It is best to protect the operation in an if using
instanceof:
if (a[i] instanceof Mammal) {
((Mammal) a[i]).setTeeth(…);
}
Abstract Classes
• An Abstract class is not instantiated, but other
classes extend it.
– abstract between accessspecifier and
class.
– public abstract class Animal {
• Although the class is not instantiated, it may
have fields and non-abstract methods.
– Contrast to interfaces, tomorrow’s topic, which
have no fields or non-abstract methods.
Abstract Methods
• An abstract method has no body and must be
overridden in a subclass.
– abstract between public and the type.
– public abstract String getRespType();
– No “{“ and “}”, only a “;” at the end.
• The author seems to imply that a non-abstract
class can contain an abstract methods. Eclipse
disagrees.
Interfaces
• An interface specifies the behavior of a class.
• It takes the form:
public interface InterfaceName {
…}
• It is not a class, and you don’t extend it, you
implement it.
• The “body” of an interface consists of empty
method descriptions, much like the abstract
method declarations in abstract classes.
Method descriptions in interfaces
• The method descriptions just consist of the
type plus the signature of the method – its
method name and its argument
type/argument list.
• public interface Relatable {
boolean equals(GradedActivity g);
boolean isGreater(GradedActivity g);
boolean isLess(GradedActivity g);
}
Using Interfaces
• The keyword implements and the interface name
go after the class name.
• Public class FinalExam3
extends GradedActivity
implements Relatable {…
• There can be more than one interface name in a
single implements clause (the names are
separated by commas)
• The interface is a “contract” that you must fulfill.
Fields in Interfaces
• Interfaces can have fields, but they treated as
both static and final.
• The custom for finals is to use all uppercase
letters.
• This is often used, as are enumerations, as
way of avoiding to have to memorize and use
artificial code numbers.
Interface reference variables
• A variable can be declared as a “type” using the name
of an interface.
• This is an example of polymorphism, similar to the way
you can declare a variable to be of a superclass type.
• When you do declare a variable to be an interface type,
you can only directly use the methods of that interface.
• To use methods of the real subclass in which you
instantiate the variable, you must use casts.
• As with superclasses, you may want to ‘protect’ those
casts with instanceof operators in if
statements.
Download