aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Overloading and overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method Overloading and overriding" Watch "Method Overloading and overriding" New topic
Author

Method Overloading and overriding

sharmistha mohapatra
Greenhorn

Joined: Dec 12, 2008
Posts: 18
Hi,

I know that overloading is when we have a different argument list with same name,return types methods whereas overriding is when we have same name,return types,arg list but different body implementations.

Kindly correct me if i am wrong.

Regards,
Sharmistha
Kedar Nath
Ranch Hand

Joined: Sep 02, 2008
Posts: 53
Overloading --- same name, different argument type/ different number of arguments. It does not matter about the return type.
Overridding--- same name,same arguments and most importantly same return type or covariant return type.

Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

Overriden methods cannot throw broader exceptions than its superclass method.
Overriden methods cannot have a lower access modifier than its superclass method.


SCJP 5.0 and now fighting with ExtJS
sharmistha mohapatra
Greenhorn

Joined: Dec 12, 2008
Posts: 18
Is it possible to have the overriden method in the same class???
Natalie Ap
Ranch Hand

Joined: Jan 09, 2009
Posts: 49
sharmistha mohapatra wrote:Is it possible to have the overriden method in the same class???


Normally no..It would give you a 'duplicate method exists' compilation error. But in the case below, you could have it!



Here, the anonymous inner class extends Test class and overrrides its method testMethod() and both of these methods (the overriding one - 3 and the overridden one - 1) lie in the same class Test - BUT method 3 is NOT a member function of Test class!!
Please correct me if i am wrong folks!


Fiona
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

Hope this example will clear your doubt
sharmistha mohapatra
Greenhorn

Joined: Dec 12, 2008
Posts: 18

Yeah,that indeed was a good example...I got it now.

Thankyou so much for clearing the confusion.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
N Pats wrote:


Here, the anonymous inner class extends Test class and overrrides its method testMethod() and both of these methods (the overriding one - 3 and the overridden one - 1) lie in the same class Test - BUT method 3 is NOT a member function of Test class!!
Please correct me if i am wrong folks!

That's a nice example, but technically the overriding method is in the anonymous class (which extends Test.)


All code in my posts, unless a source is explicitly mentioned, is my own.
adam simo
Greenhorn

Joined: Sep 01, 2008
Posts: 4
Hi,
The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method. For example, a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-runtime exception unless it's a subclass of FileNotFoundException.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

overriding = polymorphism
overloading != polymorphism


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
sudipto shekhar wrote:overriding = polymorphism
overloading != polymorphism


overriding=runtime / dynamic polymorphism
overloading=compile time/static polymorphism


SCJP 6
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

Correct me if i am wrong but Overloading does not fulfill the contract of overriding hence there is no polymorphism in overloading. What Punit said makes me confuse about polymorphism, hence i remember "there is no polymorphism in overloading".
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

What punit said is right.
sudipto shekhar wrote: i remember "there is no polymorphism in overloading".

It wrong.

Actually polymorphism means."Same external interface with different internal structure".

In overloading,

When calling the method , you dont bother to see which method to call. you just pass the value and rest is done by the compiler based on the argument during the compile time.


This is exactly same in case of overridding.
Based on the object, the right method is calledby the JVM during runtime.



SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

sudipto shekhar wrote: Overloading does not fulfill the contract of overriding hence there is no polymorphism in overloading..


What do you mean by this???
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

sudipto shekhar wrote:Overloading does not fulfill the contract of overriding
It's about what we say to what is happening.I am just talking about the theory right now. I went through the above line in book by K&B "Head First Java".
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

By rules of overriding I mean the:

1. Arguments must be the same, and return types must be compatible.
2. The method can't be less accessible.

It says "There's no polymorphism involved with overloaded methods"
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

sudipto shekhar wrote:
It says "There's no polymorphism involved with overloaded methods"


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

sudipto, punit is right. Overloading is AKA static polymorphism. Where did you read that there is no polymorphism in overloading??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

I ain't saying one is wrong! I read it K&B Head First Java(Sixth Indian Reprint Page 191). Yes overloading is static polymorphism.Dynamic binding is with overridden methods. So what about that written in the book?
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

Thank you Punit, James and Ankit.
chetan dhumane
Ranch Hand

Joined: Jan 07, 2009
Posts: 629

Hello ,

Good question you have asked.
When we talk about overloading it is done into the same class method may have different arglist,access modifier,return type but the name will be the same.

But when we talk about the over-riding a method we are talking about inheritance.The method is having same signature as in the superclass.

You will be covering more if you learn polymorphism.


Thanks.

Chetan


http://www.androcid.com/
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
What book have you guys read that states that overloading involves polymorphism? I am used to talking about polymorphism in the context of overriding only, and I thought that for polymorphism to be involved we needed to have a hierarchy of classes.

It's too bad there couldn't be a definition or definitions for polymorphism, because it seems that many times the term is used too broadly (the same as coupling.)
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

The term polymorphism when used with overloading makes the whole thing very confusing. In other books it is stated that polymorphism comes into play in terms of overloading.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
sudipto shekhar wrote:The term polymorphism when used with overloading makes the whole thing very confusing. In other books it is stated that polymorphism comes into play in terms of overloading.

Sudipto,

I think you can look at overloading as implementing some kind of polymorphism (but I don't know if that's the accepted term of polymorphism for the SCJP exam, which I thought only considered overriding as involving polymorphism.)
Polymorphism means "many forms." In programming, it is usually the ability to use objects of different types in specific code constructs. For example, as arguments when calling methods.

Normally you would think of polymorphism as implemented by inheritance, when overriding, as when you do:
method(X x) {}
method(a);
You can pass the method a parameter a that is either of type X, or of any of its subtypes. Then, if within the method you use operations on x, these operations will be resolved polymorphically at runtime based on the actual type of the object pointed to by the parameter. So if you say:
x.method1()
inside method, and x points to an object of type Y (Y extends X,) then if Y overrides method1 defined in X, that version of method1 will be invoked.
All this means that you can invoke method() using any parameter whose type is a subtype of X. In other words, you can use different types for the argument that you call method() with. Not only that, but because you are using overriding, within the method you can obtain behavior specifically defined within the type of the actual object.

But you can also think of overloading as implementing some kind of polymorphism, because you could have:
method(Y y) {}
method(Z z) {}
Then, you could invoke method() with an argument of type Y or Z, and the correct method will be resolved at compile time. I suppose you can think of this as polymorphism, although I am not used to think of polymorphism that way.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

That's what i am speaking of..Polymorphism in overriding can be well understood...All have many versions of understanding a topic.I have understood that in a way and just because which method will be called is decided at the compile time,will be regarded as static polymorphism.I think to just to consider polymorphism in terms of overriding.Hey Ruben thanks for taking the effort. Have a nice time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Overloading and overriding
 
Similar Threads
Overload
Overriding?
Overloading/Overriding Method Names
interface problem
overriding Method