Win a copy of Head First Android this week in the Android forum!
  • 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 Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Overriding and Overloading Help Needed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys,

I am reading Head First Java and i was not able to understand the concept of method overriding (something to do with arguments) and overloading...

i understood inheritance and that sub class can override a superclass but i didn't get how method overriding works and what is overriding to do with arguments and return types.

Can anyone please explain me what is method overriding and overloading??? And if you can even explain what are arguments and return types.... that would be great...

thanks....
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is the ability of the object to behave differently for the same message.

Overriding gives the benifit of having the behaviour specific to the subclass.->Which method to be called is known at compile time.Addng on the access level can be less restrictive then the overridden method.And can throw runtime exception even if the overridden method doesn't.
Overloading cleanliness of code .Take an example of valueOf(Object obj).It takes boolean,char,int ..-->Which method to be called is know at run time.

In the below example try removing the static before methd and try it gives the overriding behavior:




For overloading try the below:I have changed the arguments in the method:



 
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What the previous post means is, run those classes with static (not [b] [/b]) before "methd", then delete the word static from both classes before "methd" and see the difference.

We have an FAQ which may be helpful to you.
 
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overriding and Overloading are two different things, its easy to get them confused

Overloading, in my opinion is the easier one to understand

Overloading basically means having more than one method that uses the same name

For example, you may have the following method in your code :



Thats all fine and dandy, but what if you want to call the same method, but you don't want to pass a String in? Maybe you have an int instead, you can do this :



Making sense? OK, put those two methods into your IDE, such as eclipse. Then, underneath those, hold control and press space, this should bring up a window of available members. You should see both those methods there.

You can call either method depending on what you want to do.

The method names must be the same, and the input values (arguements) must be different types. Read that chapter in head first again, they have some good examples

Also, take a look at the String API, you can see that several methods have the same name, but different arguements. Have a look at what they do, this will help you understand reasons for doing overloading.

Overriding.

Say you have a class called Car, it has a method called driveFast().

Then you create a class called Mustang, that extends Car.

Because it extends car, it (not worrying about access levels), will have access to driveFast()

This might be ok for you, but maybe you don't want to use driveFast() on Car, but you want to provide your OWN implementation, that is specific to Mustang, that is what overriding is good for
 
reply
    Bookmark Topic Watch Topic
  • New Topic