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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:33:00+00:00 2026-05-29T09:33:00+00:00

I know this is a basic question, but I can’t find other StackOverflow posts

  • 0

I know this is a basic question, but I can’t find other StackOverflow posts or any good API docs on this.

Say I have an abstract class like Appliance and then I have some classes like Toaster and Blender that extend Appliance. Now suppose that I want to create an ArrayList that will contain mixed elements, all of which are ultimately members of Appliance but could also be Toaster or Blender as well. The Blender class has a method called turnBlenderOff() and the Toaster class has a method called turnToasterOff(), and I will want to iterate over my ArrayList and call these methods, depending on which subclass the element actually belongs to.

Currently I make a class called PowerPoint and try:

 // Constructor given an ArrayList of appliances.
 public PowerPoint(ArrayList<Appliance> initial_list_of_appliances){
     int listSize = initial_list_of_appliances.size();
     for(int ii = 0; ii < listSize; ii++){
         this.applianceList.add(initial_list_of_appliances.get(ii));
     }
 }

 /////
 // Method to switch everything in the list OFF simultaneously.
 /////
 public void switchOff(){
     int N = this.applianceList.size();
     String cur_name;
     for(int ii = 0; ii < N; ii++){
         cur_name = this.applianceList.get(ii).getClassName();
             if(cur_name.equals("Blender")){
                 this.turnBlenderOff(this.applianceList.get(ii));
             }
             else if(cur_name.equals("Toaster")){
                 this.turnToasterOff(this.applianceList.get(ii)); 
             }
             else if(cur_name.equals("Oven")){
                 this.turnOvenOff(this.applianceList.get(ii));      
             }
         }
     }

Most of my code compiles fine, but I keep getting this error message:

 PowerPoint.java:83: turnBlenderOff(appliances.ApplianceWrapper.Blender) in PowerPoint cannot be applied to (appliances.ApplianceWrapper.Appliance)
      this.turnBlenderOff(this.applianceList.get(ii));

I see that this method, implemented to work only on Blender objects is trying to be executed on an Appliance object that happens to be a Blender but that the compiler doesn’t realize this.

I tried to replace the <Appliance> type with <? extends Appliance> in the ArrayList specifications, but that gave additional errors and would not longer compile.

What is the proper way to make a list based on the abstract type, but then call methods of the subclassed type by using something like getClassName() to retrieve the subclass type?

Added

Since a lot of folks immediately pointed out the obvious: use inheritance better, I need to explain. For this project, we have to assume that all of the subclasses of Appliance were created by third-party people and put into some package that we cannot change. This was done in a bad, crufty way in which all different subclasses have different on/off methods and this can’t be changed. So the option of designing a smooth Appliance abstract class is not open to me. For example, Toaster has the method startToasting(), while Oven has the method heatUp(), each of which serves as the ‘on’ method for the two different classes.

I think the exercise is meant to teach us: how would you retro-fit someone else’s bad design job so as to minimize future damage.

  • 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-29T09:33:00+00:00Added an answer on May 29, 2026 at 9:33 am

    Use instanceof or getClass, not rolling your own getClassName, and then do an explicit cast to the type you just identified.

    That said, prefer @guitarflow’s answer, though that approach might not work if there is state that you can’t just pass to the switchOff method.

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

Sidebar

Related Questions

I know this maybe a basic question but I just can't seem to find
I know this is a basic question, but I can't seem to find an
I know this question sounds very basic. But I can't find it using Google.
OK, so I know this question seems really basic, but I can't find a
I know this maybe a very basic question but I'm having a bit of
I know this should be a basic question but I am hitting a brick
I know, this might be a very basic question but I am not 100%
Ok, I know this is a very basic question, but my head is swimming
guys I know this question is very basic but I've met in few publications
I know this is a basic question, but I'm having a bit of trouble

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.