overriding equals() method in java #21390
-
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 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi @kateryna216 , There simply is not method called **get_name() **for your Puppy class. |
Beta Was this translation helpful? Give feedback.
-
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
Protected method
Public method
Hope to solve your problem :slight_smile: -Gabriele-
|
Beta Was this translation helpful? Give feedback.
-
Great explaination @wabri ! |
Beta Was this translation helpful? Give feedback.
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
Protected method
Public method
Hope to solve your problem :slight_smile:
-Gabriele-