Class

In Java, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). Here is a simple example of a class in Java:

public class Dog {

  // Instance Variables

    private String name;

    private int age;

    private String breed;

 // Constructor Declaration of Class

    public Dog(String name, int age, String breed) {

        this.name = name;

        this.age = age;

        this.breed = breed;

    }

    // Method 1

    public String getName() {

        return name;

    }

// Method 2

    public int getAge() {

        return age;

    }

  // Method 3

    public String getBreed() {

        return breed;

    }

public void bark() {

        System.out.println(name + " says: Woof!");

    }

}

This is a class named Dog which has three instance variables: name, age, and breed. The constructor is a special type of method that is used to initialize objects of the class. There are several methods to get the data of the instance variables and another method bark to show an action by the object of this class.


Now, you can use this class to create objects and call their methods:

public class Main {

 public static void main(String[] args) {

        // Creating object

        Dog myDog = new Dog("Rover", 5, "German Shepherd");

  // Calling method

        myDog.bark();

  // Accessing instance variable through getter

        System.out.println("The dog's name is " + myDog.getName());

        System.out.println("The dog's age is " + myDog.getAge());

        System.out.println("The dog's breed is " + myDog.getBreed());

    }

}

When you run this Main class, it will create a new Dog object named myDog with the name "Rover", age 5, and breed "German Shepherd". Then it calls the bark method of myDog, and prints out the dog's name, age, and breed.


Let's build upon our Dog class and introduce some more advanced features of Java classes.

public class Dog {

    private String name;

    private int age;

    private String breed;

  // Constructor

    public Dog(String name, int age, String breed) {

        this.name = name;

        this.age = age;

        this.breed = breed;

    }

// Getter Methods

public String getName() {

        return name;

    }

public int getAge() {

        return age;

    }

public String getBreed() {

        return breed;

    }

// Setter Methods

 public void setName(String name) {

        this.name = name;

    }

public void setAge(int age) {

        if (age >= 0) { // Basic validation

            this.age = age;

        } else {

            System.out.println("Age can't be negative");

        }

    }

  public void setBreed(String breed) {

        this.breed = breed;

    }

// Additional methods

    public void bark() {

        System.out.println(name + " says: Woof!");

    }

public void celebrateBirthday() {

        age++;

        System.out.println("Happy birthday, " + name + "! You are now " + age + " years old.");

    }

}


In this new version of the Dog class, we've added setter methods to change the state of the Dog object after it's created. We've also added a celebrateBirthday method, which increases the dog's age by one.

Here's how you might use these new features:

public class Main {

   public static void main(String[] args) {

        // Create a new Dog

        Dog myDog = new Dog("Rover", 5, "German Shepherd");

        // Rover barks

        myDog.bark();

        // It's Rover's birthday

        myDog.celebrateBirthday();

        // Oh no, we got Rover's breed wrong. Let's correct it.

        myDog.setBreed("Golden Retriever");

        // Now let's print out Rover's details again

        System.out.println("The dog's name is " + myDog.getName());

        System.out.println("The dog's age is " + myDog.getAge());

        System.out.println("The dog's breed is " + myDog.getBreed());

    }

}

When you run this code, it will:

Create a new Dog named Rover who is a 5 year old German Shepherd.

Make Rover bark.

Celebrate Rover's birthday, increasing his age to 6.

Correct Rover's breed to Golden Retriever.

Print out Rover's details, showing the updated age and breed.

Comments

Popular posts from this blog

Wrapper

Conditional Statement

DataType and Variable