• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Question about method overloading

 
Ranch Hand
Posts: 134
1
IntelliJ IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading about method overloading and method overriding and trying to understand the context in which they occur.

Method overloading happens when we declare a method of the same name but with a different parameter list in the same class. For example, in the following class A, the method test is overloaded.


Method overriding happens when a subclass defines the same method with the exact same method signature. In the following class B, which extends A, overrides method named test.


My initial assumption is overloading only happens within a single class and overriding happens only when inheritance is involved.

If so, then what is the following situation called? To me, it appears the method testis being overloaded. This means overloading can span multiple classes. Does it mean my initial assumption about overloading can happen only within a single class is false?

 
Marshal
Posts: 26128
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Quazi Irfan wrote:Does it mean my initial assumption about overloading can happen only within a single class is false?



Yes and no.

In the last of your examples, class B has three methods whose name is "test". So they are overloaded methods. Sure, it's also true that two of the methods are inherited from class A, but they are still methods of class B. The idea of "within a single class" is an unhelpful idea.
 
Quazi Irfan
Ranch Hand
Posts: 134
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a bit confused as to why you said 'Yes and No'. If test method in B is overloaded, then my assumption is clearly false.
 
Marshal
Posts: 71070
292
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Quazi Irfan wrote:. . . Method overloading happens when we declare a method of the same name but with a different parameter list . . .

Correct. You can declare an overloaded method in a subclass too. You can overload constructors, but they don't appear in subclasses.

Method overriding happens when a subclass defines the same method with the exact same method signature. . . .

That should read, “...defines the same non‑private instance method with the exact same method signature.” The usual rules about access modifiers apply. You cannot override static methods.

My initial assumption is overloading only happens within a single class . . .

No, I would call your last example overloading. If those three classes are in the same package (they wouldn't compile in different packages), as Paul says, the two methods in A are inherited by B.
 
Saloon Keeper
Posts: 12493
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Quazi Irfan wrote:I am a bit confused as to why you said 'Yes and No'. If test method in B is overloaded, then my assumption is clearly false.


He said "Yes" because your assumption that method overloading can only happen in a single source file is false. Methods are inherited by subclasses in other source files, and so you can overload them in a different file.

He said "no" because when you overload a method in a different source file, the class in that second source file inherits the methods from its superclass. Even if the overloaded method is not a part of the source file that declares B, it IS part of the class B.
 
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Quazi Irfan wrote:Does it mean my initial assumption about overloading can happen only within a single class is false?


The idea of "within a single class" is an unhelpful idea.


The discussion after this demonstrates why the idea is unhelpful.

Let me give you this example instead:

All of the following are true:
- Line 5 overloads bar()
- Line 9 overloads foo()
- Line 11 overloads bar()

So overloading doesn't really depend on which class the overloaded method is defined; overloading can happen in the same class and it can happen in a subclass.

I think the nuance you should be more concerned about is exactly which overloaded methods you can call using a given reference. Given my example above, you can't do this:

Line 2 fails to compile because given only a reference type of A, the compiler doesn't know about the overloaded version of bar() that's defined in class B. It can only dispatch to the overloaded bar on either line 4 or line 5. Because the reference is declared as type A, the compiler will only use the information it has from class A. So in this case, it does matter where the overloaded method bar() is defined. This is probably why Paul's answer was "Yes and No."
 
Paul Clapham
Marshal
Posts: 26128
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:This is probably why Paul's answer was "Yes and No."



Yes; to put it differently, the idea of "a single class" is ambiguous, as you have demonstrated.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method Overloading: When we have one class and we have two methods with same name but different parameters, this is known as method overloading.
Let me give you an example:



So, it will depend on the user that how many parameters will be passed, now if the user passes let's say 5 numbers then the program will look for the sum method for 5 parameters it will not find any and it will show error

Method Overriding: This is for two different classes with same method name and parameters but in different classes. Now the action might not be the same here, let me give you an example: Lets assume your father have a phone, now you can say your friends to call at your fathers phone to contact you (As you don't have a phone), you are inherited from your father. Now you got a phone will you tell your friends to call you father anymore??



So this is how it works not lets say if the son didn't have a phone then probably he would use super function to call.

if there is any mistake please do corrections, there might be some syntax errors forgive me for those.
 
Campbell Ritchie
Marshal
Posts: 71070
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You omitted to say when the different methods are selected.
You have also made the serious error of showing those static methods as if they were overridden.

Always use the @Override annotation whenever you think you are overriding methods:-In this case you will get a compile time error on line 8.

Always start Class Names With Capital Letters. Why did you declare those classes as inner classes?
 
Paul Clapham
Marshal
Posts: 26128
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You have also made the serious error of showing those static methods as if they were overridden.



And omitting to declare their return types (which should be void). Makes them look sort of like a static constructor, if such a thing existed in Java (it doesn't.=).
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic