The following code demonstrates an Example of Classes and Objects in Java.

This example demonstrate some of the concepts of object-oriented programming. We have a class named Box. It has three data members – width, height, and depth. Also, the Box class defines a parameterized constructor and a method that returns the volume of the Box. Another public class BoxExercise1 defines the main() method that instantiates the box class and calls its methods.

class Box
{
    double width, height, depth;
    public Box(double width, double height, double depth)
    {
        this.width=width;
        this.height=height;
        this.depth=depth;
    }
    public double volume()
    {
        return width*height*depth;
    }
    public void show()
    {
        System.out.println("Width: "+width);
        System.out.println("Height: "+height);
        System.out.println("Depth: "+depth);
    }
}
public class BoxExercise1
{
	public static void main(String[] args) {
	    System.out.println("Creating Box Object...");
	    Box ob=new Box(45, 12.9, 12.6);
	    ob.show();
	    System.out.println("Box Volume: "+ob.volume());	    
	}
}

Output

Example of Classes and Objects in Java Demonstrating the Box Class
Example of Classes and Objects in Java Demonstrating the Box Class

Further Reading

Java Practice Exercise

programmingempire

Princites