Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8591635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:34:18+00:00 2026-06-11T23:34:18+00:00

I have two objects which use really similar methods, save for one line. For

  • 0

I have two objects which use really similar methods, save for one line. For example:

public class Cat extends Animal
public class Dog extends Animal

And they both use a breed method in the abstract class Animal. One calls new Dog(), and the other new Cat(). Right now I just have it declared as abstract public void breed(); in Animal, but is there a way I can generalize it so I don’t have to make it an abstract method to be overridden?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:34:19+00:00Added an answer on June 11, 2026 at 11:34 pm

    There are many ways to do this, assuming by breed you mean “create children of me.”

    Reflection

    First is to use reflection. If you have a no-args constructor for your classes, this is as easy as calling Class.newInstance:

    public Animal breed() {
        try {
            return (Animal) getClass().newInstance();
        } catch (Exception ex) {
            // TODO Log me
            return null;
        }
    }
    

    If you don’t have a no-args constructor in all your subclasses, you’ll have to have a uniform constructor across all your subclasses. For example, if you have Cat(int, String) and Dog(int, String), then you need to get the constructor via Class.getConstructor and invoke newInstance on that:

    return (Animal) getClass().getConstructor(int.class, String.class).newInstance(0, "Unnamed");
    

    int and String here may be age and name, for example. This is how you do this with reflection.

    Providers

    Another way is to use this simple interface:

    public interface Provider<T> {
        T create();
    }
    

    Then have your abstract class take an instance of this in its constructor:

    public abstract class Animal {
        private final Provider<Animal> animalProvider;
    
        protected Animal( ... , Provider<Animal> animalProvider) {
            // ...
            this.animalProvider = animalProvider;
        }
    
        public Animal breed() {
            return animalProvider.create();
        }
    }
    

    Then your subclasses will pass a Provider<Animal> to the superclass which will create new instances of the subclass:

    public class Dog extends Animal {
        public Dog( ... ) {
            super( ... , new DogProvider());
            // ...
        }
    
        private static class DogProvider implements Provider<Animal> {
            public Animal create() {
                return new Dog( ... );
            }
        }
    }
    

    Do the same for other subclasses as well.

    Note: if by breed you mean “get the type of me,” then you should edit your question to say so. If this is what you meant, then this is a viable solution:

    public abstract class Animal {
        protected final Breed breed;
    
        protected Animal( ... , Breed breed) {
            // ...
            this.breed = breed;
        }
    
        public Breed getBreed() {
            return breed;
        }
    }
    

    I recommend following the get/set conventions for data container methods. Java has bean classes designed to handle these naming conventions, and it’s more or less a standard across many platforms. For your subclasses:

    public class Dog extends Animal {
        public Dog( ... ) {
            super( ... , new Breed( ... ));
            // ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two objects and I want to merge them: public class Foo {
I have a method which compares two objects and returns a list of all
I have a helper function, which basically calls CompareTo on two objects, but does
I have two objects of the same class (Model) type. I want to check
In my game I have a base class of Loot which has methods universal
So I have two custom complex types like this (oversimplified for this example): public
Currently I have an object which I receive from a server, this contains two
I have two vc++ dll projects, which will generate two dlls. A vector<vector<string>> object
I have a an object stored in the model called domain, which has two
I have two objects I need to create a relationship between in Core Data.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.