Java is an object-oriented programming (OOP) language that offers a rich set of features for creating reusable and modular code. One of the key aspects of OOP is the ability to define classes, which serve as blueprints for creating objects. In this article, we will delve into the process of creating classes, defining attributes, and implementing methods in Java.
To create a class in Java, you simply need to use the class
keyword followed by the class name. Let's consider an example of a Person
class:
public class Person {
// class body
}
This code creates a Person
class with an empty class body. The public
keyword indicates that the class can be accessed from any other class.
Attributes, also known as instance variables or fields, define the state of an object. They represent the characteristics or properties that an object can have. In Java, attributes are defined within the class body and can have different data types.
For instance, let's define a Person
class with attributes such as name
(String) and age
(int):
public class Person {
String name;
int age;
}
Here, we have defined two attributes, name
and age
, of types String
and int
, respectively. These attributes will hold specific values for each Person
object created from this class.
Methods in Java are used to define the behavior of an object. They enable us to perform operations, modify attributes, and interact with other objects. Methods are defined within the class body and can return values or be void (not returning any value).
Let's expand our Person
class to include a method that displays information about a person's name and age:
public class Person {
String name;
int age;
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
In this example, we have added a method called displayInfo()
. When called, this method will print the person's name and age to the console. The public
access modifier allows the method to be accessed from any other class.
Once a class is defined, we can create objects of that class to access its attributes and invoke its methods. To create an object, we use the new
keyword followed by the class name, and assign it to a variable of the corresponding type.
public class Main {
public static void main(String[] args) {
Person person = new Person(); // Creating a Person object
person.name = "John Doe"; // Accessing attributes
person.age = 25;
person.displayInfo(); // Invoking the displayInfo() method
}
}
Here, we have created a Person
object named person
, assigned values to its attributes name
and age
, and invoked the displayInfo()
method to display the information.
In this article, we explored the process of creating classes, defining attributes, and implementing methods in Java. Classes allow us to create reusable blueprints for objects, attributes define their state, and methods enable us to define their behavior. Understanding these concepts is crucial for writing well-structured and modular code using the principles of object-oriented programming in Java.
noob to master © copyleft