• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overriding methods in java  RSS feed

 
Rd Dari
Ranch Hand
Posts: 214
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am so confused about overriding purpose.Why we use overriding in java or what is the purpose of overriding in java?Can anyone clear it?

Thanks
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recommended reading http://download.oracle.com/javase/tutorial/java/IandI/override.html
To quote from that article,
The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed....


Imagine a class Bell which has a method call ring. The default behavior of this method is to make a ringing sound.
Now imagine bicycle bell, church bell, door bell. All are bells . So they would subclass the Bell but override to produce tring,toll and bingBong sounds in their ring method.
 
Rd Dari
Ranch Hand
Posts: 214
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Imagine a class Bell which has a method call ring. The default behavior of this method is to make a ringing sound.
Now imagine bicycle bell, church bell, door bell. All are bells . So they would subclass the Bell but override to produce tring,toll and bingBong sounds in their ring method.


OK I am giving some example as you say but how I determine it that what is basic role of "Overridding".Can I call ring method of any class but output should be same?

Is this code is right?





 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I call ring method of any class but output should be same?

If you want the output to be the same then simply dont override the ring()
 
Rd Dari
Ranch Hand
Posts: 214
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In actually I want to overriding example but in the above code I want to that only one object but print all statements by its ring method. Is it possible or not?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry but I am unable to understand your english. Can you rephrase it please?
Are you trying to ask that you will invoke ring only once but you should get all outputs?
 
Campbell Ritchie
Marshal
Posts: 55760
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't quite understand the request. Do you want a class to invoke methods of its subclasses, or of another class? How could that be done?
 
Rd Dari
Ranch Hand
Posts: 214
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Are you trying to ask that you will invoke ring only once but you should get all outputs?


Yes I want to this type of coding.And so sorry if you are unable to understand my phrase in last request.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my mind, overriding is best seen when you deal with a collection of things. Say I have an array of Bell objects. Due to inheritance, I can put church bells, hand bells, bike bells, and doorbells all in my array.

I can then write a simple loop that goes through the array:

for (int i = 0; i < array.length; i++)
{
array[i].ring();
}


This will ring each and every bell, in turn, and due to over-riding will give me "bong", "ding", "ding ding", and "bing-bong". I can create brand new kinds of bells a year later, drop them into my collection, and this code doesn't need to be touched to ring the new bell's new method.

That is pretty cool.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rd Dari wrote:
Maneesh Godbole wrote:Are you trying to ask that you will invoke ring only once but you should get all outputs?

Yes I want to this type of coding.And so sorry if you are unable to understand my phrase in last request.

That is not possible with overriding. In fact it goes totally agains the principle of overriding. Of course you can subclass in a chained fashion. ChurchBell extends DoorBell which extends CycleBell which extends Bell and invoke super.ring() but thats not proper OOP design.

If you can explain what you are trying to do here, we can possibly offer some suggestions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!