w3resource

Creating a Java Dog Class with Parameterized Constructor


Parameterized Constructor:
Write a Java program to create a class called Dog with instance variables name and color. Implement a parameterized constructor that takes name and color as parameters and initializes the instance variables. Print the values of the variables.

Sample Solution:

Java Code:

Dog.java

// Define the Dog class
public class Dog {
    // Private instance variables
    private String name;
    private String color;

    // Parameterized constructor
    public Dog(String name, String color) {
        // Initialize name with the provided parameter
        this.name = name;
        // Initialize color with the provided parameter
        this.color = color;
    }

    // Main method to test the Dog class
    public static void main(String[] args) {
        // Create a new Dog object using the parameterized constructor
        Dog myDog = new Dog("Bailey", "Black");
        // Print the values of the instance variables
        System.out.println("Dog's Name: " + myDog.name);
        System.out.println("Dog's Color: " + myDog.color);
    }
}

Output:

Dog's Name: Bailey
Dog's Color: Black

Explanation:

  • Define the Dog class:
    • The Dog class is defined with the keyword class.
  • Private instance variables:
    • The Dog class has two private instance variables: name (a String) and color (a String).
  • Parameterized constructor:
    • The parameterized constructor Dog(String name, String color) is defined.
    • Inside the constructor, this.name is initialized with the provided name parameter.
    • this.color is initialized with the provided color parameter.
  • Main method:
    • The main method is defined to test the Dog class.
    • A new Dog object (myDog) is created using the parameterized constructor, passing "Buddy" and "Brown" as arguments.
    • The values of the instance variables (name and color) are printed to the console.

Note on Constructors:

In the above exercise, the parameterized constructor for the Dog class works by:

  • Initialization with Parameters: When a new Dog object is created, the parameterized constructor is called with specific values for name and color.
  • Setting Values: The constructor initializes the instance variables name and color with the provided values, ensuring that the object is created with specific attributes.
  • Encapsulation: By keeping the instance variables private and initializing them through the constructor, the class ensures that the variables are set only when a new object is created, promoting controlled initialization and data integrity.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java Constructor Previous: Creating a Java Cat Class with Default Constructor.
Java Constructor Next: Creating a Java Book Class with Multiple Constructors.

.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.