Tuesday, May 27, 2025

Implementing Dog

 In our last article, I mentioned that there are two kinds of constructors—those taking arguments and those that don’t. The one that doesn’t have any arguments exists by default, unless others are created, in which case it must be created explicitly.

To understand constructors, we need to learn a new keyword in Java: this.

Suppose I have a Dog class.

I want that class to have:

  1. My dog’s name
  2. My dog’s owner’s name
  3. My dog’s age
  4. My dog’s breed

This is because name, age, owner’s name, and breed are fundamental to who a dog is.

Suppose I want to also be able to let my Dog bark—simply by printing “woof!” to the screen

I can do this by creating a class to represent my dog.

public class Dog{

public static void main(String[] args){
}

}

So far, everything looks familiar, right? Now, for the new parts.

We’ve created variables inside methods before, like this:

public static void doSomething(){
        int x = 0;
}

For a reason we’ll address very shortly, that x only belongs to doSomething, not to anything else around it.

We want a way to make the Dog’s age, breed, owner, etc., intrinsic to the Dog, and so visible to all of the Dog class. For this, we need what are called instance variables—those declared not inside a method (like x), but only inside a class.

That looks something like

public class Dog{

        public String name;
        public  String owner;
        public int age;
        public String breed;

         public static void main(String[] args){
         }

}

By placing those variables there, we make them a part of any (every) Dog, not just one or two methods, and not just one or two Dog objects.

Obviously, it does us no good to have variables but not be able to look inside them and know their values, or change them. For this, we need some methods called getters and setters. These, appropriately, get back the values of variables, or set them to be certain values. The names these methods take should correspond to that. For instance, setAge sets the Dog’s age; getOwner() gets the owner of a Dog; and so on.

Fully implemented, the getters and setters look something like this:

public class Dog {

    private String name;

    private String owner;

    private int age;

    private String breed;


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

        this.name = name;

        this.owner = owner;

        this.age = age;

        this.breed = breed;

    }

public Dog(){

}

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getOwner() {

        return owner;

    }

    public void setOwner(String owner) {

        this.owner = owner;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

    public String getBreed() {

        return breed;

    }

    public void setBreed(String breed) {

        this.breed = breed;

    }

}

All these methods make use of the “this” keyword for a rather simple reason: the parameter’s name in the constructor or setter is the same as the property’s name. What we mean by this is, for example, with

    public void setBreed(String breed) {

        this.breed = breed;

    }

This means, “Set the breed field of the current Dog to be the parameter we just passed”, and likewise with the others. We do this since it would get very confusing if the parameter name and the field name were the same, without this modification. As you’re getting used to constructors and setters, I highly recommend doing things by hand and color-coding—perhaps with pens or highlighters—what it is you mean for your code to do before actually writing it. And remember: direction matters in assignment, and flows from right to left!

Now, implementing the last thing on my wishlist is trivial: Into the Dog class, I add

public void sayWoof(){
        System.out.println(“Woof!”);
}

and I’m done! All we need now is a main method (public static void main(String[] args), of course!) and we’re all set

No comments:

Post a Comment

Switch

 Other than if/if-else/if-else if-else and the ternary operator, there is yet another common and important conditional expression in Java th...