Returning New Class Object in Abstract Class

  • Thread starter Thread starter Robben
  • Start date Start date
  • Tags Tags
    Abstract Class
Click For Summary

Discussion Overview

The discussion revolves around the implementation of returning a new class object in an abstract class, specifically focusing on an abstract class named Animal. Participants explore how to structure methods related to reproduction among different animal subclasses, addressing both theoretical and practical aspects of object-oriented programming.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes an abstract class Animal with methods for reproduction and reproducing, seeking guidance on returning a new Animal object.
  • Some participants suggest modifying the reproduction method to return the type of Animal instead of a boolean, arguing that this would simplify the reproducing method.
  • Another participant insists on keeping the reproduction method strictly for determining reproductive capability, expressing the need for both methods.
  • Concerns are raised about the implementation of the reproducing method in the abstract class, with suggestions that it may be better placed in subclasses to avoid unnecessary complexity.
  • One participant questions the potential for infinite reproduction due to the logic used in the reproduction method, seeking clarification on how to prevent this issue.
  • There is a discussion about the need to override the reproduction method in subclasses to define specific reproductive behaviors between different animal types.
  • Clarifications are made regarding the nature of the Animal class and its subclasses, emphasizing that the reproduction method takes an Animal object as a parameter.

Areas of Agreement / Disagreement

Participants express differing views on the necessity and implementation of the reproducing method, with no consensus reached on the best approach. Some agree on the need for both methods, while others propose alternatives. The discussion remains unresolved regarding the optimal design for the reproduction logic.

Contextual Notes

Participants highlight limitations in their current understanding of how to structure the reproduction logic, particularly concerning the relationships between different animal subclasses and the implications of method design in abstract classes.

Robben
Messages
166
Reaction score
2

Homework Statement



How can I return a new class object in an abstract class?

Homework Equations



None

The Attempt at a Solution



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:
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;       
        }
    }
}
 
Physics news on Phys.org
You could modify the reproduction(otherAnimal) method to return the type of Animal instead of a boolean. Otherwise your reproducing() method would have to know about all of the different variations that could be produced by all animals.

For example, if a horse has its reproduction method called by another horse, you could return a horse. If it's called by a donkey then it would return a mule. If it is called by some other animal like a tiger, then return null. The reproduction method for a horse would just have to know about donkeys and other horses. Once you implement it, you'll see that the reproducing method isn't really necessary - just the reproduction() method.
 
Borg said:
You could modify the reproduction(otherAnimal) method to return the type of Animal instead of a boolean. Otherwise your reproducing() method would have to know about all of the different variations that could be produced by all animals.

For example, if a horse has its reproduction method called by another horse, you could return a horse. If it's called by a donkey then it would return a mule. If it is called by some other animal like a tiger, then return null. The reproduction method for a horse would just have to know about donkeys and other horses. Once you implement it, you'll see that the reproducing method isn't really necessary - just the reproduction() method.

But I need these two methods. The reproduction method should only return whether or not two instances can reproduce and nothing more.
 
Robben said:
But I need these two methods. The reproduction method should only return whether or not two instances can reproduce and nothing more.
Is reproducing() required by your instructor? What if you had reproduction() return null when a viable animal wasn't possible?
 
Borg said:
Is reproducing() required by your instructor?

Yup.

What if you had reproduction() return null when a viable animal wasn't possible?

The details of reproduction with other animals will be written in different classes which will be a subclass of Animal so reproduction with two different animals that aren't possible should not occur.
 
If you put reproducing() in the abstract Animal class, it's going to have to be able to return every type of possible animal. It would be better to implement it in the classes that implement Animal. Then a horse class won't have to know about tigers or bears.
 
Borg said:
If you put reproducing() in the abstract Animal class, it's going to have to be able to return every type of possible animal. It would be better to implement it in the classes that implement Animal. Then a horse class won't have to know about tigers or bears.

So I should make the reproducing method abstract in the house class and implement it through my subclasses?

Also, I noticed that if I do "reproduction(otherAnimal) == true" I will have infinite reproducing. How can I go about fixing that?
 
Robben said:
So I should make the reproducing method abstract in the house class and implement it through my subclasses?
Did you mean the Animal class? Then yes.
Robben said:
Also, I noticed that if I do "reproduction(otherAnimal) == true" I will have infinite reproducing. How can I go about fixing that?
Why do you think that? Maybe I'm not following what you're writing it at this point. Can you post your current code as we've discussed?
 
Borg said:
Did you mean the Animal class? Then yes.

Opps, I meant Animal. Thank you!

Why do you think that? Maybe I'm not following what you're writing it at this point. Can you post your current code as we've discussed?

For example, if one animal can reproduce with another, I want to limit the chance of reproduction somehow such that I won't have infinite reproduction, e.g.

if ((((reproduction(otherAnimal) == true) == true) == true) == true).

Because I was told to never compare a boolean expression with a boolean literal.
 
  • #10
Robben said:
For example, if one animal can reproduce with another, I want to limit the chance of reproduction somehow such that I won't have infinite reproduction, e.g.

if ((((reproduction(otherAnimal) == true) == true) == true) == true).
I don't think that it will be a problem. For now, can you scope out what you think the reproduction(otherAnimal) method will look like if you implement it inside of a Horse class?
 
  • #11
Borg said:
I don't think that it will be a problem. For now, can you scope out what you think the reproduction(otherAnimal) method will look like if you implement it inside of a Horse class?

If I implement it in a subclass of a Animal which is called Horse then I will have to override the reproduction method to reproduce with, let's say with a class of Donkeys, but with no other subclass of Animal. And each subclass of Animal can only reproduce with animals that are predetermined.
 
  • #12
Robben said:
If I implement it in a subclass of a Animal which is called Horse then I will have to override the reproduction method to reproduce with, let's say with a class of Donkeys, but with no other subclass of Animal. And each subclass of Animal can only reproduce with animals that are predetermined.
The reproduction method takes an Animal class. You can pass any animal in. A donkey is just another type of animal. Also, keep in mind that the otherAnimal object isn't a donkey or a tiger or a bear. It's an Animal.
 
  • #13
Borg said:
The reproduction method takes an Animal class. You can pass any animal in. A donkey is just another type of animal. Also, keep in mind that the otherAnimal object isn't a donkey or a tiger or a bear. It's an Animal.

I see, thank you!
 
  • #14
Robben said:
I see, thank you!
You're welcome. I'll be interested to see the final result. :smile:
 
  • Like
Likes   Reactions: Robben
  • #15
Borg said:
You're welcome. I'll be interested to see the final result. :smile:

Will do! It might take me awhile because it's a long code (most likely tomorrow) but I definitely post it once I am done.
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K