Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regd Abstract keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regd Abstract keyword" Watch "Regd Abstract keyword" New topic

Regd Abstract keyword

Manvir Singh

Joined: Oct 19, 2003
Posts: 6
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

Joined: Dec 20, 2001
Posts: 3271
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.

SCJP Tipline, etc.
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168
Are there any examples in the SDK?
(where a concrete method is overridden by an abstract method?)
jQuery in Action, 3rd edition
subject: Regd Abstract keyword
It's not a secret anymore!