• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Method Overloading and overriding

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 136
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overriden methods cannot throw broader exceptions than its superclass method.
Overriden methods cannot have a lower access modifier than its superclass method.
 
sharmistha mohapatra
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to have the overriden method in the same class???
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope this example will clear your doubt
 
sharmistha mohapatra
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Thankyou so much for clearing the confusion.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
overriding = polymorphism
overloading != polymorphism
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sudipto shekhar wrote:overriding = polymorphism
overloading != polymorphism



overriding=runtime / dynamic polymorphism
overloading=compile time/static polymorphism
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sudipto, punit is right. Overloading is AKA static polymorphism. Where did you read that there is no polymorphism in overloading??
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Punit, James and Ankit.
 
Ranch Hand
Posts: 643
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic