aspose file tools*
The moose likes Beginning Java and the fly likes abstract classes/methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "abstract classes/methods" Watch "abstract classes/methods" New topic
Author

abstract classes/methods

Stephan Norwood
Greenhorn

Joined: Mar 23, 2006
Posts: 14
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

Joined: Mar 22, 2006
Posts: 16
"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

Joined: Mar 23, 2006
Posts: 14
@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

Joined: Mar 22, 2006
Posts: 16
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"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


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract classes/methods