Review:    automatic insertion of the default constructor

  • Constructors are invoked to create objects

    Therefore:

      • Every class must have at least one constructor

  • If a class does not have any constructor, then the Java compiler will automatically insert this constructor for you:

          className() { // no statements }    

  • The constructor that is inserted by the Java compiler is called:  

      • The "default constructor"

Review:    automatic insertion of the default constructor

The Circle class has no constructors defined:

public class Circle 
{
    double radius = 1;       /** The radius of this circle */

    // Circle has no constructors defined....
  
    // The Java compiler will insert the default constructor !
       
}

public static void main(String[] args) { Circle a = new Circle(); // Invokes the default constructor !! Circle b = new Circle(2); // Compile error (constructor not found) }

The Circle class has no constructors, so the Java compiler will insert the default constructor

Review:    automatic insertion of super() to invoke constructor in superclass

A subclass object always contains a superclass object:

And objects are initialized using a constructor...

Review:    automatic insertion of super() to invoke constructor in superclass

Therefore:   a constructor in the subclass must invoke some constructor in its superclass as its first statement:

The keyword super( ... ) is used to invoke a constructor in its superclass

Review:    automatic insertion of super() to invoke constructor in superclass

If a constructor in the subclass does NOT invoke any constructor in its superclass as its first statement:

Then:   the Java compiler will automatically insert the call super( ) as the first statement

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

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

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

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:

  • Because every class in Java inherits from the Object class, every object in Java will have the methods defined in the Object class !

The Object class in Java's library

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

  • The most useful instance methods defined in the Object class are:

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

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

      • clone(): returns a real copy of this object

          You can use the clone( ) method to make a copy of an array instead of using a for-loop !

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 !
} 
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( ) );
    }
}








  

Using and overriding the inherited toString() method

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

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;
    }

    // No "toString()" method defined !
} 
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( ) );
    }
}








  

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( ) );
    }
}








  

Copying Array objects with the clone() method

  • We have discussed how to copy an Array object before: ()

        public static void main(String[] args) 
        {
            double[] myList = {34, 15, 66, 7};
          
            double[] myListCopy = new double[ myList.length ];
     
            for ( int i = 0; i < myList.length; i++ )
                myListCopy[i] = myList[i];
        } 

Copying Array objects with the clone() method

  • We can also copy an Array object (in fact, any kind of object) using the clone() method as follows:

    import java.util.Arrays;
    
    public class myProg
    {
        public static void main(String[] args)
        {
            double[] myList = {34, 15, 66, 7};
          
            double[] myListCopy = myList.clone();
     
            myListCopy[0] = 99;  // Make sure it's a copy
            
            System.out.println( Arrays.toString(myList) );
            System.out.println( Arrays.toString(myListCopy) );
        }
    }