Defining a class

How to define a class that you can use to construct Circle objects:

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

    public Circle() { }             /** Constructor 1 for a circle object */

    public Circle(double newRadius) /** Constructor 2 for a circle object */
    {
        radius = newRadius;
    }

    public double getArea()      /** Return the area of this circle */
    {
        return 3.14159 * radius * radius;
    }

    public void setRadius(double newRadius) /** Set new radius for this circle */
    {
       radius = newRadius;
    }
} 

Using the Circle class to construct some circles

We can use the Circle class to create 2 Circle objects as follows:

    public static void main()
    {
        Circle circle1 = new Circle();  
                         // Invokes Circle() to make this circle

        Circle circle2 = new Circle(2); 
                         // Invokes Circle(double) to make this circle
    }
             

DEMO: demo/03-classes/01-class - in BlueJ - show the objects in main().      Step into the constructors

UML: a standardized modeling representation description

The Unified Modeling Language representation of classes and objects:

    public static void main()
    {
        Circle circle1 = new Circle();  
                         // Invokes Circle() to make this circle

        Circle circle2 = new Circle(2); 
                         // Invokes Circle(double) to make this circle
    }
             

How a class definition represents objects

A Java class uses variables to define data fields (properties) of objects:

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

    public Circle()                 /** Constructor 1 for a circle object */
    { 
    } 

    public Circle(double newRadius) /** Constructor 2 for a circle object */
    {
        radius = newRadius;
    }

    public double getArea()      /** Return the area of this circle */
    {
        return 3.14159 * radius * radius;
    }

    public void setRadius(double newRadius) /** Set new radius for this circle */
    {
       radius = newRadius;
    }
} 

How a class definition represents objects

A Java class uses methods to define the actions/behaviors of objects:

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

    public Circle()                 /** Constructor 1 for a circle object */
    { 
    }

    public Circle(double newRadius) /** Constructor 2 for a circle object */
    {
        radius = newRadius;
    }

    public double getArea()      /** Return the area of this circle */
    {
        return 3.14159 * radius * radius;
    }

    public void setRadius(double newRadius) /** Set new radius for this circle */
    {
       radius = newRadius;
    }
} 

How a class definition represents objects

Important note:     methods to define the actions of objects do not have the static qualifier

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

    public Circle()                 /** Constructor 1 for a circle object */
    { 
    }

    public Circle(double newRadius) /** Constructor 2 for a circle object */
    {
        radius = newRadius;
    }

    public double getArea()      /** Do NOT use static qualifier ! */
    {
        return 3.14159 * radius * radius;
    }

    public void setRadius(double newRadius) /** Do NOT use static qualifier ! */ 
    {
       radius = newRadius;
    }
} 

Constructors

A class provides special methods called constructors which are invoked (only) to create a new object:

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

    public Circle()                 /** Constructor 1 for a circle object */
    { 
    }

    public Circle(double newRadius) /** Constructor 2 for a circle object */
    {
        radius = newRadius;
    }

    public double getArea()      /** Return the area of this circle */
    {
        return 3.14159 * radius * radius;
    }

    public void setRadius(double newRadius) /** Set new radius for this circle */
    {
       radius = newRadius;
    }
} 

Constructors are designed to perform initializing actions, such as initializing the data fields of objects

The main( ) method is optional

A class that represents real world objects usually does not need a main( ) method:

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

    public Circle()                 // called when you use: new Circle()
    {
    }

    public Circle(double newRadius) // called when you use: new Circle(number)
    {
        radius = newRadius;
    }

    public double getArea()      /** Return the area of this circle */
    {
        return 3.14159 * radius * radius;
    }

    public void setRadius(double newRadius) /** Set new radius for this circle */
    {
       radius = newRadius;
    }
} 

Without a main( ) method, such class cannot be run as a Java program

The main( ) method is optional

You may put a main( ) method in the Circle class to test the methods:

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

    public static void main(String[] args) 
    {                                      
        Circle circle1 = new Circle();     
        Circle circle2 = new Circle(2);    
    }                                      

    public Circle() { }             // called when you use: new Circle()

    public Circle(double newRadius) // called when you use: new Circle(number)
    {
        radius = newRadius;
    }

    public double getArea()      /** Return the area of this circle */
    {
        return 3.14159 * radius * radius;
    }

    public void setRadius(double newRadius) /** Set new radius for this circle */
    {
       radius = newRadius;
    }
} 

DEMO: demo/03-classes/02-constructor --- I prefer to write a separate class to do the testing...

How to use the methods on an object

Example invoking methods on objects:

    public static void main()
    {
        Circle circle1 = new Circle();  // Create a Circle object circle1
        Circle circle2 = new Circle(2); // Create a Circle object circle2

	double area1 = circle1.getArea(); // Tell circle1 to run getArea()
	System.out.println("Area of circle1 = " + area1);

	double area2 = circle2.getArea(); // Tell circle2 to run getArea()
	System.out.println("Area of circle2 = " + area2);


	circle1.setRadius(5);          // Tell circle1 to run setRadius()

	area1 = circle1.getArea();     // Tell circle1 to run getArea()
	System.out.println("Area of circle1 = " + area1);
    }
  

DEMO: demo/03-classes/03-invoke-method

Preventing undesirable behavior in objects

The Circle class implementation allows a user to access the object variables directly:

    public static void main()
    {
        Circle circle1 = new Circle();  // Create a Circle object circle1

	double area1 = circle1.getArea(); // Tell circle1 to run getArea()
	System.out.println("Area of circle1 = " + area1);

	circle1.radius = 10;   // Update the radius variable directly

	area1 = circle1.getArea();     // Tell circle1 to run getArea()
	System.out.println("Area of circle1 = " + area1);
    } 

Because:

public class Circle 
{
    public double radius = 1;  // The radius variable is not private  
    .... 

 

Preventing undesirable behavior in objects

We can prevent direct access to variables in a class by using the private qualifier:

    public static void main()
    {
        Circle circle1 = new Circle();  // Create a Circle object circle1

	double area1 = circle1.getArea(); // Tell circle1 to run getArea()
	System.out.println("Area of circle1 = " + area1);

	circle1.radius = 10;   // Compile error !!! 

	area1 = circle1.getArea();     // Tell circle1 to run getArea()
	System.out.println("Area of circle1 = " + area1);
    } 

Because:

public class Circle 
{
    private double radius = 1;  // Disallows direct access to radius  
    .... 

DEMO: demo/03-classes/04-private

What happens inside the computer when you create an object

The following diagrams show what happens inside the computer system:

     Circle circle1;           // circle1 is a reference variable !
     circle1 = new Circle(4);

"Circle circle1" will allocate (reserve memory) a reference variable circle1:

 

What happens inside the computer when you create an object

The following diagrams show what happens inside the computer system:

     Circle circle1;           // circle1 is a reference variable !
     circle1 = new Circle(4);  

"new Circle(4)" will allocate (reserve memory) for an radius variable and return its base address:

The radius variable will also be initialize with the value 4.

What happens inside the computer when you create an object

The following diagrams show what happens inside the computer system:

     Circle circle1;           // circle1 is a reference variable !
     circle1 = new Circle(4);  

"circle1 = " will assign the return value to the variable circle1:

 

What happens inside the computer when you create an object

Summary of the effect of:

     Circle circle1;           // circle1 is a reference variable !
     circle1 = new Circle(4); 

The reference variable circle1 will reference to a newly created Circle object:

 

Apply what you ahve learned...

Previously, we used this expression to access the radius variable in Circle1:

   circle1.radius = 10;
 

In this diagram, you can clearly see that Java uses the reference variable Circle to access radius: