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 Getting There Object Inheritance

Miguel Gonzalez Rocha
Miguel Gonzalez Rocha
2,849 Points

Why use override?

As i understand we use override to use the method toString that we made, instead of the one that is already inherited from the object class, but why is it necessary if we could just use another name

4 Answers

George Pirchalaishvili
George Pirchalaishvili
3,747 Points

Think about it this way.

There is Object class. It is the main class, parent class or what ever. All other classes are objects, like all cats are animals.

Than you have a String class, which is an object, but has some extra methods, which make it unique, like "Myaw" sounds are unique for cats.

Everytime a cat wants to say something (you want to call your toString method), it says "Myaw" (something happens with object).

But lets say you want a cat to say "Whoof", than you need to override her method which is responsible for talking(your toString).

Is it easier this way or I confused you even more? :D

Miguel Gonzalez Rocha
Miguel Gonzalez Rocha
2,849 Points

ok i got that, but then after you override one then how do you access the original one? you have to choose between using "Myaw" and "Whoof"? why not just make a method with different name and call that one?

George Pirchalaishvili
George Pirchalaishvili
3,747 Points

Once you override a method, you cannot use original one.

Once again you can create new method tellWhoof (for example) and use it manually to make cat tell "Whoof".

But if your cat is interacted without you (not manually, but automatically by Android), it will use default method which will make it tell "Myaw". In most cases you want to replace that default

Miguel Gonzalez Rocha
Miguel Gonzalez Rocha
2,849 Points

ok thanks thats what i didnt undestand, we are replacing a method that is used automatically (inherited methods) thanks for the patience

George Pirchalaishvili
George Pirchalaishvili
3,747 Points

no problems :) Hope it helps you in the future ;)

Gavin Ralston
Gavin Ralston
28,770 Points

One thing to keep in mind with things like toString is that every object is expected to have that method.

The default toString prints the object name and a memory address, which isn't nearly as useful as a short description of what the object is (In this example it's just a brief listing of the description and the author name)

Another example that will probably come up (maybe sooner, maybe later) are built-in equality methods that every object gets.

By default, Java probably won't understand how you would want to compare cats to determine equality. Maybe you've got some need to compare cats, and for your purposes, all you need to know is that they're the same color and they weigh the same amount, but you don't care if they meow, or rawr, or if they're persian or an alley cat.

So you'd want to override the method you get automatically for checking equality, in this case, equals()

So you'd override equals, and explain in detail how to compare cats, and if "weight" and "color" were equal, you could return true, and anybody that wanted to compare cats could now do so without writing a ton of code.

George Pirchalaishvili
George Pirchalaishvili
3,747 Points

Thats because, most of classes which have some methods based on what happens with them.

Lets say onCreate method. It is used when object is created, so even if you make newOnCreate it wont be used automatically. That is why you use override on onCreate. Same with most other things - they are called automatically when something is done.

Miguel Gonzalez Rocha
Miguel Gonzalez Rocha
2,849 Points

so this inherited methods dont need to be called? they just are used without us knowing? and in this case we configure the toString method to work in the specific way that we need it to work? But then how do we use the original one if its already overrided? i think im getting more confused :S