ADVERTISEMENTS

Object and Class

In Java, a class is a blueprint or a template for creating objects. It defines the properties and behaviors that objects of that class will have. A class can contain fields (variables) and methods (functions), which define the state and behavior of the objects that are created from the class. To create a class in Java, we use the "class" keyword, followed by the name of the class. For example:

public class Car {
    // Fields
    String make;
    String model;
    int year;
    String color;
    // Constructor
    public Car(String make, String model, int year, String color) {
        this.make = make;
        this.model = model;
        this.year = year;
        this.color = color;
    }
    // Methods
    public void start() {
        System.out.println("The car has started.");
    }
    public void stop() {
        System.out.println("The car has stopped.");
    }
    public void accelerate() {
        System.out.println("The car is accelerating.");
    }
    public void brake() {
        System.out.println("The car is braking.");
    }
}

 

In this example, we've defined a class called "Car" that has fields for make, model, year, and color, as well as methods for starting, stopping, accelerating, and braking. We've also included a constructor method that sets the initial values of the fields when an object of the Car class is created. Once we've defined a class in Java, we can create objects of that class by using the "new" keyword and calling the constructor method. For example:

Car myCar = new Car("Honda", "Civic", 2020, "Blue");

Here, we've created an object of the Car class called "myCar", with the make, model, year, and color properties set to "Honda", "Civic", 2020, and "Blue", respectively.

ADVERTISEMENTS

An object is an instance of a class. When we create an object, we are creating a specific, concrete instance of a class with its own set of properties (also called attributes or fields) and behaviors (also called methods). To create an object in Java, we first need to define a class. We use the "new" keyword to create a new object of that class, and we can assign the object to a variable so that we can access and modify its properties and behaviors. For example, let's say we have a class called "Person" that has fields for name and age, as well as methods for setting and getting those fields:

public class Person {
    String name;
    int age;
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    public int getAge() {
        return age;
    }
}

 

To create an object of the Person class, we can use the following code :

Person person1 = new Person();

Here, we've created a new object of the Person class called "person1". We can now set and get the name and age properties of the object using the methods we defined in the class:

person1.setName("John");
person1.setAge(30);
System.out.println(person1.getName()); // Output: John
System.out.println(person1.getAge()); // Output: 30

In this example, we've set the name and age properties of the object to "John" and 30, respectively, and then used the getName() and getAge() methods to retrieve those values and print them to the console.

Overall, objects are a core concept in Java and object-oriented programming, and they allow us to create and manipulate specific instances of classes with their own unique set of properties and behaviors.

ADVERTISEMENTS