• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Overriding methods in java

 
Ranch Hand
Posts: 214
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13069
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ice is for people that are not already cool. Chill with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic