Inheritance lets a new class reuse and extend an existing class. Overriding lets the new class change behaviour that it inherits.
Defining and Extending Classes
Inheritance is the mechanism by which one class acquires the fields and methods of another. The existing class is the superclass (base or parent class), and the new class is the subclass (derived or child class). It is declared with extends: class Dog extends Animal { ... }. The subclass inherits all non-private members, can add new fields and methods, and can change inherited methods. Inheritance models an is-a relationship: a Dog is an Animal. It gives code reuse and supports polymorphism.
Definition: Inheritance. The mechanism by which a subclass acquires the fields and methods of a superclass, declared in Java with the keyword extends.
Types of Inheritance
Single inheritance: one subclass from one superclass. Multilevel: a chain, such as A → B → C. Hierarchical: several subclasses from one superclass. Multiple inheritance, a class with several superclasses, is not allowed for classes in Java, to avoid ambiguity (the diamond problem), but is achieved with interfaces. Hybrid inheritance combines types, again through interfaces.
The super Keyword
super refers to the superclass. super(arguments) calls the superclass constructor, and must be the first statement of the subclass constructor; if it is omitted, Java calls the no-argument superclass constructor automatically. super.method() calls the superclass version of an overridden method, and super.field reaches a hidden field. Constructors run from the top of the hierarchy downwards.
Method Overriding
Method overriding is defining in a subclass a method with the same name, parameters and return type as a method in its superclass. When the method is called on a subclass object, the subclass version runs. A superclass reference can refer to a subclass object, Animal a = new Dog();, and a.sound() runs Dog's version. The method is chosen at run time by the actual object, which is dynamic method dispatch or run-time polymorphism. The @Override annotation asks the compiler to check the override. The overriding method cannot have weaker access. final methods cannot be overridden, final classes cannot be extended, and static methods are hidden rather than overridden. An abstract method has no body and must be overridden, and a class with one is abstract and cannot be instantiated.
Table 3.5: Overloading and overriding
| Feature | Overloading | Overriding |
|---|---|---|
| Where | Same class | Superclass and subclass |
| Parameters | Must differ | Must be the same |
| Binding | Compile time | Run time |
| Purpose | Same action, different inputs | Change inherited behaviour |
Example 3.5: Shape and Circle
Problem: Create a class Shape with getArea() and getPerimeter(), and a subclass Circle that overrides them for a radius of 7. Call them through a Shape reference.
Solution:
- Shape gives default methods returning 0.
- Circle extends Shape, stores the radius, and overrides both methods: area = πr², perimeter = 2πr.
- Shape s = new Circle(7); s.getArea() runs Circle's version by dynamic dispatch.
Result: Area = 153.94 and perimeter = 43.98.
public class ShapeDemo {
public static void main(String[] args) {
Shape s = new Circle(7); // superclass reference
System.out.printf("Area = %.2f%n", s.getArea());
System.out.printf("Perimeter = %.2f%n", s.getPerimeter());
System.out.println("Type: " + s.name());
}
}
class Shape {
double getArea() { return 0; }
double getPerimeter() { return 0; }
String name() { return "Shape"; }
}
class Circle extends Shape {
double r;
Circle(double r) { super(); this.r = r; }
@Override double getArea() { return Math.PI * r * r; }
@Override double getPerimeter() { return 2 * Math.PI * r; }
@Override String name() { return "Circle, a " + super.name(); }
}
Output
Area = 153.94
Perimeter = 43.98
Type: Circle, a Shape
Common mistakes
- Trying to extend two classes at once. Java allows only one superclass.
- Putting super(...) anywhere other than the first line of a constructor.
- Changing the parameters when overriding, which creates an overload instead.
Key takeaways
- extends creates a subclass that inherits non-private members.
- Java supports single, multilevel and hierarchical inheritance of classes.
- super calls the superclass constructor or methods.
- Overriding is resolved at run time by the object's actual class.
Practice questions
- What is method overriding?
- Why does Java not allow multiple inheritance of classes?
- What does super() do?
Answers to the practice questions
- Redefining in a subclass a superclass method with the same name, parameters and return type.
- To avoid ambiguity when two superclasses have members with the same name (the diamond problem).
- It calls the superclass constructor and must be the first statement in a subclass constructor.
Sources
- Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit III
