• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overriding vs. Overloading  RSS feed

 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am aware that method signatures are identical for leagal overrides, and that the argument list changes for leagal overloads.

But what if the argument list changes only in that it takes a sub type of the parameter in the original method? Would the new method be considered overloaded because the parameter changed, or since the parameter is a sub type, would the argument list be considered unchanged, and therefore it is an override?

See code below. Is Two.doSomething() overloading One.doSomething()?

Thanks in advance,
-Russ
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the @Override annotation in front of your overriding method and see if the compiler complains.
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it does. Says I'm not overriding. So this is a completely different method signature. Thanks!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what happens when you have this?



Doesn't overriding makes sense? ... meaning which method will be called if it was an override?

Henry
 
Chetan Sarnad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overloading concept involves two or more methods having the same name in the same class.
The example you have quoted depicts inheritence i.e a super and subclass. To overide the method in super class, the method in the subclass should have the same method signature.
 
Rumesh Eranga
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have use primitive types here.Isn't your code similar to mine?
This is not overriding.This is overloading.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!