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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:49:47+00:00 2026-06-16T01:49:47+00:00

It seems to work but is it concidered ‘good’ programming? Here is an example

  • 0

It seems to work but is it concidered ‘good’ programming?

Here is an example to clarify:

public class foo{
   foomethod(){do something};
}

public class foo1 extends foo{
    @override foomethod(){do something};
}

public class foo2 extends foo{
    @override foomethod(){do something};
}

ArrayList x /*with foo1 and foo2 objects*/
for (Object o : x){((foo)o).foomethod();}

is this concidered ‘good’ programming and if not what is a nice compact alternative?
(apart from switch(o.getClass()) if possible)

edit because I am really bad at clarifying it seems 🙂

I do mean to call the method that has been overriden and not the super method.
An alternative method for what I want to do would be:

ArrayList x /* again with foo1 and foo2 objects*/

for (Object o : x){
    switch(o.getClass()){
        case foo1.class:
            ((foo1)o).foomethod();
            break;
        case foo2.class:
            ((foo1)o).foomethod();
            break;
    }
}

but I want to avoid the switch statement because it makes the code much bigger and complex.

  • 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-16T01:49:48+00:00Added an answer on June 16, 2026 at 1:49 am

    It seems to work but is it concidered ‘good’ programming?

    Modulo the problem with the wording of your question …

    Yes, it is good programming to declare a method in a superclass, override it in a subclass, and call it polymorphicly.

    It is certainly much better than using a chain of instanceof tests or a switch on the class name. Those approaches are considered “bad” … because they involve wiring too much knowledge of the class hierarchy into other code. For instance, if you add another subclass of Foo, all of those switches need to be checked and potentially changed. (And the compiler won’t remind you to do it …)

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

Sidebar

Related Questions

Yes, I have searched and tried many techniques, but nothing seems to work. Here
This seems in my head like it should work but I cant figure out
return false seems to work in the submitHandler but I'm not sure why. function
I have tried many permutations but they all don't seems to work well. Am
I am Using WaitforComplete() in watiN but it doesnt seems to work well. As
This evaluation in sql doesn't seems to work properly for some reason but i
I've searched similar topics but no solution seems to work. Is there a way
I tried things like $_ENV['CLIENTNAME'] == 'Console' but that seems to work on only
Is the following always acceptable code? It seems to work, but does it consistently
I'm trying to make a google-chart with transparency, but it seems not to work.

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.