Object Superclass

Learning Targets:

  • What is the Object class
  • Why is the Object class important to remember

Every class and object created without the extends keyword will be implicitly extended from the Object Superclass. This means it will inherit some basic methods. Some notable methods are:

  1. getClass()
  2. toString()
  3. equals()

So What?

Well its important to keep in mind when writing out your class. If you are planning to have a method in your class/object that matches the basic Object, then it must be a public override because all of the Object methods are public.

  • are some methods from Object such as getClass() that you cannot override.
// this will return an error
class Shape {
    String toString(){
        return "Shape";
    }
}
// this will be fine
class Shape{
    @Override
    public String toString(){
        return "Shape";
    }
}

Popcorn Hacks

Create an example where you execute an unchanged method from Object, then execute a different method from Object that you changed.

class Shape {
    public String draw() {
        return "Drawing a shape";
    }
    public double area() {
        return 0; // Default implementation
    }
    @Override
    public String toString() {
        return "turned me into a string, funniest thing ive ever seen";
    }
}

class Circle extends Shape {
    private int radius;
    public Circle(int radius) {
        this.radius = radius;
    }

    @Override
    public double area() {
        return Math.PI*Math.pow(radius, 2);
    }
}

class Square extends Shape {
    private int length;
    public Square(int length) {
        this.length = length;
    }
    @Override
    public double area() {
        return Math.pow(length, 2);
    }
}

class Triangle extends Shape {
    private int side1;
    private int side2;
    private int side3;
    public Triangle(int s1, int s2, int s3) {
        this.side1 = s1;
        this.side2 = s2;
        this.side3 = s3;
    }
    
    @Override
    public double area() {
        double area = 0;
        int s = (side1 + side2 + side3) / 2;
        area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
        return area;
    }
}

class Rectangle extends Shape {
    private int length;
    private int width;
    public Rectangle(int length, int width) {
        this.length = length;
        this.width = width;
    }
    @Override
    public double area() {
        return length*width;
    }
}

public class Main {
    public static void main(String[] args) {
        Rectangle aaah = new Rectangle(10, 5);
        Triangle beaa = new Triangle(3, 4, 5);
        Square dddd = new Square(5);
        Circle ddaa = new Circle(10);

        System.out.println(aaah.area());
        System.out.println(beaa.area());
        System.out.println(dddd.area());
        System.out.println(ddaa.area());

        System.out.println(aaah.getClass()); 

        System.out.println(aaah.toString()); 
    }
}

Main.main(null);
50.0
6.0
25.0
314.1592653589793
class REPL.$JShell$16$Rectangle
turned me into a string, funniest thing ive ever seen