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 trialAditya Puri
1,080 PointsWhat 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.
3 Answers
Matthew Francis
6,967 PointsOverride 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
1,080 Pointsso when we use that treet object in the System.out.printf line, it automatically calls the toString method of the object class?
Matthew Francis
6,967 PointsSorry, 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".
Aditya Puri
1,080 Pointsoh, ty!!
andren
28,558 PointsOverriding 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
1,080 Pointsso when we use that treet object in the System.out.printf line, it automatically calls the toString method of the object class?
Victor Georgescu
2,991 PointsYes, 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. :)
Aditya Puri
1,080 Pointsthanks!
Aditya Puri
1,080 PointsAditya Puri
1,080 Pointsit IS mentioned above