In-depth Notes on Inheritance in Java Programming

Objectives

  • Understand overriding methods
  • Grasp class hierarchies
  • Learn about visibility in inheritance
  • Design for inheritance effectively

Overriding Methods

  • Definition: A child class can override an inherited method with its own implementation.
    • Must match the parent's method signature (same name, parameters).
    • The execution context determines which version of the method runs (parent or child).
  • Example in Code:
    • Messages Class: Demonstrates overriding methods
      java public class Messages { public static void main(String[] args) { Thought parked = new Thought(); Advice dates = new Advice(); parked.message(); dates.message(); // overridden } }
  • Output:
    • I feel like I'm diagonally parked in a parallel universe.
    • Warning: Dates in calendar are closer than they appear.

Messages.java

  • Thought Class: Base for the overridden method; prints a message.
    java public class Thought { public void message() { System.out.println("I feel like I'm diagonally parked in a parallel universe."); } }
  • Advice Class: Overrides the message method of Thought.
    java public class Advice extends Thought { public void message() { System.out.println("Warning: Dates in calendar are closer than they appear."); super.message(); } }
  • Invoking Parent's Method: Uses super.message().
  • Overriding Restrictions:
    • A final method cannot be overridden.
    • Shadowing variables occurs when child classes redefine variables from parent classes (can lead to confusion).

Overloading vs Overriding

  • Overloading: Same method name with different parameters in the same class.
  • Overriding: Same method name and parameters, one in parent class and one in child class.
    • Usage: Overloading allows flexibility in methods while overriding allows specialization of methods in child classes.

Class Hierarchies

  • Definition: Structure where child classes derive from parent classes, forming a tree-like structure.
  • Siblings: Two child classes from the same parent.
  • Design Principle: Common features should reside higher up in the hierarchy to ensure all child classes inherit them.

The Object Class

  • Base Class: All classes in Java inherit from the Object class if not explicitly defined otherwise.
  • Methods: Includes useful methods such as toString() and equals(Object obj).
    • toString: Default returns class name and hash code, commonly overridden.
    • equals: Checks if two references point to the same object but can be overridden for meaningful equality checks.

Abstract Classes

  • Definition: Acts as a placeholder for a general concept in a hierarchy; cannot be instantiated directly.
    • Declaration: public abstract class Product { }
  • Characteristics:
    • May have abstract methods (no bodies) and concrete methods (fully defined).
    • Child classes must implement abstract methods or also be declared abstract.

Interface Hierarchies

  • Inheritance for Interfaces: One interface can inherit from another; implementing class must define all inherited methods.

Visibility Revisited

  • Access Control: All variables and methods are inherited, even private members (accessible via parent methods).
  • Indirect Access: Ensure encapsulation while allowing derived classes to access parent methods indirectly.

Designing for Inheritance

  • Focus on creating maintainable and reusable class designs through careful inheritance relationships.
    • Ensure that every derivation represents an is-a relationship.
    • Keep generic functionality in higher hierarchy levels.
    • Override methods where necessary for functionality.
  • Design Principles:
    • Use abstract classes for general concepts.
    • Apply visibility modifiers judiciously to maintain encapsulation.

Restricting Inheritance

  • Final Modifier: If applied to a method, it cannot be overridden. If applied to a class, no subclasses can be created.