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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:55:27+00:00 2026-05-25T12:55:27+00:00

I have two programs. First one is without ‘Buffer’ interface and second is with

  • 0

I have two programs.
First one is without ‘Buffer’ interface and second is with ‘Buffer’ interface.
Both programs give me same output.
My question is.
why I need to create “Buffer” interface while I can get same output without interface?
Here is my both programs:

Without Interface:

public class MainClass {

    public static class producer {

        maintainBuffer bf;

        producer(maintainBuffer buffer) {
            this.bf = buffer;
        }

        public void setData() {
            for (int i = 0; i <= 10; i++) {
                bf.set(i);
            }
        }
    }

    public static class reader {

        maintainBuffer bf;

        reader(maintainBuffer buffer) {
            this.bf = buffer;
        }

        public void getData() {
            System.out.println("From Reader Class:-" + bf.get());

        }

        public void extraData() {
            System.out.println("Extra DATA");
        }
    }

    public static class maintainBuffer {

        int buffer = -1;

        public int get() {
            return buffer;
        }

        public void set(int a) {
            buffer += a;
        }
    }

    public static void main(String str[]) {
        maintainBuffer b = new maintainBuffer();
        producer p = new producer(b);
        p.setData();
        reader r = new reader(b);
        r.getData();
        r.extraData();
    }
}

With Interface

public class MainClass {

    public static class producer {

        Buffer bf;

        producer(Buffer buffer) {
            this.bf = buffer;
        }

        public void setData() {
            for (int i = 0; i <= 10; i++) {
                bf.set(i);
            }
        }
    }

    public static class reader {

       Buffer bf;

        reader(Buffer buffer) {
            this.bf = buffer;
        }

        public void getData() {
            System.out.println("From Reader Class:-" + bf.get());

        }

        public void extraData() {
            System.out.println("Extra DATA");
        }
    }


 public static interface Buffer{
       public int get();
       public void set(int a);
   }


  static class maintainBuffer implements Buffer{
      int buffer=-1;

        public int get() {
         return buffer;
        }

        public void set(int a) {
           buffer +=a;
        }
  }
    public static void main(String str[]) {
        Buffer b = new maintainBuffer();
        producer p = new producer(b);
        p.setData();
        reader r = new reader(b);
        r.getData();
        r.extraData();

    }
}
  • 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-25T12:55:27+00:00Added an answer on May 25, 2026 at 12:55 pm

    With an interface you can use other implementations later without changing all your code. E.g. if you use a List interface, this could be a LinkedList, an ArrayList, a CopyOnWriteArrayList or something else.

    You are making it clear, the details of the implementation are not important.

    On the other hand, I wouldn’t go around making an interface for every class. In most cases adding an interface later in the unlikely event you really need it isn’t always a difficult one.

    I would use an interface when you are want loose coupling e.g. when the caller is another module or used by another developer. If you have tight coupling like a package local implementation, I might not bother with an interface.

    In the above example, you might want to add a unit test to see what values are set or a developer might want to use a Buffer which logs which values are set. Using an interface makes this easier.

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

Sidebar

Related Questions

I have created two JSP programs as follows. First One: Addmulti.jsp <html> <head><title>Upload Excel
I have two programs one of which writes some entries into a file and
I have two (UNIX) programs A and B that read and write from stdin/stdout.
I have recently been put in charge of debugging two different programs which will
I would like a batch file to launch two separate programs then have the
I have two computers. One computer is an old XP and the other is
At school we have been using a bootstrap program to run stand-alone programs without
I have two window form applications written in C, one holds a struct consisting
I have two c++ programs that need to have a map type -> int
I have two programs A and B . I can't change the program A

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.