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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:00:59+00:00 2026-05-31T09:00:59+00:00

I have a class defined by an interface public interface Test { void testMethod();

  • 0

I have a class defined by an interface

public interface Test {
    void testMethod();
}

Test test = new TestImpl();

public class TestImpl implements Test {
    @Override
    public void testMethod() {
         //Nothing to do here
    }

    public void anotherMethod() {
        //I am adding this method in the implementation only.
    }
}

How can I call anotherMethod?

test.anotherMethod(); //Does not work.

I want to be able to define a few methods in the implementation only because in my production code, the Test interface covers a pretty broad spectrum of classes and is implemented by multiple classes. I use methods defined in the implementation to set dependencies that aren’t covered by the DI framework in my unit testing so the methods change from implementation to implementation.

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

    The problem is with the following line:

    Test test = new TestImpl();
    

    This tells the compiler to forget that the new object is a TestImpl and treat it as a plain old Test. As you know, Test does not have anotherMethod().

    What you did is called “upcasting” (casting an object to a more general type). As another poster has said, you can fix your problem by not upcasting:

    TestImpl test = new TestImpl();
    

    If you’re sure that a Test object is really a TestImpl, you can downcast it (tell the compiler it is a more specific type):

    Test test = new TestImpl();
    :
    ((TestImpl) test).anotherMethod();
    

    This is generally a bad idea, however, since it can cause ClassCastException. Work with the compiler, not against it.

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

Sidebar

Related Questions

I have defined the following Unit-Test: [TestMethod] //@Test for the Java crowd public void
I have an interface and a class defined as below public interface IShape {
I have a functional AdjacencyListGraph class that adheres to a defined interface GraphStructure. In
I have the interface defined as below and is implemented by a single class
I have the following interface defined to expose a .NET class to COM: [InterfaceType(ComInterfaceType.InterfaceIsDual)]
I have defined an interface in C++, i.e. a class containing only pure virtual
I have a class defined as public class viewGroups extends ListActivity Somewhere in the
I have a class defined as: class Obj { public: int width, height; Obj(int
I have this class class Validator implements iValidation{ protected $_fields, $_errors; public function __construct($fields){
I have a class defined as follows interface ITest { List<T> Find<T>(int i); }

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.