Java Inheritance
Core Concepts
Inheritance has the same core concepts in any object-oriented language
- One class is derived from another, inherits all its data and
methods
- Terminology
- superclass, base class, parent class
- subclass, derived class, child class. Also called an extended
class in Java
Inheritance in Java
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
keyword super
You can use super like a function call in a derived class
constructor -- invokes the base class constructor
super(); // invokes base class default constructor
super(parameters); // invokes base class constructor with parameters
// Example, for a class called HourlyEmployee, derived from Employee
public class HourlyEmployee extends Employee
{
public HourlyEmployee() // default constructor
{
super(); // invokes Employee() constructor
}
public HourlyEmployee(double h, double r)
{
super(h,r); // invokes Employee constructor w/ 2 parameters
}
// ... more methods and data
} // end class HourlyEmployee
- The call to super() must be the first line of the derived
class constructor
- If explicit call to parent constructor not made, the subclass'
constructor will automatically invoke super(). (the
default constructor of the base class, if there is one)
- Can also use super to invoke a method from the parent class
(from inside the derived class). Format:
super.method(parameters)
The protected modifier
- Like in C++, protected data and methods of a public class can
be accessed by any classes derived from the given class.
- In Java, a protected member can also be accessed by any class
in the same package
The final modifier
Use in Java for a few special inheritence-related purposes:
- When used on a class declaration, it means that the class cannot be
extended. (i.e. it cannot become a parent class to a new subclass)
- When used on a method declaration, it means that the method cannot be
overridden in a subclass. (i.e. this is the final version of the
method)
Other differences
- Java does not have multiple inheritance. A class can only
extend one other class
- C++ has multiple inheritance (a class can be derived from more than
one base class
- Java has a construction known as an interface. This is
actually a keyword in Java. It allows behavior similar to inheritance,
especially regarding polymorphism
- In Java, every class is part of an inheritance hierarchy,
because the class called
Object
(java.lang.Object) is automatically a base class to every Java
class
Method Overriding
Just like in C++, this is when a derived class has a method with the
same prototype as a method in the base class. (The derived class function
overrides the base class version, when called for a derived
object).
Example:
- Suppose a class Rectangle is derived from class Shape.
- Shape has a method:
void Draw() { ... }
- We can define a method in class Rectangle with the same signature.
The derived class version will override the base class version,
when called through an object of type Rectangle.
Rectangle r = new Rectangle(); // create a Rectangle object
// which has all the Shape methods available
r.Draw(); // invokes the Draw method from the
// Rectangle class
- Note that the Rectangle class' Draw() method can still invoke
the superclass' method, with the keyword super
public void Draw()
{
super.Draw(); // invoke parent's Draw()
// continue with any processing specific to Rectangle
}
Abstract Classes
- Like in C++, an abstract class is one that cannot be
instantiated
- To make a class abstract in Java, use the keyword abstract
(which is a modifier)
public abstract class Shape
- Now that Shape is abstract, this would be illegal:
Shape s = new Shape(); // specifically, it's new Shape();
// that is illegal
Java methods can be abstract as well: