Classes and objects-Employee class
Let's break down the concepts of classes, objects, and constructors in Java with an example.
Objects in Java
Example
Classes in Java
A class in Java is a blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data into one single unit. A class can contain fields (variables) and methods to describe the behavior of an object.
An object is an instance of a class. When a class is defined, no memory is allocated until an object of that class is created. Objects are created using the new keyword.
Constructors in Java
A constructor in Java is a special method that is called when an object is instantiated. Its primary purpose is to initialize the newly created object.
A constructor:
- Has the same name as the class.
- Does not have a return type.
- Can be overloaded to accept different numbers or types of parameters.
Let's create a simple Employee class to illustrate these concepts.
class Employee {
// Member variables
private String name;
private int age;
private double salary;
// Constructor
public Employee(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
// Method to display employee details
public void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}
public class MainApp {
public static void main(String[] args) {
// Creating an object of Employee using the constructor
Employee emp1 = new Employee("John Doe", 30, 50000);
emp1.displayDetails();
}
}
Explanation:
Employee Class: This class (Employee.java) contains the Employee class definition, including its member variables (name, age, salary), constructor (Employee(String name, int age, double salary)), and methods (displayDetails() to print employee details).
MainApp Class: This class (MainApp.java) contains the main method, which serves as the entry point for the Java application. It creates an instance of Employee (emp1) using the constructor, sets its initial values, and then calls emp1.displayDetails() to display the employee details.
Code Reusability: The Employee class can be reused in other parts of the application or in other applications.
Encapsulation: Each class encapsulates its own functionality, promoting cleaner and more maintainable code.
Encapsulation is the concept of bundling data (variables) and methods (functions) that operate on the data into a single unit, known as a class. It restricts direct access to some of the object's components, which is a means of preventing unintended interference and misuse of the methods and data. Encapsulation is achieved by using access modifiers such as private, protected, and public.
Abstraction is the concept of hiding the complex implementation details of a system and exposing only the essential features to the user. It focuses on what an object does rather than how it does it. Abstraction allows you to create a simple, user-friendly interface that interacts with more complex underlying code.
Comments
Post a Comment