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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:04:57+00:00 2026-06-11T02:04:57+00:00

I have a superclass called Canvas, from which I create several subclasses. I want

  • 0

I have a superclass called “Canvas,” from which I create several subclasses. I want to store instances of those subclasses in a dictionary. So I tried setting up my code as follows:

public class Main {
    public static void main(String[] args) throws Exception{
        Canvas canvas = new Canvas();



        Menu menu = new Menu();
        Instructions instructions = new Instructions();
        Map<String, Canvas> screens = new HashMap<String, Canvas>();
        screens.put("menu", menu);
        screens.put("instructions", instructions);

So, being that the things I want to add to the dictionary are all subclasses of my Canvas class, I set up the hashtable to accept that Type. And this works.. for the most part. However, the big problem is that I cannot access any of the subclass’ methods. I can only call methods that exist in the superclass.

If I attempt to do something like,

screens.get("menu").some_func_in_subclass();

The compiler spits out a “Can not find symbol” message.

So, if I have something like this:

SuperClass
  |
  |
  +------------------------------------   
        |               |             |
     SubClass1     SubClass2     SubClass3

How would I store instances of those subclasses in a HashMap?

  • 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-11T02:04:59+00:00Added an answer on June 11, 2026 at 2:04 am

    That’s the idea of polymorphism – all the sub-classes of Canvas share the same methods (from the Canvas super class) but if you add methods to those classes they are not shared any longer.

    One way would be to cast your result (assuming you have checked that the value really is a Menu):

    ((Menu)screens.get("menu")).some_func_in_subclass();
    

    But it defeats the purpose of holding everything in a Map<String, Canvas> in the first place.

    In the end, either you need Canvas objects, and you should not have to call sub-classes methods, or you need something more specific and there probably is a design issue.

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

Sidebar

Related Questions

I have a superclass called City and a bunch of subclasses that inherit from
Say I have one superclass, and several subclasses inheriting it. I'd like to cram
I have some classes which extends a superclass, and in the JSP I want
I have a superclass of UIViewController - MasterViewController which declares a property called itemsViewController
I want to define several plugins. They all inherit from the superclass Plugin. Each
Basically I have a Parent superclass which i.e is called MAMMAL. The mammal by
I'm new in JPA. I have a superclass called Person and two subclasses Teacher
Say I have superclass which listens to a notification: @implementation SuperClass - (void)viewWillAppear:(BOOL)animated {
I have a superclass we can call class A and few subclasses, e.g. class
If I have a superclass, say Animal , and two subclasses: Zebra and Giraffe

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.