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.
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.
Overriden methods cannot throw broader exceptions than its superclass method.
Overriden methods cannot have a lower access modifier than its superclass method.
Bala
SCJP 5.0
Gonna hunt down SCWCD soon..
sharmistha mohapatra
Greenhorn
Joined: Dec 12, 2008
Posts: 18
posted
0
Is it possible to have the overriden method in the same class???
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!
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.
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.
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".
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 ! !
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".
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?
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.
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.)
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
posted
0
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.
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.