File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 24199

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

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

Joined: Apr 20, 2006
Posts: 3381

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
It's not a secret anymore!