• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Needs explanation  RSS feed

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can a method that�s overloaded but not overridden by a subclass.
It�s perfectly legal to do the following:
public class Foo {
void doStuff() { }
}
class Bar extends Foo {
void doStuff(String s) { }
}

hi all,
how can the above is possible?can anybody provide the explanation for the above?


thanks
venkat
 
Ranch Hand
Posts: 431
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not?

I could not understand why you got this doubt first of all. Leave that. Lets come to the topic.


public class Foo {
void doStuff() { }
}
class Bar extends Foo {
void doStuff(String s) { }
}


Above is the code given by you.
If you consider the following code


Class Bar
{
void doStuff() { }
void doStuff(String s) { }
}


what will you say?...This is a piece of code with method overloading..right?...Similarly when you extend a class with some methods that are visible to the child class you can call them with the child object also. That means they are almost in Child class except for the fact that they are derived methods. Now if u add one more method in the child class with the same method name as the one got from parent but with different set of parameters, how will u call it?...Thats what you have done in your code.

Method overriding completely replaces the method from the parent. But here you can still access the method from your parent (no-arg) as well as your own method with the same name(1-arg). So this is method overloading not overriding.

Clear?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!