Understanding Class Hierarchies

Class hierarchies in Java enable structured relationships between classes through inheritance. This concept is foundational to object-oriented programming and enhances code reusability and organization, which simplifies managing complex systems.

In a class hierarchy, a superclass can pass down properties and methods to its subclasses. This allows subclasses to inherit functionality while also being able to define or modify behaviors that are specific to themselves. This leads to a more manageable code structure and helps in maintaining code consistency.

Components of Class Hierarchies

  1. Superclass: The parent class from which properties and methods are inherited.
  2. Subclass: A child class that inherits from the superclass and can override or extend its functionality.
  3. Polymorphism: The ability to treat objects of different classes through a common interface, allowing for method overriding.

Example Hierarchy

public class Shape {
    protected String name;

    public Shape(String name) {
        this.name = name;
    }

    public double calc_area() {
        return 0.0;
    }

    public void print_shape() {
        System.out.println("Shape: " + name);
    }

    public String draw() {
        return "Drawing a shape";
    }
}

Derived Class: Rectangle

Now, let’s create a Rectangle class that inherits from Shape.

public class Rectangle extends Shape {
    private int length;
    private int width;

    public Rectangle(String name, int length, int width) {
        super(name);
        this.length = length;
        this.width = width;
    }

    @Override
    public double calc_area() {
        return length * width;
    }
}

class Circle extends Shape {
    public int radius;

    public Circle(String name, int radius) {
        super(name);
        this.radius = radius;
    }

    @Override
    public double calc_area() {
        return Math.PI * radius * radius;
    }

    @Override
    public String draw() {
        return "Drawing a circle";
    }
}


Derived Class: Triangle

Next, let’s create a Triangle class that also inherits from Shape.

public class Triangle extends Shape {
    private int side1;
    private int side2;
    private int side3;

    public Triangle(String name, int s1, int s2, int s3) {
        super(name);
        this.side1 = s1;
        this.side2 = s2;
        this.side3 = s3;
    }

    @Override
    public double calc_area() {
        double s = (side1+side2+side3)/2;
        double area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
        return area;
    }

    @Override
    public String draw() {
        return "Drawing a triangle";
    }
}

class Square extends Rectangle {
    public Square(String name, int sideLength) {
        super(name, sideLength, sideLength);
    }

    // NOTE: SINCE SQUARE EXTENDS RECTANGLE, WE DON'T NEED TO OVERRIDE CALC_AREA()

    @Override
    public String draw() {
        return "Drawing a square";
    }
}

Testing Our Classes

Let’s create instances of Rectangle and Triangle and call their methods.

public class Main {
    public static void main(String[] args) {
        Shape myCircle = new Circle("Circle 1", 5);
        Shape mySquare = new Square("Square 1", 2);

        System.out.println(myCircle.draw()); // Outputs: Drawing a circle
        System.out.println(mySquare.draw()); // Outputs: Drawing a square
    }
}

Main.main(null);
Drawing a circle
Drawing a square

In the example above, Shape acts as the superclass, while Circle and Square are subclasses that override the draw method to provide specific behavior. This demonstrates polymorphism, enabling us to reference subclasses using a superclass type and invoke the overridden methods dynamically at runtime. This is particularly powerful as it allows for flexibility in the code, facilitating easier changes and enhancements.

Benefits of Using Class Hierarchies

  1. Code Reusability: Common behaviors are defined in the superclass, significantly reducing code duplication and making it easier to maintain and update.
  2. Organized Structure: A clear hierarchical structure in your code aids in understanding the relationships between classes, making the codebase more intuitive and manageable.
  3. Polymorphism: Allows for dynamic method resolution, enabling methods to be invoked on objects of subclasses through references of the superclass type.

Practical Exercise: Popcorn Hack 1

Let’s implement the Triangle subclass to deepen your understanding. Below is a half-completed method for the Triangle class. Your task is to complete the draw method:

class Shape {
    public String draw() {
        return "Drawing a shape";
    }
}

class Triangle extends Shape {
    @Override
    public String draw() {
        // TODO: Implement this method
    }
}

public class Main {
    public static void main(String[] args) {
        Shape myTriangle = new Triangle();
        System.out.println(myTriangle.draw()); // Should output: "Drawing a triangle."
    }
}

Make sure your implementation returns a unique string for the Triangle class. This exercise will help reinforce how subclasses can extend functionality.

Expanding Your Skills: Adding a Rectangle Class

Next, let’s implement the Rectangle subclass. Below is the basic setup for it. Your task is to implement the draw method for the Rectangle class:

class Rectangle extends Shape {
    @Override
    public String draw() {
        // TODO: Implement this method
    }
}

public static void main(String[] args) {
    Shape myRectangle = new Rectangle();
    System.out.println(myRectangle.draw()); // Should output: "Drawing a rectangle."
}

Complete the draw method in Rectangle, ensuring it returns a unique string. This will reinforce how multiple subclasses can have distinct implementations of the same method, enhancing your understanding of class hierarchies.

Advanced Challenge: Area Calculation

Now, let’s enhance our Shape class to include an area calculation feature. Modify the Shape class to include an area method, and implement it in your subclasses. Below is a structure to help you get started:

```java class Shape {
public String draw() {
    return "Drawing a shape";
}
public double area() {
    return 0; // Default implementation
} }

class Circle extends Shape {
    @Override
    public double area() {
        // TODO: Implement area calculation
    }
}

class Square extends Shape {
    @Override
    public double area() {
        // TODO: Implement area calculation
    }
}
// Implement for Triangle and Rectangle as well
``` Ensure each subclass calculates and returns its area correctly. This will allow you to practice method overriding further and understand how different shapes can extend base functionalities.

Homework Hack

For your homework, create your own class hierarchy for shapes. You should have a base class called Shape with subclasses Triangle, Rectangle, and Hexagon. Each subclass should implement a method called draw(), returning a unique string for each shape type.

- `Triangle`: "Drawing a triangle."

- `Rectangle`: "Drawing a rectangle."

- `Hexagon`: "Drawing a hexagon."

Make sure to demonstrate polymorphism by creating an array of Shape types and iterating through it to call the draw() method. This will reinforce your understanding of class hierarchies and method overriding.

// I was editing the code in the code cells above, so I have copied it down here + addeed the hexagon

public class Shape {
    protected String name;

    public Shape(String name) {
        this.name = name;
    }

    public double calc_area() {
        return 0.0;
    }

    public void print_shape() {
        System.out.println("Shape: " + name);
    }

    public String draw() {
        return "Drawing a shape";
    }
}

public class Rectangle extends Shape {
    private int length;
    private int width;

    public Rectangle(String name, int length, int width) {
        super(name);
        this.length = length;
        this.width = width;
    }

    @Override
    public double calc_area() {
        return length * width;
    }

    @Override
    public String draw() {
        return "Drawing a rectangle";
    }
}

class Circle extends Shape {
    public int radius;

    public Circle(String name, int radius) {
        super(name);
        this.radius = radius;
    }

    @Override
    public double calc_area() {
        return Math.PI * radius * radius;
    }

    @Override
    public String draw() {
        return "Drawing a circle";
    }
}


public class Triangle extends Shape {
    private int side1;
    private int side2;
    private int side3;

    public Triangle(String name, int s1, int s2, int s3) {
        super(name);
        this.side1 = s1;
        this.side2 = s2;
        this.side3 = s3;
    }

    @Override
    public double calc_area() {
        double s = (side1+side2+side3)/2;
        double area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
        return area;
    }

    @Override
    public String draw() {
        return "Drawing a triangle";
    }
}

class Square extends Rectangle {
    public Square(String name, int sideLength) {
        super(name, sideLength, sideLength);
    }

    // NOTE: SINCE SQUARE EXTENDS RECTANGLE, WE DON'T NEED TO OVERRIDE CALC_AREA()

    @Override
    public String draw() {
        return "Drawing a square";
    }
}

class RegularHexagon extends Shape {
    int sideLength;

    public RegularHexagon(String name, int sideLength) {
        super(name);
        this.sideLength = sideLength;
    }

    @Override
    public double calc_area() {
        return 3*Math.sqrt(3)/2 * sideLength * sideLength;
    }

    @Override
    public String draw() {
        return "Drawing a hexagon";
    }
}

public class Main {
    public static void main(String[] args) {
        Rectangle rect = new Rectangle("rect 1", 2, 6);
        Square square = new Square("square 1", 25);
        RegularHexagon hex = new RegularHexagon("rect 1", 82);
        Triangle tri = new Triangle("triangle 1", 3, 4, 5);
        Circle circle = new Circle("circle 1", 4);

        System.out.println(rect.draw());
        System.out.println(square.draw());
        System.out.println(hex.draw());
        System.out.println(tri.draw());
        System.out.println(circle.draw());

        System.out.println();

        System.out.println("Area of rectangle: " + rect.calc_area());
        System.out.println("Area of square: " + square.calc_area());
        System.out.println("Area of hexagon: " + hex.calc_area());
        System.out.println("Area of triangle: " + tri.calc_area());
        System.out.println("Area of circle: " + circle.calc_area());
    }
}

Main.main(null);
Drawing a rectangle
Drawing a square
Drawing a hexagon
Drawing a triangle
Drawing a circle

Area of rectangle: 12.0
Area of square: 625.0
Area of hexagon: 17469.464445139696
Area of triangle: 6.0
Area of circle: 50.26548245743669