Results 1 to 6 of 6
Thread: Abstract Class return
- 03-11-2015, 09:16 AM #1
Member
- Join Date
- Feb 2015
- Posts
- 67
- Rep Power
- 0
Abstract Class return
Suppose I have an abstract class named Animal (there are many other animal types that are made as classes). In this abstract class it has two methods, reproduction(Animal otherAnimal) and reproducing(Animal otherAnimal). Reproduction determines if the animal can reproduce with another instance of an animal. Reproducing should return a new Animal of the same type if reproduction is possible, but I do not know how to do this because the class is abstract.
Java Code:public abstract class Animal{ public abstract boolean reproduction(Animal otherAnimal); public Animal reproducing(Animal otherAnimal) { if (reproduction(otherAnimal) == true) { return ... //how can I return a new Animal of the same type? } else { return null; } } }
- 03-11-2015, 09:24 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Abstract Class return
Why don't you let the (concrete subclass of) Animal reproduce itself? It should test the otherAnimal and reproduce if it is a compatible animal. The 'instanceof' operator can come in handy here, e.g.
Java Code:public class Aardvark extends Animal { ... public Animal reproduce(Animal otherAnimal) { if (otherAnimal instanceof Aardvark) return new Aardvark(); return null; } ... }
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 03-11-2015, 09:58 AM #3
Re: Abstract Class return
Why stop at one?
Java Code:if ((((reproduction(otherAnimal) == true) == true) == true) == true) {
Java Code:if (reproduction(otherAnimal)) {
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 03-11-2015, 10:32 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Abstract Class return
I feel a naughty orgy description coming up ...
kindest regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 03-11-2015, 12:09 PM #5
- 03-12-2015, 01:03 AM #6
Member
- Join Date
- Feb 2015
- Posts
- 67
- Rep Power
- 0
Re: Abstract Class return
Hmm, I see so I should instead make my current reproducing method abstract and when I build my subclass of Animal then I will use your method using instanceof? Thank you!
Oh wow, thank you for catching that! How will I go about making the necessary change in order to avoid infinitely reproducing?
I feel a naughty orgy description coming up ...
kindest regards,
JosLast edited by Robben; 03-12-2015 at 01:06 AM.
Similar Threads
-
Interface + Abstract Class >= Abstract Class?
By absentmaverick in forum Advanced JavaReplies: 1Last Post: 10-08-2014, 08:01 PM -
abstract method-abstract class
By durgaprasad1407 in forum New To JavaReplies: 1Last Post: 04-29-2011, 07:58 PM -
Class is not abstract and does not override abstract method run(com.
By rgeurts in forum New To JavaReplies: 4Last Post: 04-14-2011, 12:42 PM -
Case with return statement within abstract
By monia in forum New To JavaReplies: 2Last Post: 09-21-2009, 08:47 PM -
Difference between Abstract class having only abstract method and a Interface class
By Santoshbk in forum New To JavaReplies: 6Last Post: 02-11-2009, 11:51 AM
Bookmarks