overriding equals() method in java

public boolean equals(Object otherObject) { super.equals(otherObject); boolean isEqual = false; if(otherObject != null && otherObject instanceof Puppy) { Puppy otherPuppy = (Puppy) otherObject; if((this.dep.equals(otherPuppy.dep)) && (this.getName().equals(otherPuppy.getName()))) { isEqual = true; } } return isEqual; }

Puppy class extends Dog class. Dog class extends Animal class. In Animal class method getName() was declared. I have error (The method getName() is undefined for the type Puppy). Why

Hi @kateryna216 ,

There simply  is not method called **get_name() **for your  Puppy class. 

1 Like

Hi @kateryna216,

Welcome to the Github Community Forum!

The getName() method is declared in the parent Animal class, so with this in mind what access modifiers did you assign to getName()? If is a private method you can use that only in a Animal object. To use only in a ereditary class you can assign a protected modifiers. To use everywhere assign the public modifier.

Private method

private String getName() {
            return "something";
}

Protected method

protected String getName() {
            return "something";
}

Public method

public String getName() {
            return "something";
}

Hope to solve your problem :slight_smile:

-Gabriele-

Mark helpful posts with Accept as Solution to help other users locate important info. Don’t forget to give Kudos for great content!

 

2 Likes

Great explaination @wabri !

1 Like