![]() |
|
this, and ovverride - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Java, JVM, & JRE (https://sinister.ly/Forum-Java-JVM-JRE) +--- Thread: this, and ovverride (/Thread-this-and-ovverride) |
this, and ovverride - rexist - 10-09-2013 guys, how do u use >this< and >Override<??? RE: this, and ovverride - Slarek - 10-09-2013 Could you tell the language? I think it's probably Java or C#. RE: this, and ovverride - Psycho_Coder - 10-09-2013 (10-09-2013, 05:06 AM)Slarek Wrote: Could you tell the language? Since he has posted this in the java sub forum so he must be referring to java. @rexist @Override annotation was added in JDK 1.5 and it is used to instruct compiler that method annotated with @Override is an overridden method from super class or an interface. @Override is particularly useful while overriding methods that accept Object as parameter just like equals, compareTo of Comparable interfaces or hashCode() method etc. Take for an example the following :- Code: public class HC{
private String name;
public boolean equals(HC username){
return name.equals(HC.username);
}
}In the above code we are attempting to override equals() method, but instead of overriding, its actually overloaded method. This error escape from compiler and not even detected on runtime by any Exception and it’s very hard to detect. @Override annotation in Java prevents this category of mistake. When we put @Override annotation above equals method than compiler will verify if this method actually overrides a super class or an interface method or not. if its not then it throw compilation error something like "method does not override or implement a method from a super type". @Override saves you a lot of debugging. Also using @Override is a good programming practice. From Java 6 onwards you can use @Override annotation while implementing interface method as well. Here are some examples :- http://javarevisited.blogspot.sg/2011/02/how-to-write-equals-method-in-java.html this http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html http://www.javatpoint.com/this-keyword Read the above and its enough. The second link has examples study those. By the way these questions can be easily googled. RE: this, and ovverride - rexist - 10-11-2013 thanks a lot,,,,I'll figure out the rest. RE: this, and ovverride - rexist - 10-11-2013 thanks a lot,,,,I'll figure out the rest. RE: this, and ovverride - Slarek - 10-11-2013 @Psycho_Coder Well that was rather embarrassing. I couldn't just check the sub forum:Grin: RE: this, and ovverride - Slarek - 10-11-2013 @Psycho_Coder Well that was rather embarrassing. I couldn't just check the sub forum:Grin: |