• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regd Abstract keyword

 
Manvir Singh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ppl,
Thanx for the replies to my first posting on Stringbuffers.
This time I have one more trivia.
SUN says U can over-ride a concrete method by an abstract method. Y in this World I wud do that? Need some pragmatic examples.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find this technique useful when you have multiple levels of inheritance. Let's say we've got an inheritance structure relating to animals. Let's start with the Animal class:

Now, let's say that we want to have a couple classes that depict a couple types of dogs. Well, obviously, many dogs have a handful of things in common, including the ability to fetch a stick. Therefore, we wouldn't want to duplicate that code in every class. So, let's define an abstract class to encapsulate the common features:

There are a couple things you should notice about this class. First of all, it contains a concrete method, fetchStick(), which allows all Dogs to share that ability. However, perhaps every Dog sleeps a little differently (it's a bit contrived, but it'll do) so I make that method abstract - even though it was concrete in the parent class, Animal. Now, let's finish our inheritance structure by creating a couple Dog classes:

By making the method abstract in the Dog class, we have forced the subclasses (the particular Dog classes) to implement their own, more specific, versions of that method. In this case, a Bulldog grumbles and snorts in its sleep while a Poodle whimpers in its sleep.
Obviously, this is a rather contrived example, but I hope you see the technique being used here.
 
Michael Zalewski
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there any examples in the SDK?
(where a concrete method is overridden by an abstract method?)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic