Hey! I wanted to ask that what is overiding in java? It is confusing me allot. I got the point of overloading but in case of overriding, I am unable to get what it is?
Please help!
SA.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hey! I wanted to ask that what is overiding in java? It is confusing me allot. I got the point of overloading but in case of overriding, I am unable to get what it is?
Please help!
SA.
So you know that with overloading, several methods or constructors can have the same name but take different parameters. Overriding is a method with exactly the same signature as an extended or parent class so that the derived class' method overrides the parent class' method.
The toString() method is a good example that you should have learned about. The Object class provides a toString() method, but a programmer will usually override the toString() method with his/her own version in order to present the class appropriately when printed using the default output.
To add to GragBrannon's succinct explanation, Java Inheritance gives a good rundown on method overriding by starting with inheritance, followed by defining overriding methods in a subclass, an Annotation to use when overriding a method, and finally how fields cannot be overridden in a subclass.
Another good resource is Java - Overriding, which goes into greater depth by covering the rules for method overiding. While you're at it, you might want to also go through the next page in the tutorial that links polymorphism to method overriding.
To round this off, take a look at Overriding and Hiding Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance), which covers a similar concept - method hiding - that is applicable to static methods. (You may want to skip the Interface Methods section though. This is a new feature introduced in Java 8, and it's quite confusing.)
Overriding method: same signature (same return type, list of parameters, name of method), but do slightly different task.
Overloading method: same name of method, but different list of parameters.