Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 4

overriding equals() method in java

Solved! Go to Solution.

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

Screen Shot 2019-03-10 at 6.35.03 PM.png

3 Replies
Commander Lvl 3
Message 2 of 4

Re: overriding equals() method in java

Hi @kateryna216 ,

 

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


- Mark
Solution
Commander Lvl 2
Message 3 of 4

Re: overriding equals() method in java

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 :)

 

-Gabriele-

 


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

 


 

Highlighted
Commander Lvl 3
Message 4 of 4

Re: overriding equals() method in java

Great explaination @Wabri !


- Mark