• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract classes/methods

 
Stephan Norwood
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am a little bit confused.
(I use "Head First Java 2nd Edition")

Suppose:
You have a class (ANIMAL) marked as abstract with
public abstract methods and
public instance variables.
You have the following abstract subclasses: FELINE,CANINE,extended from ANIMAL
Both have inherited the abstact methods of their superclass ANIMAL.

The first concrete class in the inheritance tree must implement all abstract methods.

Now my problem:
Suppose I want to create the (not abstract) class DOG which is extended from CANINE.

So I have to implement all of the abstract methods inheritated from ANIMAL and CANINE.

But since CANINE is extended from ANIMAL it has the same abstract methods.
(CANINE does not get any new methods)
So they are identical;except for the name.

I don't understand why I should create a class like CANINE ?

Why not simply extend a dog class simply from the ANIMAL class
without a class like CANINE???

Or in general: why not create a superclass with only abstract methods (you will implement the methods later in your concrete class) and extend anything from this superclass.

like you don't need a CANINE or FELINE class and only abstract any animal from this one superclass ANIMAL

I know it sounds weird,but I hope somebody is getting my point....

Thanks in advance
 
Shaun Williams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Darth",

As I understand it, as that inhertiance tree stands, it doesn't make sense (although i'm sure that there will be some argument against having such a wide tree, though i can't think of any as it stands)

However, abstract classes can have non-abstract methods: behind the argument of coding to an interface and not revealing the implementation, one can argue that surly there are some methods that the implementation for certain groups of species (forgotten what then are called: genus??) will be the same; if then one specific animal does have a slight diffrent implementation for a method, it can be overloaded/overridden.

I'm sure thats one of the arguments for why this is the case; but your freindly book authour didn't have the time or felt it wasnt nessacary to flesh out those methods.

Saying all that, i'm sure other people have totally diffrent opinons...
It'll be intresting to see.

Shaun
 
Stephan Norwood
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Shaun

thanks for your reply

the author mentioned that abstract classes can have non-abstract methods.
That's why I said CANINE does not get any new method.
 
Shaun Williams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan,

My point was that although the author chose not to write code to 'flesh out' thoose methods, some of the abstract CANINE methods could have been implemented for some behavour all canines exhibited. (Do all canines bark when they 'speak' for instance?)

Also, are the public instance varibles diffrent in the implementation of CANINE and FELINE?

Hope that helps clear things up

Shaun
[ March 23, 2006: Message edited by: Shaun Williams ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The author may want to have future behavior (a method) that works only on Canines and another that works only on Felines.

public void doSomethingDoggy( Canine subject )

public void doSomethingCatty( Feline subject )

Hmm, those sound vaguely dirty. Anyhow, I'm not sure I've seen anyone make abstract classes just for that purpose and it may or may not be a Good Thing. I'm just making up explanations for something we don't understand, so it could be way off.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Stephan,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic