How to create a class and object in Java?

Creating a Class and Object in Java

Introduction

In Java, a class is a blueprint or a template that defines the structure and behavior of an object. An object is an instance of a class, which means it has its own set of attributes (data) and methods (functions) that can be used to manipulate and interact with the object. In this article, we will explore how to create a class and object in Java.

Defining a Class

A class in Java is defined using the public class keyword. The class name should be a valid Java identifier, and it should start with a capital letter. Here is an example of a simple class:

public class Person {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}

In this example, we define a Person class with two private attributes: name and age. We also define a constructor method Person(String name, int age) that initializes the attributes when an object of the class is created. The displayDetails() method is a method that can be used to display the details of the object.

Creating an Object

An object is created by instantiating a class. To create an object, we use the new keyword followed by the class name. Here is an example:

public class Main {
public static void main(String[] args) {
Person person = new Person("John Doe", 30);
person.displayDetails();
}
}

In this example, we create a Person object named person by instantiating the class using the new keyword. We then call the displayDetails() method on the object to display its details.

Creating an Object with Parameters

We can also create an object with parameters by passing them to the constructor method. Here is an example:

public class Main {
public static void main(String[] args) {
Person person = new Person("Jane Doe", 25, "Software Engineer");
person.displayDetails();
}
}

In this example, we create a Person object named person by passing three parameters to the constructor method: name, age, and occupation. We then call the displayDetails() method on the object to display its details.

Access Modifiers

Java classes can have access modifiers that control who can access the class and its members. Here are the three access modifiers available in Java:

  • public: All classes can access the class and its members.
  • private: Only the class itself can access the class and its members.
  • protected: Only the class itself and its subclasses can access the class and its members.

Here is an example of a class with different access modifiers:

public class Main {
public class PrivateClass {
private String privateAttribute = "Private attribute";
}

public class PublicClass {
public String publicAttribute = "Public attribute";
}

public class ProtectedClass {
protected String protectedAttribute = "Protected attribute";
}
}

Inheritance

Inheritance is the process of creating a new class based on an existing class. Here is an example of a class that inherits from another class:

public class Animal {
private String name;

public Animal(String name) {
this.name = name;
}

public void sound() {
System.out.println("The animal makes a sound");
}
}

public class Dog extends Animal {
public Dog(String name) {
super(name);
}

public void sound() {
System.out.println("The dog barks");
}
}

In this example, we define an Animal class with a constructor and a sound() method. We then define a Dog class that extends the Animal class and adds its own sound() method.

Polymorphism

Polymorphism is the ability of an object to take on multiple forms. Here is an example of polymorphism:

public class Animal {
public void sound() {
System.out.println("The animal makes a sound");
}
}

public class Dog extends Animal {
@Override
public void sound() {
System.out.println("The dog barks");
}
}

public class Cat extends Animal {
@Override
public void sound() {
System.out.println("The cat meows");
}
}

public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.sound();

animal = new Cat();
animal.sound();
}
}

In this example, we define an Animal class with a sound() method. We then define two subclasses Dog and Cat that extend the Animal class and override the sound() method. We create an object of type Animal and assign it to an object of type Dog and Cat. We then call the sound() method on the object to display its sound.

Conclusion

In this article, we have explored how to create a class and object in Java. We have defined a simple class with attributes and methods, created an object using the new keyword, and demonstrated polymorphism and inheritance. We have also discussed access modifiers and how they can be used to control who can access the class and its members.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top