Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Professional Android 2 Application Development
This is the forum to discuss the Wrox book Professional Android 2 Application Development, 2nd Edition by Reto Meier; ISBN: 978-0-470-56552-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android 2 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 8th, 2010, 03:37 AM
Registered User
 
Join Date: Jul 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default What is @Override for?

Hi, I've google for a good explanation for the use/purpose of @Override but yet to find a good explanation.

What is it for? When/why use it?
  #2 (permalink)  
Old July 8th, 2010, 04:36 AM
Registered User
 
Join Date: Jul 2010
Location: Italy
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
Talking ... Override ...

Override means rewrite behavior (code) you inherited, so that son can do something different from father while invoking same named behavior.
For example, you can have an Employe class that can "calculateSalary", and a specialized Boss class that inherits from it but makes an override of "calculateSalary" so that it could give an higher result...So now you have two stamps for your objects that have a really similar behavior, but can give you a different result. And probably you did not rewrite the whole routine in the son (the Boss), but invoked from it its father method and applied a "small" correction to the result before returning it to the caller. Thus you wrote few code in the son, and you authomatically add to all bosses any surplus you will give to the whole employes (plus correction)....
The Following User Says Thank You to Cimperiali For This Useful Post:
Whoelse (July 8th, 2010)
  #3 (permalink)  
Old July 8th, 2010, 09:47 AM
Authorized User
Points: 123, Level: 2
Points: 123, Level: 2 Points: 123, Level: 2 Points: 123, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2010
Posts: 18
Thanks: 0
Thanked 6 Times in 3 Posts
Default

@Override is used to perform compile-time checks to ensure that the annotated method overrides a method from the superclass. Those checks prevent you from making mistakes like introducing a new method (e.g., you had a typo or you messed up the case of a method name) or overloading a method (e.g., you used the wrong signature) when you intended to override a method. Without the annotations, the compiler has no way to identify those errors, and when you run the program the inherited version of the method executes when you're expecting your version of the method to execute.

Consider, for example:

Code:
public class Example {
    public String tostring() {
        return "Example";
    }
}
In this case, the author intended to override toString() from Object; however, he typed 'tostring' (lower case 's') instead of 'toString'. This is not a syntax error -- 'tostring' is a valid method name. If, however, we call 'toString()' on an instance of this class, we might get something like:

Example@123456

when we expected:

Example

If, however, we used the @Override annotation:

Code:
@Override
public String tostring() {
    return "Example";
}
We'll get the compile time error:
Quote:
Example.java:2: method does not override or implement a method from a supertype
The programmer can then correct the signature, and the code will compile and execute properly.
  #4 (permalink)  
Old July 8th, 2010, 10:21 PM
Registered User
 
Join Date: Jul 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

DrGaribaldi, that is a very clear explanation.
Thank.
Cimperiali too for your help.
  #5 (permalink)  
Old July 9th, 2010, 01:51 AM
Registered User
 
Join Date: Jul 2010
Location: Italy
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by DrGaribaldi View Post
@Override is used to perform compile-time checks to ensure that the annotated method overrides a method from the superclass.
Nice, I did not know (thought "@" was an extra to Override). That is specific to java, correct?


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why would you override ToString()? chobo2 C# 2008 aka C# 3.0 16 November 11th, 2008 11:01 PM
Configuration Override Files Jeff Mason Visual Studio 2005 1 March 14th, 2007 08:52 AM
how to override equals() and hashcode() praveena Java Basics 3 March 14th, 2006 02:30 AM
To Override or Not to Override frresh BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 1 May 2nd, 2005 01:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.