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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:10:28+00:00 2026-05-31T08:10:28+00:00

I have a programming problem in Java: Can I define a different method for

  • 0

I have a programming problem in Java:

Can I define a different method for any different interface array element?

myInterface[] op = new myInterface[4];

Now “myInterface” only has 1 method: public static int doSomething(int a, int b);.

I need to define that method to do different things with the numbers based on the element id (0-3).

Restrictions:

  1. must use hash tables.
  2. no switch-case or if statements (because of hash tables)
  3. cannot under any circumstance use the Java defined hash tables

i.e. if I call op[0].doSomething(2,3) it’ll output 12 (2^2 * 3)
and if I call op[1].doSomething(2,3) it’ll output 5 (2+3).

EDIT: I’d like to know if its possible or am I chasing my tail?
if it is could someone suggest a way for it to be done, I don’t need the entire answer just suggestions, please.

P.S. Also I looked at all the pages on interfaces in Java, haven’t found anything helpful.

  • 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-31T08:10:29+00:00Added an answer on May 31, 2026 at 8:10 am

    I got it.

    class interface operations { public int doSomething(int a, int b) {} }
    
    public class test {
        public static void main(...) {
        operations[] ops = new operations[4];
        ops[0] = new operations() {public int doSomething(int a, int b) {return a-b*a;}};
    ...etc.
    }
    

    Answer: use anonymous classes.

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

Sidebar

Related Questions

I've been programming Java for 2 years now, and I have encountered a problem
I'm not new to Java programming in any way, but this problem is unheard
I am stuck with a programming problem: I have two Java projects, in my
I am new to Java programming / Android development - but I have been
Hey everyone! I am new to blackberry programming although I do have some java
I'm a beginner in Java programming language, and I have a problem that I
I have this programming problem, I'm using C#. I want to check if varA
I have a question about a programming problem from the book Cracking The Code
I have recently started programming in WPF and bumped into the following problem. I
I have one problem with android calendar programming, so If there are 2 months

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.