Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Getting There Object Inheritance

What's the point of overriding in this case?

I know there's a question already like this under this video, but I still don't understand. Why wouldn't we just create a new method that does what we want it to do instead of overriding an existing method? Does it really change how the code acts?

3 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi Stephanie!

In this specific case, lets assume we wrote our own method named differentStringVersion we could definitely call this, but remember there is a toString method on every object so both methods would be present. How would users of your class know which one to choose?

toString overriding is common and people expect to be able to use that.

Did that help?

Ok, so the reason we're overriding the toString method is so that there is no confusion over which method to use? In that case, when I want to create a method that is similar to (but a little different from) another premade method, should I always override it?

I have stole this answer from stackoverflow (http://stackoverflow.com/questions/14228520/why-is-method-overloading-and-overriding-needed-in-java) , but I think it answers your question nicely:

Overriding is a feature that is available while using Inheritance.

It is used when a class that extends from another class wants to use most of the feature of the parent class and wants to implement specific functionality in certain cases.

In such cases we can create methods with the same name and signature as in the parent class. This way the new method masks the parent method and would get invoked by default.

class Thought { public void message() { System.out.println("I feel like I am diagonally parked in a parallel universe."); } }

public class Advice extends Thought { @Override // @Override annotation in Java 5 is optional but helpful. public void message() { System.out.println("Warning: Dates in calendar are closer than they appear."); } }

Also read http://beginnersbook.com/2014/01/method-overriding-in-java-with-example/ for more details

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi Stephanie!

In this specific case, lets assume we wrote our own method named differentStringVersion we could definitely call this, but remember there is a toString method on every object so both methods would be present. How would users of your class know which one to choose?

toString overriding is common and people expect to be able to use that.

Did that help?