Inheritance

Subclasses and superclasses

Example: Suppose that class Y is inherited from class X.

Declaring a subclass

Use the keyword extends to declare the derived class
  // Example 1
  public class AAA		// AAA is the base class
  { ... }

  public class BBB extends AAA  // BBB is the derived class
  { ... }


  // Example 2
  public class Employee {...}				// base class
  public class HourlyEmployee extends Employee { ... }  // derived

The keyword super


The protected modifier


The final modifier

In addition to creating constant variable identifiers, the keyword final can be used for a couple of special purposes involving inheritance:

Method Overriding

Although the derived class inherits all the methods from the base class, it is still possible to create a method in the derived class with the same signature as one in the base. Example:

In-class Example

Here's a link to the code example we wrote up in class. Base class is Shape, and Circle is derived from Shape. The Tester.java file contains some sample calls

Abstract Classes

Methods can be abstract as well:

The Object class

In Java, every class is derived automatiacally from a class called Object. If no specific inheritance is declared for a class, it automatically has Object as a superclass.

While there are several methods in class Object, here are three important such methods, inherited by every Java class

Let's look at each.

public boolean equals(Object object)

tests whether two objects are equal
  object1.equals(object2)	// returns true if equal, false if not
				// (object1 and object2 same class type)
Default implementation is:
  public boolean equals(Object obj)
  {
	return (this == obj);
  }
Note that this default implementation is equivalent to the == operator, since it only tests the reference variables for equality. The intent is that subclasses of Object should override the equals method whenever they want a test of equality of two objects' contents.

public String toString()

returns a string that represents the object. Call format:
  objectName.toString();
The default version of the string might not always be useful, but this can be overridden in any derived class. Example for a class called Fraction:
  public String toString()
  {
    return numerator + "/" + denominator;
  }
Assuming the above function for a Fraction class, the following illustrates its usage:
  Fraction f1 = new Fraction(4,5);	// create the fraction 4/5
  System.out.print(f1.toString());	// will print "4/5"

  System.out.print(f1);		// also prints "4/5" as this
				//  always invokes a class' toString method

Examples:

public Object clone()

Remember, direct assignment between object names will only copy one reference variable to another. Use the clone() method to make copies of objects.
  newObject = someObject.clone();
Not all objects can be cloned. Only objects imeplementing the java.lang.Cloneable interface (which will be discussed later) can use the clone method.

The clone() method from the object class does a "shallow copy" (i.e. copies reference variables verbatim). If a "deep copy" is needed (a la copy constructors in C++), you should override clone() for a class.


Other methods from class Object