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

Aditya Puri
Aditya Puri
1,080 Points

What does overriding mean?

Some new concepts are not explained here at treehouse and just shoved in in a video.

Like what does overriding mean? Noone explained it and its in the video!! I am not being rude or something and you guys do a great job but this happens a lot :/

Please help.

Aditya Puri
Aditya Puri
1,080 Points

it IS mentioned above

3 Answers

Matthew Francis
Matthew Francis
6,967 Points

Override is literaly what it means. It overrides whatever the previous value is.

In toString, it overrides the class name.

class Main {
  public static void main(String[] args) {
    System.out.println("hello world");
    Jon a = new Jon();
    System.out.println(a); //Try removing toString(), it would print the object name. With toString() it's going to always return "Matt"

  }
}

class Jon{
    @Override//Not neccecary, but Override reminds you of typos. Try misspelling toString. Then try to include @Override and mispell toString()

    public String toString(){
        return "Matt";
    }
}
Aditya Puri
Aditya Puri
1,080 Points

so when we use that treet object in the System.out.printf line, it automatically calls the toString method of the object class?

Matthew Francis
Matthew Francis
6,967 Points

Sorry, didn't read your previous message.

But yup

If you do system.out.println(Jon), it would not print Jon@12348 (default name java gave to the object, also known as a haschode), but instead it would print out "Matt".

andren
andren
28,558 Points

Overriding is when you create your own implementation of a method that the class inherited from another class.

As was discussed in the video all classes in Java inherits from the object class, which is why all classes has a toString method, however the default toString method is not very useful (it just prints then name and hash code of the object), so most classes define their own toString method that actually produces a string that describes the class more properly.

When they do so they are overriding the method, to put it as simply as possible overriding is just creating a method that has the exact same name, and takes the exact same parameters as a method that you have inherited from another class.

The @override annotation is not actually mandatory, but is just there to tell Java that you are intentionally creating a method that will override an inherited method.

Aditya Puri
Aditya Puri
1,080 Points

so when we use that treet object in the System.out.printf line, it automatically calls the toString method of the object class?

Victor Georgescu
Victor Georgescu
2,991 Points

Yes, exactly. I was thinking of an example while I was reading all the comments in here. Think of buying a new car with no options on it. Then you decide to do some customization to that car to fit your taste. When he changed the toString method to return Matt instead of Jon@1234 is similar with the customization part in the example given above.

I am sorry if my English is not that good but it's my first answer on treehouse community and I am doing my best to help. :)