DEV Community

Cover image for Mastering Java: Classes, Methods, Inheritance, and Encapsulation
shahTanya
shahTanya

Posted on

Mastering Java: Classes, Methods, Inheritance, and Encapsulation

Java is a versatile and powerful programming language widely used for building robust applications. In this article, we will delve into some of the fundamental concepts of Java: classes, methods, inheritance, and encapsulation. These concepts form the backbone of object-oriented programming (OOP) in Java and are essential for writing efficient and maintainable code.

Classes in Java
In Java, a class is a blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data into one single unit. Here's a basic example of a class in Java:

public class Animal {
    // Fields
    private String name;
    private int age;

    // Constructor
    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Method
    public void displayInfo() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example:

Animal is a class with two fields: name and age.
The constructor Animal(String name, int age) initializes these fields.
The displayInfo method prints the name and age of the animal.
Methods in Java
Methods are functions defined inside a class that describe the behaviors of the objects created from the class. Methods can take parameters, perform actions, and return values.

Here's how you can add more methods to the Animal class:

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

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

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

    // New method to change the age
    public void setAge(int newAge) {
        age = newAge;
    }

    // New method to retrieve the age
    public int getAge() {
        return age;
    }
}
Enter fullscreen mode Exit fullscreen mode

In this modified class:

setAge method allows changing the age of the animal.
getAge method returns the current age of the animal.
Inheritance in Java
Inheritance is a mechanism wherein a new class inherits properties and behaviors (fields and methods) from an existing class. The class that inherits is called a subclass (or derived class), and the class it inherits from is called a superclass (or base class).

Here's an example of inheritance:

// Superclass
public class Animal {
    private String name;
    private int age;

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

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

// Subclass
public class Dog extends Animal {
    private String breed;

    public Dog(String name, int age, String breed) {
        super(name, age);
        this.breed = breed;
    }

    public void displayBreed() {
        System.out.println("Breed: " + breed);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example:

Animal is the superclass with fields name and age, and a method displayInfo.
Dog is the subclass that extends Animal and adds a new field breed and a method displayBreed.
The super(name, age) call in the Dog constructor calls the constructor of the superclass Animal.
Encapsulation in Java
Encapsulation is the wrapping up of data under a single unit. It is the mechanism that binds together the code and the data it manipulates. One way to achieve encapsulation is by making the fields of a class private and providing public getter and setter methods to modify and view the fields' values.

Here's how we can encapsulate the Animal class:

public class Animal {
    // Private fields
    private String name;
    private int age;

    // Constructor
    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getter method for name
    public String getName() {
        return name;
    }

    // Setter method for name
    public void setName(String name) {
        this.name = name;
    }

    // Getter method for age
    public int getAge() {
        return age;
    }

    // Setter method for age
    public void setAge(int age) {
        this.age = age;
    }

    // Method to display information
    public void displayInfo() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this encapsulated class:

Fields name and age are private.
Public getter and setter methods are provided to access and modify these fields.
This ensures that the fields cannot be directly accessed from outside the class, thus protecting the integrity of the object's data.

Understanding classes, methods, inheritance, and encapsulation is crucial for mastering Java and object-oriented programming. By using these concepts, you can create modular, reusable, and maintainable code. Experiment with these examples, build your own classes and methods, and leverage inheritance and encapsulation to design robust applications. Happy coding!

Top comments (1)

Collapse
 
ashtad123 profile image
ASHTAD IRANI

pretty accurate !!