This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overriding" Watch "overriding" New topic


suganya raj

Joined: Aug 08, 2007
Posts: 10

What is overriding? How it differ from overloading?


Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Overriding is the concept where a subclass will "re-implement" an inherited method to change the way in which that method works. The overriding method would have the same contract as the original method.

When you over load a method, you are provide alternative versions of the same method, but their contact most differ from each other. This can be seen in the wrapper classes for the primitive types. Can be seen in constructors where a number of constructors exist for a class.

camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
overriding - inheritance is mentioned to it when a subclass whom to be more it specifies with the method of the superclassroom.
overloading - you mention yourself to have metodos with name the same, more with different arguments.

so long!

Brazil - Sun Certified Java Programmer - SCJP 5 About Java - Update every Week.
Guide SCJP - tips that you need know
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
In simple-speak....

1) overriding - you are completely replacing the inherited method within the subclass. When this method name is called using a reference to the new subclass object you get the subclass' version of the method. It MUST have the same parameter list and must return the same type EXCEPT that it MAY return a subclass of the overridden class' return type ( see covariant return)

2) overloading - you are adding an implementation of the superclass' method that deals with a different parameter type, a different parameter list, or a combination of those. Since it is a new method it can change the return type.

(edited to fix typo)
[ August 08, 2007: Message edited by: Bob Ruth ]

SCJP - 86% - June 11, 2009
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

So are these questions from a test, or from an interview?

[Jess in Action][AskingGoodQuestions]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Looks like a standard interview question in the basic level.

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
I agree. Here's the link:
subject: overriding
Similar Threads
"overload the method"question required
overriding and polymorphism
Typical Overriding and Overloading Examples
How main in superclass is called from subclass