the predefined field super in the code of a subclass
refers to the object of the superclass:
public class Child extends Parent {
private String myName;
public Child (String name)
{
super (); /* call no-argument constructor for parent */
this.myName = name; /* initialize local data */
}
/* omitting the hasFeature method would give the same result */
public boolean hasFeature(String feature)
{
return super.hasFeature(feature);
}
public String name()
{
return myName;
}
}
Overriding methods
- A class overrides a method when it provides another implementation
of a method its superclass provides
- examples:
- public String toString()
- public boolean equals(Object obj)
Overloading methods
- A class overloads a method when it provides another method
of the same name but with a different signature:
- examples:
- public String toString(int numberOfElements)
- public String toString(double maxValue)
- public String toString(int numberOfElements, double maxValue)
Wrapper classes
Polymorphism
Abstract Classes
- Reminder: an interface can be used to specify that a class provides
certain methods
- a class can implement any number of interfaces
- a class can have only one superclass
- if that superclass is abstract, that means
that the superclass itself cannot be instantiated
- if an abstract class has abstract methods, those methods must
be implemented by the subclass
- Number is an abstract class
Abstract Class Example
public abstract class AgriculturalPlant {
protected double plantSize;
// cannot be instantiated, so no constructor needed or possible
// non-abstract (i.e., implemented) methods may appear in an abstract class
public void grow(double fraction)
{
plantSize = plantSize * (1.0 + fraction);
}
// abstract methods must be implemented by the subclass(es)
public abstract double harvest(); // returns weight harvested
public abstract void fertilize(double fertilizerAmount);
}
class Object
- class Object is the superclass of every object class in Java,
even if not extended explicitly
- useful methods include:
boolean equals(Object obj);
String toString();
int hashCode();
Class<?> getClass();
- hashCode() will be used later in this course, when
hash tables are discussed
- getClass() can be used wih equals to test whether
two objects are in the same class. For example,
public class Foo {
public boolean equals(Object obj) {
if (this.getClass() != obj.getClass()) {
return false;
}
...
- instanceof can also be used to test class membership, e.g.
Integer x = new Integer (99);
if (x instanceof Number) {
...
Exception class Hierarchy
- Throwable is a subclass of Object
- Exception is a subclass of Throwable
- many exceptions are subclasses of Exception
- RuntimeException is also a subclass of Exception
- many common exceptions are subclasses of RuntimeException
- exceptions that are subclasses of Exception (but not
of RuntimeException) are checked exceptions: if a method
may throw such an exception, the method's declaration must say so
- runtime exceptions need not be declared, e.g.
public String readFoo() throws java.io.FileNotFoundException
{
// this constructor may throw java.io.FileNotFoundException
java.io.FileInputStream foo = new java.io.FileInputStream("foo");
java.util.Scanner s = new java.util.Scanner (foo);
// s.nextLine may throw NoSuchElementException or IllegalStateException,
// both of which are subclasses of RuntimeException, so need not be declared
return s.nextLine();
}