TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java

Java Classes and Objects

Introduction

In this post, we will learn to create Classes and objects in Java and how to build them with 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. Objects have state and behavior. 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 and create a new class called “Cat”.

 

Java Class and Object

 

For the example, Cat should define three things like color, age, 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 + "]";
 }

}

 

Classes and Objects in Java

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/in/java/

Related Posts

Download Greenfoot Windows

Java /

Download & Install Greenfoot on Windows

Java /

Java Static Code Analysis

Java /

Java Testing Tools

Java /

Handle Multiple Exceptions in Java

Exceptions_In_Java

Java /

Exceptions in Java Programs

‹ Object Oriented Principles› User-Defined Exceptions in Java

Recent Posts

  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • RAPTOR Editions
  • Flowgorithm Conditional Breakpoint Statement
  • Flowgorithm Read Numbers from File Example
  • Search Text File Flowchart Example
  • Flowgorithm Turtle Graphics Symbols
  • Draw Circle using Flowgorithm Turtle
  • Draw Parallel Lines using Flowgorithm Graphics

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version