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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:58:43+00:00 2026-06-04T18:58:43+00:00

I am writing a method that should accept as its parameter an object of

  • 0

I am writing a method that should accept as its parameter an object of one of two types which do not share a parent type other than Object. For example, the types are Dreams and Garlic. You can do both dreams.crush() and garlic.crush(). I want to have a method utterlyDestroy(parameter), that would accept as its parameter both Dreams and Garlic.

utterlyDestroy(parameter) {
    parameter.crush()
}

Both Garlic and dreams are a part of some library, so having them implement an interface ICrushable (so that I could write utterlyDestroy(ICrushable parameter) ) is not an option.

My method body is quite long so overloading it would mean duplicating code. Ugly.
I am sure I could use reflection and do some class hacking. Ugly.

I tried using generics but apparently I cannot write something like

utterlyDestroy(<T instanceof Dreams || T instanceof Garlic> parameter)

Is it possible to typecast Garlic to Dreams?

utterlyDestroy(Object parameter) {
    ((Dreams)parameter).crush()
}

This would still be ugly though. What are my other options and what is the preferred method of dealing with the situation?

  • 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-04T18:58:44+00:00Added an answer on June 4, 2026 at 6:58 pm

    How about this:

    interface ICrushable {
        void crush();
    }
    
    utterlyDestroy(ICrushable parameter) {
        // Very long crushing process goes here
        parameter.crush()
    }
    
    utterlyDestroy(Dreams parameter) {
        utterlyDestroy(new ICrushable() { crush() {parameter.crush();});
    }
    
    utterlyDestroy(Garlic parameter) {
        utterlyDestroy(new ICrushable() { crush() {parameter.crush();});
    }
    

    New development should implement the ICrushable interface, but for the existing Classes, the parameter is wrapped in an ICrushable and passed to the utterlyDestroy(ICrushable) that does all the work.

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

Sidebar

Related Questions

I'm writing the spec for controller: it 'should call the method that performs the
I am writing a deserialization method that converts xml to a Java object. I
I'm writing tetris right now, I have a method that drops figures one by
I've got a method in a class that's writing to some string, which calls
What happens when you call notifyAll method on an object that is not waiting?
Each time I'm writing a method that takes a boolean parameter representing an option,
I am interested in writing a method that would accept another method as a
I am writing a crawler/parser that should be able to process different types of
I'm writing a method that should extract the fully qualified class name out of
I'm writing a model instance method that should find an AR association if it

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.