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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:26:11+00:00 2026-05-24T04:26:11+00:00

I have an Interface and two Classes which are implementing the Interface. public interface

  • 0

I have an Interface and two Classes which are implementing the Interface.

public interface MyInterface {
    public void firstMethod();  
    public int secondMethod();
}

public class MyClass1 implements MyInterface  {
    public void firstMethod() {}
}

public class MyClass2 implements MyInterface  {
    public void firstMethod() {}
    public int secondMethod() {}
}

The class MyClass1 is telling me to Add unimplemented methods, because the secondMethod is not implemented, OK I will do that. But the problem is that I don’t need this method in MyClass1.

In your opinion what is the best thing to do?

  1. Add the unimplemented method with something like return 0
  2. There is another way to fix this if I don’t want to implement it.
  • 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-24T04:26:12+00:00Added an answer on May 24, 2026 at 4:26 am

    You should do one of the following:

    1. Break up the interface into smaller pieces and compose as needed. This is the preferred approach, especially if you control MyInterface.

    2. Return the most sensible default value that you can.

    3. Throw an UnsupportedOperationException.


    Here’s a more illustrative example. Let’s say your interface looks like this:

    public interface Driveable {
      public Position accelerate(Vector v, Time t);
      public String getVehicleIdentificationNumber();
    }
    

    If your MyClass1 is actually a Boat and doesn’t have a vehicle identification number, then it doesn’t make sense for you to implement this. In fact, it’s actually wrong. Other clients expect you to have this value, but you can’t give it to them.

    It would be better to chunk and compose the interfaces into smaller pieces, using the right slice as necessary. For example, you might instead write this:

    public interface Driveable {
      public Position accelerate(Vector v, Time t);
    }
    
    public interface Vehicle extends Driveable {
      public String getVehicleIdentificationNumber();
    }
    
    public class Boat implements Driveable { ... }
    public class Car implements Vehicle { ... }
    

    This is the best approach since it segments the responsibilities exactly as needed across the interfaces.

    If it really was important in your domain for all Driveables to have a vehicle identification number, and this is just a special case where the vehicle identification number is unknown or not available, then you can provide a default implementation:

    public String getVehicleIdentificationNumber() {
      return "";
    }
    

    If it would be wrong in your domain to return a vehicle identification number at all, then you should throw an exception:

    public String getVehicleIdentificationNumber() {
      throw new UnsupportedOperationException("vehicle identification
        number not supported on boats");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two related classes which share a common interface and are both stored
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
I have an abstract base class which acts as an interface. I have two
Have a interface class abc { public: virtual int foo() = 0; ... }
Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {
I have two interfaces like these: public interface IMyInterface1 { string prop1 { get;
I have a machine with VmWare installed which added two extra network interfaces. The
If I have interface IFoo, and have several classes that implement it, what is
I'm currently occupying myself with implementing a fluent interface for an existing technology, which
I have an interface ISerializeDeserialize defined and some classes inheriting the generic interface. 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.