what is class and object and different ways to create object in java with example
We’re going to cover three interview questions in this session.
1. what is class in java
2. What is object in java
3. what are the different ways to create object in java
what is class in java
In Java, a class is a blueprint or a template for creating objects. It defines a data structure along with methods that operate on the data. Objects are instances of classes, and they encapsulate data and behavior. Each object created from a class can have its own unique data, but it follows the structure and behavior defined by the class.
Let’s consider a real-life example to illustrate the concept of a class in Java:
Example: 'Car'
Class
// Defining a Car class
public class Car {
// Fields (attributes)
String brand;
String model;
int year;
double price;
// Constructor
public Car(String brand, String model, int year, double price) {
this.brand = brand;
this.model = model;
this.year = year;
this.price = price;
}
// Method to display information about the car
public void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
System.out.println("Price: $" + price);
}
// Method to start the car
public void start() {
System.out.println("The " + brand + " " + model + " is starting.");
}
// Method to accelerate the car
public void accelerate() {
System.out.println("The " + brand + " " + model + " is accelerating.");
}
// Method to stop the car
public void stop() {
System.out.println("The " + brand + " " + model + " has stopped.");
}
}
Now, you can create instances of the Car
class and use its methods to perform actions on individual cars:
public class Main {
public static void main(String[] args) {
// Creating instances of the Car class
Car car1 = new Car("Toyota", "Camry", 2022, 25000.50);
Car car2 = new Car("Honda", "Civic", 2021, 22000.75);
// Using methods to interact with the cars
car1.displayInfo();
car1.start();
car1.accelerate();
car1.stop();
System.out.println(); // Separating output for clarity
car2.displayInfo();
car2.start();
car2.accelerate();
car2.stop();
}
}
In this example, the Car
class defines the attributes of a car (brand, model, year, price) and methods that represent actions a car can take (display information, start, accelerate, stop). The main
method in the Main
class demonstrates creating instances of the Car
class and using its methods to perform actions on specific cars.
What is object in java
In Java, an object is an instance of a class. A class serves as a blueprint or template, defining the structure and behavior of objects. When you create an object, you are creating a concrete instance of that class, with its own unique set of data (attributes) and behavior (methods).
Let’s extend the previous example with the Car
class and demonstrate how to create objects:
// Car class (as defined in the previous example)
public class Car {
// Fields (attributes)
String brand;
String model;
int year;
double price;
// Constructor
public Car(String brand, String model, int year, double price) {
this.brand = brand;
this.model = model;
this.year = year;
this.price = price;
}
// Methods (as defined in the previous example)
public void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
System.out.println("Price: $" + price);
}
public void start() {
System.out.println("The " + brand + " " + model + " is starting.");
}
public void accelerate() {
System.out.println("The " + brand + " " + model + " is accelerating.");
}
public void stop() {
System.out.println("The " + brand + " " + model + " has stopped.");
}
}
Now, let’s create objects (instances) of the Car
class in a Main
class:
public class Main {
public static void main(String[] args) {
// Creating instances (objects) of the Car class
Car car1 = new Car("Toyota", "Camry", 2022, 25000.50);
Car car2 = new Car("Honda", "Civic", 2021, 22000.75);
// Using methods to interact with the objects
car1.displayInfo();
car1.start();
car1.accelerate();
car1.stop();
System.out.println(); // Separating output for clarity
car2.displayInfo();
car2.start();
car2.accelerate();
car2.stop();
}
}
In this example, car1
and car2
are objects of the Car
class. They are created using the new
keyword followed by the constructor of the class (new Car(...)
). Each object has its own set of attributes (brand, model, year, price) and can perform actions using the methods defined in the class.
Creating and using objects is fundamental to object-oriented programming, as it allows you to model and interact with real-world entities in your code.
What are different ways to create object in java
In Java, there are several ways to create objects, and the choice of method depends on the specific requirements of your program. Here are the main ways to create objects in Java:
1. Using the new
Keyword: The most common way to create an object is by using the new
keyword followed by the class constructor.
ClassName objectName = new ClassName();
// Example
Car car = new Car("Toyota", "Camry", 2022, 25000.50);
2. Using a Static Factory Method: Some classes provide static factory methods that create and return instances of the class. These methods are named and may include specific logic for object creation.
ClassName objectName = ClassName.createInstance(); // Example method name: createInstance()
// Example
// Assuming a static factory method in Car class
Car car = Car.createCar("Toyota", "Camry", 2022, 25000.50);
3. Using Object Cloning: The clone()
method is used to create a copy of an existing object. The class of the object being cloned must implement the Cloneable
interface.
Note: Object cloning is considered somewhat complex and is not commonly used.
ClassName clonedObject = (ClassName) existingObject.clone();
// Example
// Assuming the Car class implements Cloneable
Car clonedCar = (Car) car.clone();
4. Using Deserialization: Objects can be created by deserializing them from a stream. This involves converting an object from its byte-stream representation back into an object.
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("file.ser"));
ClassName objectName = (ClassName) ois.readObject();
// Example
// Assuming Car class is Serializable
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("car.ser"));
Car car = (Car) ois.readObject();
Note: Deserialization involves reading objects from a file or network, and it requires the class to implement the Serializable
interface.
These are the common ways to create objects in Java. The most frequently used method is using the new
keyword and the class constructor. The other methods are used in specific scenarios or for specific design patterns.