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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:07:16+00:00 2026-06-11T19:07:16+00:00

In Java when you add a new method to an interface, you break all

  • 0

In Java when you add a new method to an interface, you break all your clients. When you have an abstract class, you can add a new method and provide a default implementation in it. All the clients will continue to work.

I wonder why the interface is designed this way?

All the old methods are still there, so seems like there is no backward compatibility issue. (Of course there need to be certain exceptions, but I think enabling to add new methods to java interfaces without breaking the clients could be really good idea…)

I’d appreciate your comments.

  • 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-11T19:07:17+00:00Added an answer on June 11, 2026 at 7:07 pm

    There are a few possible breaks I can see

    • you assume that clients will use a new overloaded method, but they don’t because the code hasn’t been recompiled.
    • you add a method which the client already had which does something different.
    • you add a method which means their subclasses break when recompiled. IMHO This is desirable.
    • you change the return type, method name or parameters types which will cause an Error at runtime.
    • you swap parameters of the same type. This is possibly the worst and most subtle bug. 😉

    IMHO, It’s the subtle problems which are more likely to cause you grief. However, I wouldn’t assume that simply adding a method will break the code and I wouldn’t assume that if a client’s code isn’t getting runtime error means they are using the latest version of anything. 😉


    If I compile this code

    public interface MyInterface {
        void method1();
    
        // void method2();
    }
    
    public class Main implements MyInterface {
        @Override
        public void method1() {
            System.out.println("method1 called");
        }
    
        public static void main(String... args) {
            new Main().method1();
        }
    }
    

    it prints

    method1 called
    

    I then uncomment method2() and recompile just the interface. This means the interface has a method the Main doesn’t implement. Yet when I run it without recompiling Main I get

    method1 called
    

    If I have

    public class Main implements MyInterface {    
        public void method1() {
            System.out.println("method1 called");
        }
    
        public void method2() {
            System.out.println("method2 called");
        }
    
        public static void main(String... args) {
            new Main().method1();
        }
    }
    

    and I run with // method2() commented out, I don’t have a problem.

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

Sidebar

Related Questions

Can I add more permit to a semaphore in Java? Semaphore s = new
I have the below java code: List<SomePojo> list = new ArrayList<SomePojo>(); //add 100 SomePojo
I have a Java interface in an API that contains a single method: public
Let's say I have an interface in Java: interface I { void add(I foo);
I am very new to Java, I want to add a PNG image to
If we add event listener to a button in java like this : btn.setOnClickListener(new
Can I add Java portlet in sharepoint 2007? If yes, can some please explain
I have a problem in Java: I have an interface: public interface I extends
import java.util.*; public class Test { static List<Integer> list = new LinkedList<Integer>(); public static
I have Foo.java , which is interface. And lots of classes that implement 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.