Notes for Java Abstract Class and Interface

Notes for Java Abstract Class and Interface

Notes for abstract class and interface:

public class ReviewQuestions {
	public static void main(String[] args) {
		new Circle9();
	}	
}

abstract class GeometricObject {
	protected GeometricObject() {
		System.out.print("A");
	}
	protected GeometricObject(String color, boolean filled) {
		System.out.print("B");
	}
}

class Circle9 extends GeometricObject {
    /** Default constructor */
	public Circle9() {
		this(1.0);
		System.out.print("C");
	}
	/** Construct circle with a specified radius */
	public Circle9(double radius) {
		this(radius, "white", false);
		System.out.print("D");
	}
	/** Construct a circle with specified radius, filled, and color */
	public Circle9(double radius, String color, boolean filled) {
		super(color, filled);
		System.out.print("E");
	}
}

Output would be BEDC because the non default constructor in the super class GeometricObject is called from the third
constructor of Circle9 class. However, if the third constructor is not called, Java system will insert a call to the default 
constructor of GeometricObject right before this(1.0) in the default constructor of Circle9. In a nutshell, the constructor
of superclass is called only once when creating a child object.
			
Number[] numberArray = new Integer[2];
numberArray[0] = new Double(1.5);

The first line is OK because it creates an array of two Integer objects and assign to super class type Number.
The second line causes run-time error because numberArray[0] is an object of Integer type (that is its actual 
data type) and it cannot be assigned to a sibling object (new Double(1.5)). However, the following is fine:
Number num = new Double(1.5); Number is a super data type.
public class TestClone {
	public static void main(String[] args) {
		try{
			GeometricObject x = new Circle(3);
			GeometricObject y = (Circle)((Circle)x).clone();
			//GeometricObject y = (Circle)(x.clone());  //x has to be converted to Circle object first.
			System.out.println(x);
			System.out.println(y);			
		}
		catch (Exception e){
			e.printStackTrace();
		}
	}
}

abstract class GeometricObject
{
	GeometricObject()
	{
		System.out.println("GeometricObject constructor ...");
	}
}

class Circle extends GeometricObject implements Cloneable
//class Circle extends GeometricObject  // This compiles but clone() returns null object.
{
	private double radius;
	Circle(double r)
	{
		radius = r;
		System.out.println("Circle constructor ...");
	}
	
	public Object clone() throws CloneNotSupportedException
	{
		return super.clone();
	}
}

/**
C:\Users\jdoe\Downloads>java TestClone
GeometricObject constructor ...
Circle constructor ...
Circle@77afea7d
Circle@161cd475
*/