Classes and Objects Definition

In Java, classes and objects are fundamental concepts of object-oriented programming (OOP). A class is a blueprint for creating objects, which are instances of that class. Objects, in turn, encapsulate data and behaviour associated with a specific entity.

Class Definition in Java

In Java, a class is defined using the class keyword, followed by the class name. The body of the class contains data members (fields) and methods.

Syntax for class definition

public class ClassName {
    // Fields (attributes or properties)
    DataType fieldName;
    // Constructor(s)
    public ClassName(parameters) {
        // Constructor logic
    }
    // Methods
    public ReturnType methodName(parameters) {
        // Method logic
    }
}Code language: Java (java)

Syntax for object definition

In Java, objects are instances of classes, and you create objects using the new keyword followed by a constructor. Here is the syntax for creating objects in Java:

ClassName objectName = new ClassName();Code language: Java (java)

Here, ClassName is the name of the class from which you want to create an object, and objectName is the name you give to the object. The new keyword is used to allocate memory for the object, and the constructor is invoked to initialize the object.

If the class has a parameterized constructor, you can pass arguments to it during object creation:

ClassName objectName = new ClassName(argument1, argument2, ...);Code language: Java (java)

Here’s a brief explanation of each part:

ClassName: The name of the class from which you want to create an object.

objectName: The name you give to the object.

new: A keyword used to allocate memory for the object.

ClassName(): The constructor of the class, which is called to initialize the object.

Here’s an example that demonstrates how to define java class and to create object.

class Sparrow {
    // Fields
    String species;
    int age;
    boolean canFly;
    // Constructor
    public Sparrow(String species, int age, boolean canFly) {
        this.species = species;
        this.age = age;
        this.canFly = canFly;
    }
    // Method to get information about the Sparrow
    public void displayInfo() {
        System.out.println("Species: " + species);
        System.out.println("Age: " + age + " years");
        System.out.println("Can Fly: " + (canFly ? "Yes" : "No"));
    }
    // Method to make the Sparrow fly
    public void fly() {
        if (canFly) {
            System.out.println("The sparrow is flying.");
        } else {
            System.out.println("This sparrow cannot fly.");
        }
    }
}
class ClassObjectDemo{
    public static void main(String[] args) {
        // Creating an object of Sparrow class
        Sparrow mySparrow = new Sparrow("Java Sparrow", 3, true);
        // Using object methods
        mySparrow.displayInfo();
        mySparrow.fly();
    }
}

Output:

D:>java ClassObjectDemo
Species: Java Sparrow
Age: 3 years
Can Fly: Yes
The sparrow is flying.
Scroll to Top