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 7179399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:07:56+00:00 2026-05-28T17:07:56+00:00

I have constructed a map and try to put subclasses of a given class

  • 0

I have constructed a map and try to put subclasses of a given class into it. Problem is that map accepts some of them, but not all. Please let me know what is the problem, why some subclasses are not accepted and how to fix it. I also tried to make class Cat and Dog extend Rodent (since Hamster works here), but this doesn’t work. Thank you

Here is the code for each class:

package typeinfo.pets;

public class Pet extends Individual {
  public Pet(String name) { super(name); }
  public Pet() { super(); }
} ///:~

package typeinfo.pets;

public class Rodent extends Pet {
  public Rodent(String name) { super(name); }
  public Rodent() { super(); }
} ///:~

package typeinfo.pets;

public class Cat extends Pet {
  public Cat(String name) { super(name); }
  public Cat() { super(); }
} ///:~

package typeinfo.pets;

public class Hamster extends Rodent {
  public Hamster(String name) { super(name); }
  public Hamster() { super(); }
} ///:~

import typeinfo.pets.*;

import java.util.*;

import static net.mindview.util.Print.*;



public class PetMap {

  public static void main(String[] args) {

    Map<String,Pet> petMap = new HashMap<String,Pet>();

    petMap.put("My Hamster", new Hamster("Bosco"));

    //the two lines here cause problems "Map<String, Pet> is not 
    // applicable to <String, Cat>
    petMap.put("My Cat", new Cat("Molly"));

    petMap.put("My Dog", new Dog("Ginger"));

    print(petMap);

    Pet dog = petMap.get("My Dog");

    print(dog);

    print(petMap.containsKey("My Dog"));

    print(petMap.containsValue(dog));

  }

}
  • 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-05-28T17:07:57+00:00Added an answer on May 28, 2026 at 5:07 pm

    As most of the users suggested, your problem doesn’t lie in the inheritance tree, but somwhere along the lines of what classes you imported and from where.

    The Cat and Dog you use in your main method may not be subclasses of Pet; probably that’s why Hamster is accepted and the others aren’t. I tried to make your code running and added what was missing. The following, for example works:

    import java.util.*;
    
    class Individual {
        public String name;
        public Individual(String name) {
            this.name = name;
        }
        public Individual () {
            this.name = new String();
        }
    }
    class Pet extends Individual {
      public Pet(String name) { super(name); }
      public Pet() { super(); }
    } ///:~
    
    class Rodent extends Pet {
      public Rodent(String name) { super(name); }
      public Rodent() { super(); }
    } ///:~
    
    class Cat extends Pet {
      public Cat(String name) { super(name); }
      public Cat() { super(); }
    } ///:~
    
    class Dog extends Pet {
          public Dog(String name) { super(name); }
          public Dog() { super(); }
        } ///:~
    
    class Hamster extends Rodent {
      public Hamster(String name) { super(name); }
      public Hamster() { super(); }
    } ///:~
    
    
    
    public class PetMap {
    
      public static void main(String[] args) {
    
        Map<String,Pet> petMap = new HashMap<String,Pet>();
    
        petMap.put("My Hamster", new Hamster("Bosco"));
    
        // the two lines now work
        petMap.put("My Cat", new Cat("Molly"));
    
        petMap.put("My Dog", new Dog("Ginger"));
    
        System.out.println(petMap);
    
        Pet dog = petMap.get("My Dog");
    
        System.out.println(dog);
    
        System.out.println(petMap.containsKey("My Dog"));
    
        System.out.println(petMap.containsValue(dog));
    
      }
    
    }
    

    Please recheck your package definitions and imports.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem that I need some help on but I feel I'm
I have a requirement that a Map will be constructed with up to 50~200
I have the following code that saves a map into shared memory using boost
Let's say I have a class called Map, and Map is constructed with argument
Python Decimal doesn't support being constructed from float; it expects that you have to
I've tried this a couple ways, the first is have a class that implements
I have a class EnumConverter<T extends Enum<?>> that converts strings to the correct enum
I have constructed a map and loaded it with data. If I iterate over
I have a class FourByFourBoard that extends GameBoard . I define the following fields:
I have an abstract base class called Curve . There are three classes that

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.