Creating References Using Inheritance Hierarchies with Shapes

Introduction

Inheritance in object-oriented programming allows us to create a new class that is based on an existing class. This concept is fundamental for organizing and structuring code efficiently. In this lesson, we will explore inheritance hierarchies using a simple example involving shapes.

Objectives

  • Understand the basics of inheritance in Java.
  • Learn how to create and use an inheritance hierarchy with shapes.
  • Explore how references work in the context of an inheritance hierarchy.

Inheritance Basics

Inheritance allows a subclass to inherit fields and methods from a superclass. This helps in creating a hierarchical relationship between classes.

Example: Basic Shape Hierarchy

Let’s start with a basic example involving a Shape class and its subclasses.

// Superclass
class Shape {
    void draw() {
        System.out.println("Drawing a shape.");
    }
}

// Subclass
class Circle extends Shape {
    void draw() {
        System.out.println("Drawing a circle.");
    }
}

// Another subclass
class Rectangle extends Shape {
    void draw() {
        System.out.println("Drawing a rectangle.");
    }
}

// Main class to test the hierarchy
public class Main {
    public static void main(String[] args) {
        Shape myShape1 = new Circle();    // Reference of Shape pointing to Circle
        Shape myShape2 = new Rectangle(); // Reference of Shape pointing to Rectangle
        
        myShape1.draw();  // Calls overridden method in Circle
        myShape2.draw();  // Calls overridden method in Rectangle
    }
}


## Explanation:

- Shape is the base class with a method draw.
- Circle and Rectangle are subclasses of Shape, each overriding the draw method to provide specific behavior.
- In the Main class, myShape1 and myShape2 are references of type Shape, but they point to objects of Circle and Rectangle, respectively. The overridden draw method is called based on the actual object type.

Inheritance hierarchies create a structured relationship between classes, allowing for more complex designs.

## Example: Shape Hierarchy with Additional Methods
Let's expand our example by adding more functionality:


```java
// Base class
class Shape {
    void draw() {
        System.out.println("Drawing a shape.");
    }
    
    void area() {
        System.out.println("Calculating area of shape.");
    }
}

// Subclass
class Circle extends Shape {
    void draw() {
        System.out.println("Drawing a circle.");
    }
    
    void area() {
        System.out.println("Calculating area of circle.");
    }
}

// Another subclass
class Rectangle extends Shape {
    void draw() {
        System.out.println("Drawing a rectangle.");
    }
    
    void area() {
        System.out.println("Calculating area of rectangle.");
    }
}

// Main class to test the hierarchy
public class Main {
    public static void main(String[] args) {
        Shape myShape1 = new Circle();    // Reference of Shape pointing to Circle
        Shape myShape2 = new Rectangle(); // Reference of Shape pointing to Rectangle
        
        myShape1.draw();  // Calls overridden method in Circle
        myShape1.area();  // Calls overridden method in Circle
        
        myShape2.draw();  // Calls overridden method in Rectangle
        myShape2.area();  // Calls overridden method in Rectangle
    }
}

Main.main(null);
Drawing a circle.
Calculating area of circle.
Drawing a rectangle.
Calculating area of rectangle.

Conclusion

  • Inheritance hierarchies allow for organized and reusable code by establishing a hierarchy of classes. Understanding how to create and use these hierarchies, as well as how to work with references, is essential for effective object-oriented design.

Hacks

  • Create a hierarchy of Shape classes where Shape is the base class and Circle, Rectangle, and Triangle are subclasses. Implement methods to calculate and display the perimeter of each shape.

  • Add an abstract method to the Shape class that forces subclasses to implement a specific behavior, and demonstrate its usage.