Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By DarrylBurke

Thread: Abstract Class return

  1. #1
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default 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;       
            }
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default 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;
       }
       ...
    }
    Of course the reproduce( ... ) method is abstract again in the Animal class itself.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Abstract Class return

    Quote Originally Posted by Robben View Post
    Java Code:
            if (reproduction(otherAnimal) == true) {
    Why stop at one?
    Java Code:
            if ((((reproduction(otherAnimal) == true) == true) == true) == true) {
    More seriously, never compare a boolean expression with a boolean literal. It's unnecessary, adds avoidable clutter, and when comparing a variable with a literal, is error prone.
    Java Code:
            if (reproduction(otherAnimal)) {
    Also, you might want to rethink your method names. I would name your reproduction(..) method canReproduce(...) or canReproduceWith(...).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Abstract Class return

    I feel a naughty orgy description coming up ...

    kindest regards,

    Jos
    milovan likes this.
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Abstract Class return

    Quote Originally Posted by JosAH View Post
    I feel a naughty orgy description coming up ...

    kindest regards,

    Jos
    Java Code:
    class Jos extends Animal {
    
      public boolean reproduction(Animal otherAnimal) {
        return true;
      }
    }
    db
    milovan likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Abstract Class return

    Quote Originally Posted by JosAH View Post
    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;
       }
       ...
    }
    Of course the reproduce( ... ) method is abstract again in the Animal class itself.

    kind regards,

    Jos
    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!

    Quote Originally Posted by DarrylBurke View Post
    Why stop at one?
    Java Code:
            if ((((reproduction(otherAnimal) == true) == true) == true) == true) {
    More seriously, never compare a boolean expression with a boolean literal. It's unnecessary, adds avoidable clutter, and when comparing a variable with a literal, is error prone.
    Java Code:
            if (reproduction(otherAnimal)) {
    Also, you might want to rethink your method names. I would name your reproduction(..) method canReproduce(...) or canReproduceWith(...).

    db
    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,

    Jos
    Lmao!
    Last edited by Robben; 03-12-2015 at 12:06 AM.

Similar Threads

  1. Interface + Abstract Class >= Abstract Class?
    By absentmaverick in forum Advanced Java
    Replies: 1
    Last Post: 10-08-2014, 07:01 PM
  2. abstract method-abstract class
    By durgaprasad1407 in forum New To Java
    Replies: 1
    Last Post: 04-29-2011, 06:58 PM
  3. Replies: 4
    Last Post: 04-14-2011, 11:42 AM
  4. Case with return statement within abstract
    By monia in forum New To Java
    Replies: 2
    Last Post: 09-21-2009, 07:47 PM
  5. Replies: 6
    Last Post: 02-11-2009, 10:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •