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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:20:44+00:00 2026-05-26T10:20:44+00:00

Okay so I’ve been set an assignment from university and I just can’t get

  • 0

Okay so I’ve been set an assignment from university and I just can’t get my head around how this problem, I’m the only person in the class that’s got this far and my lecturer’s aren’t getting back to me.

Basically it’s on inheritance and polymorphism, we are using a parent class and then creating the children from the parent;

private HashMap<String,Aircraft> allAircraft = new HashMap<String,Aircraft>();
Aircraft plane = new Plane(reg,pass,cargo);

So the parent cannot use the children methods, I understand the majority of the concept behind this and have managed to get my head around it. For one of the methods we have specifically been asked to use casting (even though I have been told countless times that if I have to use it then I should restructure) but for the other method we have been asked to call a method which is only specific of two of the children classes, casting won’t work here because I don’t know what the children classes are in the HashMap.

So my question is; how would I favor two children classes methods over the parent class whilst using polymorphism?

My mind is quite literally exploding.

  • 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-26T10:20:45+00:00Added an answer on May 26, 2026 at 10:20 am

    If you need to downcast, then you’re not using polymorphism anymore. If that’s what the teacher wants, then you may still downcast in a safe way by testing that the actual type of the object is the appropriate one :

    if (plane instanceof SubClass1) {
        SubClass1 s = (SubClass1) plane;
        s.someSpecificMethod();
    }
    if (plane instanceof SubClass2) {
        SubClass2 s = (SubClass2) plane;
        s.someSpecificMethod();
    }
    

    If this method is common to both subclasses, it might mean that they in fact share the same interface. So polymorphism could come back into play here :

    public interface PassengerTransporter {
        void someSpecificMethodToAllPassengerTransporters();
    }
    
    public class SubClass1 implements PassengerTransporter {
        void someSpecificMethodToAllPassengerTransporters() {
            // TODO : implement this
        }
    }
    
    public class SubClass2 implements PassengerTransporter {
        void someSpecificMethodToAllPassengerTransporters() {
            // TODO : implement this
        }
    }
    
    if (plane instanceof PassengerTransporter) {
        PassengerTransporter p = (PassengerTransporter) plane;
        p.someSpecificMethodToAllPassengerTransporters();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I've been messing around with the excellent JodaTime library, attempting to get a
Okay, before you guys go nuts -- this is just a small site, temporary
Okay people, I'm sure someone has had this issue and can help me out.
Okay,I've been following this tutorial http://coenraets.org/blog/android-samples/androidtutorial/ Basically it gives me exactly what i need
Okay, so I've known about this for like 600 years now, but I've only
Okay, first some background, I can't use any javascript library except YUI for this
Okay i have this problem with every page i make. im not sure what
Okay. I know this looks like the typical Why didn't he just Google it
Okay, so this seems simple, but I can't think of a straightforward solution; Basically
Okay so this has been modified there is still 3 tables board, account, log

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.