Things that the Java compiler does automatically

  • Previously, we have learned that the Java compiler does these things "secretly" (= automatically) based on some rules of object creation:

      1. Every object must be initialized, therefore:

          • Every class must have a constructor method

          • If a class does not have one: the Java compiler will insert the default constructor

      2. Every subclass object contains a superclass object (that must be initialized), therefore:

          • A subclass constructor must invoke super(...) as its first statement

          • If not so: the Java compiler will insert super(); at the beginning

There is one more automatic insertion that the Java compiler will do...

  • The designers of the Java programming language wants to achieve the following inheritance hierachy:

     

    • Every class in Java is descended from one special class called the Object class

    • I.e.: the Object class is the parent class of every class in any Java program

How Java makes sure every class will inherit from the Object class

  • If no inheritance is specified when a class is defined:

     

     

     

How Java makes sure every class will inherit from the Object class

  • The Java compiler will automatically insert:    extends Object in the class definition:

  • Result:

      • Every class in Java inherits from the Object class

      • Every object in Java will have all the methods defined in the Object class !

The inheritance hierarchy of Java classes

  • Because every class in Java will extend some class (by default, a class extends Object), Java's classes is organized as a tree hierarchy:

       Cat is-a Animal   
       Cat is-a Object also because:  Animal is-a Object
    

Let look at an important method in the Object class:   toString( )

The toString() method in the Object class in Java's library

  • The online documentation on the Object class can be found here:

  • The most useful instance method defined in the Object class is:

      • toString(): returns a string representation of this object

  • Every class in Java will inherit the toString() method

  • Furthermore:

      • The System.out.print( ) method invokes the toString( ) method so it can print out any object !

  • Therefore:

      • We can control the print out of an object by overriding the toString() in a class !!

Using and overriding the inherited toString() method

Example that shows that the GeometricObject class inherited the toString() method from the Object class

public class GeometricObject
{
    private String color;
    
    GeometricObject( String col )
    {
        color = col;
    }

    public String getColor()          
    { 
        return color; 
    }

    public double getArea()  // Dummy method
    {
        return 0;  // Some default value
    }

    // No "toString()" method defined !
    // Inherits "toString()" from Object !
} 
public class myProg
{
    public static void main(String[] args)
    {
        GeometricObject a = 
               new GeometricObject("red");

        // We can call the toString() method 
        // using a GeometricObject object    
        System.out.println( a.toString( ) );
    }
}









  

DEMO: demo/04-inheritance/15-toString

Using and overriding the inherited toString() method

Example that shows that the Circle class also inherited the toString() method:

public class Circle extends GeometricObject 
{
    private double radius;

    Circle(String col, double r)
    {
        super(col);
        radius = r;
    }

    public double getRadius()         
    { 
        return radius;
    }

    public double getArea()
    {
        return 3.14159*radius*radius;
    }

    // Inherits "toString()" from GeomObject
} 
public class myProg
{
    public static void main(String[] args)
    {
        Circle a =
               new Circle("red", 2.0);

        // We can call the toString() method 
        // using a Circle object    
        System.out.println( a.toString( ) );
    }
}









  

DEMO: demo/04-inheritance/16-toString

Using and overriding the inherited toString() method

We can override the inherited toString() method to print out an object in a more suitable format:

public class Circle extends GeometricObject 
{
    private double radius;

    Circle(String col, double r)
    {
        super(col);
        radius = r;
    }

    ...



    // Override the "toString()" method
    public String toString()
    {
        return "Color = " + getColor() 
                + " : "
                + "radius = " + radius;
    }
} 
public class myProg
{
    public static void main(String[] args)
    {
        Circle a =
               new Circle("red", 2.0);

        // We can call the toString() method 
        // using a Circle object    
        System.out.println( a.toString( ) );

        System.out.println(  a  );  // Same !
    }
}







  

DEMO: demo/04-inheritance/17-toString