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

  • Home
  • SEARCH
  • 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 7728023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:36:43+00:00 2026-06-01T05:36:43+00:00

I have interface Foo public interface Foo { public void test(); } Class FooChild

  • 0

I have interface Foo

public interface Foo {
    public void test();
}

Class FooChild implements interface Foo.

public class FooChild implements Foo {

    @Override
    public void test() {...}

    public void test2() {...}
}

I have to call function test2 in somewhere in the code.

Foo f = new FooChild();
((FooChild) f).test2();

I really hate casting an interface to a class.

So I added another interface Bar.

public interface Bar extends Foo {
    public void test2();
}

public class FooChild implements Bar {

    @Override
    public void test() {...}

    @Override
    public void test2() {...}
}

Now I call test2() with casting to an interface, not a class.

Foo f = new FooChild();
((Bar) f).test2();

I may add function test2 to the interface Foo,
but there are too many classes that implement Foo.

If I add the function, I have to add codes to all the classes that implement Foo.

Although I can do that above, I actually don’t have permission to edit Foo.

Will there be any fancier way to solve this problem? (hopefully without casting)

EDIT:
f is passed as a method parameter.

As Jon said, I should have be more specific.
Sorry for the confusion.

public void doSomething(Foo f) {
...
}
  • 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-01T05:36:44+00:00Added an answer on June 1, 2026 at 5:36 am

    If you know that the variable value really has to be of type FooChild, just declare it that way:

    FooChild f = new FooChild();
    f.test2();
    

    If there’s some reason why that won’t work for you, it’s something you haven’t told us…

    EDIT: Okay, so apparently it’s really a method parameter:

    public void doSomething(Foo f) {
        ((FooChild) f).test2();
    }
    

    … so no, there’s nothing you can do to avoid the cast. The cast indicates that there’s a requirement on the value beyond what the declared variable type already guarantees – in this case, you want to say “It’s got to actually be a FooChild, not just any implementation of Foo.” Casts are the way of expressing that, if you can’t change the parameter.

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

Sidebar

Related Questions

Suppose I have: interface Foo { void doStuff(); } class FooImpl implements Foo {
I have this type of class hierarchy: public interface IA{} public interface IB{void Foo();}
I have an interface: public interface IMech { } and a class that implements
Suppose I have these interfaces: public interface I1 { void foo(); } public interface
public interface Foo { } public class SpecificFoo implements Foo { } public interface
I have a GWT serializable class, lets call it Foo. Foo implements IsSerializable, has
Assume that I have this piece of code: @interface Foo : NSObject { Bar
Let's say I have a method public void Foo(string bar) that the caller should
The following code: struct interface_base { virtual void foo() = 0; }; struct interface
Suppose I have this interface public interface IFoo { ///<summary> /// Foo method ///</summary>

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.