Site icon TestingDocs.com

Java Classes and Objects

Introduction

In this post, we will learn to create Classes and objects in Java and how to build them with a demo Java driver program. Class is like a prototype or blueprint for which we will create objects and work with them in the code. Objects are real entities that are created from the class specifications. An object has a state and behaviors. Object state is represented with instance variables or attributes/fields. Behavior is coded with methods.

Create a Class

Let’s start by building a small class used to represent a single concept such as a Cat. Inside the Cat class but outside of any method, we define the instance variables for the Cat class.  Make a new Java project and create a new class called “Cat”.

 

 

For example, the Cat class defines three things like color, age, and size. Lets us create a class specification for the Cat as shown below:

 

public class Cat {
  private String color;
  private int age;
  private double size;

  /**
   * @param color
   * @param age
   * @param size
   */
  public Cat(String color, int age, double size) {
    super();
    this.color = color;
    this.age = age;
    this.size = size;
  }


  /**
   * @return the color
   */
  public String getColor() {
    return color;
  }

  /**
   * @param color the color to set
   */
  public void setColor(String color) {
    this.color = color;
  }

  /**
   * @return the age
   */
  public int getAge() {
    return age;
  }

  /**
   * @param age the age to set
   */
  public void setAge(int age) {
    this.age = age;
  }

  /**
   * @return the size
   */
  public double getSize() {
    return size;
  }

  /**
   * @param size the size to set
   */
  public void setSize(double size) {
    this.size = size;
  }


  /**
   * @param food
   */
  public void eat(String food) {
    System.out.println("Cat is eating.." + food);
  }

  public void sleep() {
    System.out.println("Cat is sleeping... ");
  }

  /**
   * @param speed
   */
  public void run(int speed) {
    System.out.println("Cat is running @ speed... " + speed + " m/s");
  }

  @Override
  public String toString() {
    return "Cat [color=" + color + ", age=" + age + ", size=" + size + "]";
  }

}

 

Create an Object

Let’s create two cat objects using the class specification Cat. We can instantiate new objects using the new operator as shown below. CatDemo is the driver class with the main method. The main method that builds 2 Cat objects and prints the information to the console.

public class CatDemo {

  public static void main(String[] args) {
    //Creating two cat objects
    Cat c1=new Cat("brown",1,1.5);
    Cat c2=new Cat("orange",2,2.5);
    
    //Print object info to console.
    System.out.println("Cat 1=");
    System.out.println(c1.toString());
    System.out.println("Cat 2=");
    System.out.println(c2.toString());
  }

}

Output:

Cat 1=
Cat [color=brown, age=1, size=1.5]
Cat 2=
Cat [color=orange, age=2, size=2.5]

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Exit mobile version