Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Method overriding
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Java=== In [[Java (programming language)|Java]], when a subclass contains a method with the same signature (name and parameter types) as a method in its superclass, then the subclass's method overrides that of the superclass. For example: <syntaxhighlight lang="java"> class Thought { public void message() { System.out.println("I feel like I am diagonally parked in a parallel universe."); } } public class Advice extends Thought { @Override // @Override annotation in Java 5 is optional but helpful. public void message() { System.out.println("Warning: Dates in calendar are closer than they appear."); } } </syntaxhighlight> Class {{Java|Thought}} represents the superclass and implements a method call {{Java|message()}}. The subclass called {{Java|Advice}} inherits every method that could be in the {{Java|Thought}} class. Class {{Java|Advice}} overrides the method {{Java|message()}}, replacing its functionality from {{Java|Thought}}. <syntaxhighlight lang="java"> Thought parking = new Thought(); parking.message(); // Prints "I feel like I am diagonally parked in a parallel universe." Thought dates = new Advice(); // Polymorphism dates.message(); // Prints "Warning: Dates in calendar are closer than they appear." </syntaxhighlight> When a subclass contains a method that overrides a method of the superclass, then that (superclass's) overridden method can be explicitly invoked from within a subclass's method by using the [[Keyword (computer programming)|keyword]] {{Java|super}}.<ref name="lewis-loftus" /> (It cannot be explicitly invoked from any method belongings to a class that is unrelated to the superclass.) The {{Java|super}} reference can be <syntaxhighlight lang="Java"> public class Advice extends Thought { @Override public void message() { System.out.println("Warning: Dates in calendar are closer than they appear."); super.message(); // Invoke parent's version of method. } </syntaxhighlight> There are methods that a subclass cannot override. For example, in Java, a method that is declared final in the super class cannot be overridden. Methods that are declared private or static cannot be overridden either because they are implicitly final. It is also impossible for a class that is declared final to become a super class.<ref name="deitel">Deitel & Deitel 2001, p.474</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)