• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why overriding concept is used  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know overriding concept. Why this is being used as we can use different name of function in subclass and can implement the things which we wanted to implement in same name function?
I want example where we can use only overriding concept..without that code will fail. If anybody can guide me, it would be a greate help in clearing my understanding.
Thanks.

*removed bold letters
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to specialize your super class method, you do overriding. for example, look at Object# toString. if you rely on this, when you print your object to see, wont be expressive. so you override.

there are many example. polymorphism is the one of the key player in Oop.

<edit>

</edit>
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Even I also thought about toString method

Another good example is equals method.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have already been shown good examples. Just wanted to add mine though.



The makeSound() method is overridden in the subclasses to match the subclass animal's behaviour.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically when you wanted to have a same prototype of a parent class and different behavior of its children in that case you use this technique of overriding a method.
As you said, these methods will work with different names but for that, your parent class must have that many signatures of similar (but not same) methods.

I'll use above example to explain it little further.

Now suppose you wanted to create an object of class Animal, you have 2 implemented versions of this class. So

In above example we can also have separate methods, but with that you wont be able to use a concept of abstraction and polymorphism.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
geeta kasar wrote:I want example where we can use only overriding concept..without that code will fail. If anybody can guide me, it would be a greate help in clearing my understanding.

It's not generally a case of whether it fails or not; it's of programming that makes sense.
Personally, I've always liked the Shape example:If you run it, you'll find that it prints out:
a Circle
a Square
a Triangle
a Square

which proves that the overridden method is being run in each case. Polymorphism in action.

Tip: Although it's not required, it's worth using the "@Override" annotation when you write overridden methods. It helps the compiler to give you nice meaningful messages; especially if you change things later on.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!