Introduction:
This Java program demonstrates the use of two classes, Person and Main. The Person class represents a person with attributes such as name and age, along with methods to get, set, and display information. The Main class contains the main method where we create an instance of the Person class, display information, and update the age.
Algorithm:
- Person Class:
- Define a class named Person with private attributes name (String) and age (int).
- Create a constructor that initializes the name and age attributes when a Person object is created.
- Implement getter methods (getName and getAge) to retrieve the values of the attributes.
- Implement setter methods (setName and setAge) to update the values of the attributes.
- Create a method displayInfo to print the name and age of the person.
- Main Class:
- Define a class named Main.
- Inside the main method:
- Create an instance of the Person class named person1 with the name “John Doe” and age 25.
- Display information about person1 using the displayInfo
- Update the age of person1 to 26 using the setAge
- Display the updated information about person1.
Explanation:
The Person class encapsulates the properties of a person and provides methods to interact with those properties. The Main class serves as the entry point for the program, where we create an instance of Person, manipulate its attributes, and observe the changes.
- In the Person class, the constructor initializes the name and age attributes, and getter and setter methods ensure controlled access to these attributes.
- In the Main class, we instantiate a Person object (person1) and display its initial information. We then update the age of the person and display the updated information. This demonstrates the concept of encapsulation, where the internal details of the Person class are hidden, and interactions occur through well-defined methods.
The program provides a foundation for understanding the principles of object-oriented programming, encapsulation, and class interactions in Java.
Code:
// Person.java
public class Person {
private String name;
private int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getter methods
public String getName() {
return name;
}
public int getAge() {
return age;
}
// Setter methods
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
// Display information about the person
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
// Main.java
public class Main {
public static void main(String[] args) {
// Create an instance of the Person class
Person person1 = new Person("John Doe", 25);
// Display information about the person
System.out.println("Person 1 Information:");
person1.displayInfo();
// Update the age of the person
person1.setAge(26);
// Display updated information
System.out.println("\nUpdated Person 1 Information:");
person1.displayInfo();
}
}
In this example, we have a Person class with attributes like name and age, along with getter and setter methods. The displayInfo method is used to print information about the person. The Main class contains the main method, where we create an instance of the Person class, display information, and update the age.
FAQs :
Q: Why use two classes in a Java program?
A: Using two classes in a Java program promotes better code organization and encapsulation. It allows for the separation of concerns, making the code more modular and maintainable. One class can focus on specific functionality (e.g., a data model), while the other can serve as the entry point or driver program.
Q: What is the purpose of the Main class in a Java program with two classes?
A: The Main class in a Java program serves as the entry point for execution. It typically contains the main method where the program starts running. In programs with two classes, the Main class is responsible for creating instances of other classes, coordinating interactions, and demonstrating how the classes work together.
Q: How does encapsulation contribute to the design of Java programs with multiple classes?
A: Encapsulation in Java involves bundling data (attributes) and methods that operate on that data within a class. When using multiple classes, encapsulation ensures that each class encapsulates its own functionality, making it easier to understand, modify, and maintain. It also provides a clear interface for interactions between classes.
Q: Can I have multiple constructors in each class of a Java program with two classes?
A: Yes, it’s possible to have multiple constructors in each class. Constructors with different parameter lists enable flexibility in object instantiation. In the provided Java program, the Person class has a single constructor, but you can add additional constructors with different parameters if needed.
Q: How can I extend this Java program with more functionality using two classes?
A: To extend the program, you can add new methods or attributes to the existing classes or create additional classes with specific functionalities. For example, you could create a new class representing a group of people, add methods for group operations, and demonstrate interactions between the Person and Group classes in the Main class. This demonstrates the extensibility and scalability of object-oriented design.