wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes The Ducks are back Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The Ducks are back" Watch "The Ducks are back" New topic

The Ducks are back

Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
I know for a fact that you can extend another class from a superclass that is abstract. Now, does the subclass have to be abstract too?
every example I have seen says no.
The reason that I am asking is because I cannon instantiate any duck objects when all of my child classes are abstract.
Here is my error:
"Duck.java": Error #: 454 : class project9_1.Duck should be declared abstract; it does not define method speak(java.lang.String) in class project9_1.Animal at line 3, column 8
The error goes away when I declare the Duck class as abstract, but then I wont be able to instantiate any duck objects in my main method, because the Duck class is abstract.
My purpose to create a duck object is because I am to write statements to make all elements in myPets to speak(�Hello.�)
Thanks in advance
Here is my code:

[ April 18, 2003: Message edited by: Steve Wysocki ]
[ April 18, 2003: Message edited by: Steve Wysocki ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hey SteveO,
The problem is that your speak() method in the Duck class is wrong. It should be declared public void speak(String msg). That is dictated by the abstract method in class Animal. Your speak method is an overloaded method of speak.

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
An abstact class can't be instantiated, you're right. You can't ever say 'new' on an abstract class, but that's what you want, and in fact that's the reason you mark the class as abstract in the first place. Usually, you make a class abstract because you want the class for the purposes of using the abstract class type as a polymorphic reference type (including the declared type of an array, for example), but you don't want anyone making a new object of the abstract type.
Your example is good, because you don't want anyone making an instance of type Animal -- it's too... abstract
What the heck does an Animal look like? We know what a Duck looks like, so it might make sense for Duck to be non-abstract (which we call 'concrete').
But the rule is that a non-abstract class MUST implement all of the abstract methods it has inherited from its superclasses. An abstract class gets a special exemption from that rule, so that's why if the compiler says, "Hey, if you are NOT going to fulfill the contract of Animal by implementing all of the abstract methods of Animal, then you yourself must ALSO be abstract. Declaring an abstract subclass means the subclass is passing the buck on implementing the abstract methods. But sooner or later, the first concrete subclass must implement all the abstract methods.
So... make your Duck class implement the speak(String s) method of Animal. Implementing the method means you must follow the same rules as you do for overriding, and overriding a method requires that your method argument list be identical (and return type as well) to the one in Animal. That looks like the problem in your Duck class... your speak() method in Duck is an overload, not an override, because your Duck's speak() method doesn't match the arguments of the Animal version.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Gee Kathy, I beat your post by 6 minutes, but your post really explains it better than my three lines. I guess that's why you're a cowgirl and author and I'm still a lowly bartender
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Ok now I understand overloading and overwriting in abstract classes. But here is my problem now.
After I fixed my speak method in the Duck class I get this after running my Test class:
[Duck3, Duck4, Duck1, Duck2, Duck0]
at java.lang.String.concat(String.java:1546)
at project9_1.Duck.speak(Duck.java:8)
This error repeats itself like a million times
all I did was add the code:
d1.speak("Hello"); to my test class
Im sure that the error is in my concat phrase but I dont know how to fix it.
And to repost my original purpose

My purpose to create a duck object is because I am to write statements to make all elements in myPets to speak(�Hello.�)

Thanks Mike and Kathy for so much help
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Ok, I realized what my problem was on that last post.
I changed my duck class to:

but now when I run test it gives me this error:
Exception in thread "main"
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Ok, I got that working I change my duck class up a bit to:

but now I am just wondering if there is a way to get each duck to say Quack Quack: Hello after their names like this:
Duck0 Quack Quack: Hello
Duck3 Quack Quack: Hello
Duck4 Quack Quack: Hello
Duck1 Quack Quack: Hello
Duck2 Quack Quack: Hello
because right now it just says:
Quack Quack: Hello
Quack Quack: Hello
Quack Quack: Hello
Quack Quack: Hello
Quack Quack: Hello
[Duck2, Duck4, Duck3, Duck0, Duck1]
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
lol, I guess I figured that one out too.
Wasn't too hard just tweaked my main method and I used: System.out.Println(myPets.get(count));
then right after I put in d1.speak("Hello")
Well I guess that wraps this one up.
Thanks again for your help Mike and Kathy
Did the Lone star get there yet?

[ April 27, 2003: Message edited by: Steve Wysocki ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
What the hell do you need us for Steve? You seem to be figurin' this stuff out pretty good on your own. Still waitin' on that armydiller piss. Plan to give it to the two coons that come up on my back porch every night that eat the left over cat food. Think they'll like it?
aadhi agathi
Ranch Hand

Joined: Apr 29, 2002
Posts: 263
Better late than never...
Why do we need abstract class???:-
1) Go thr' all the API list and
"study" the abstract classes.
OK, i will walk you through. Here we go...Consider the AbstractCollection, abstract class.
There are only 2 methods that are defined abstract namely
1)int size() and
2) Iterator iterator().
think for a second(preferably 2/3 days), why these implementations are delegated to the child classes.
Ok, i will do it for you.
1)take size(), how to define it???
one way is, the sum of elements in a collection.
Why not implement it in Abstract Class???
what if there is a criteria (implementation), wherein you want to ignore nulls/duplicates etc. looking from the AbstractCollection view, size can't be concretely implemented.
one implementaion of size() might be:-
1) getArray().length()
2) getArray()
//remove duplicates then return size
3) getArray()
//remove nulls then return size etc..
consider iterator() implementaion
1) return Array.toList(getArray()).itetrator()
2) you got it.........
But what is the beauty of an Abstract class??.

please check the following implementation[maybe] of sun's AbstractColllection

1) The abstract methods are a way to say "if you answer me a question aka method [note the anwer varies], then i will answer/solve some other question".
2) the parent is having lot of "hooks". the parent's concrete methods [not all methods] will depend on the hooks to solve some general stuff!!
like "if you say what's your size, then i will tell you whether you are empty"
3) An Abstract class is like a counsellor.
the "hook" can be placed anywhere in the general flow(any method) so that it gives a uniform implementation to all other concrete methods.
To give an real time example, imagine you are going to get the blessing from a Priest. The priest expects that you confess your sins before the blessing.[whether you really confess is another story ] . According to the contract of a Priest,read the soliloquy
"When anyone seeks my blessing, i will ask my self to confessSin, to purify my heart,i don't know how the confessSin would be ... it depends on the Priest and the religion.
different form of confessSin would be:-
1)removing some blood from the body
2)shouting from the roof top
3)praying to God
4)telling what kind of beast you are to the Person who asks the blessing.
Look at the flow above. i am plugging the confessSin() inside the getBlessing(). I can also plug-in the confessSin() before resignJob() or takeJob() or whatever . the point is that, some changable implementation is put inside the generic flow.
Hope this is of help! Looking for comments and appreciations
[ April 23, 2003: Message edited by: aadhi agathi ]

It is sorta covered in the JavaRanch Style Guide.
subject: The Ducks are back
Similar Threads
why am i able to call a static method using an object of that class ...
I know its goofy but Im having problems with it
Hey Mike, I need an idea for this one
Creating Objects
Polymorphism is the deebil