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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:00:17+00:00 2026-05-12T12:00:17+00:00

I keep hearing the statement on most programming related sites: Program to an interface

  • 0

I keep hearing the statement on most programming related sites:

Program to an interface and not to an Implementation

However I don’t understand the implications?
Examples would help.

EDIT: I have received a lot of good answers even so could you’ll supplement it with some snippets of code for a better understanding of the subject. Thanks!

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

    You are probably looking for something like this:

    public static void main(String... args) {
      // do this - declare the variable to be of type Set, which is an interface
      Set buddies = new HashSet();
    
      // don't do this - you declare the variable to have a fixed type
      HashSet buddies2 = new HashSet();
    }
    

    Why is it considered good to do it the first way? Let’s say later on you decide you need to use a different data structure, say a LinkedHashSet, in order to take advantage of the LinkedHashSet’s functionality. The code has to be changed like so:

    public static void main(String... args) {
      // do this - declare the variable to be of type Set, which is an interface
      Set buddies = new LinkedHashSet();  // <- change the constructor call
    
      // don't do this - you declare the variable to have a fixed type
      // this you have to change both the variable type and the constructor call
      // HashSet buddies2 = new HashSet();  // old version
      LinkedHashSet buddies2 = new LinkedHashSet();
     }
    

    This doesn’t seem so bad, right? But what if you wrote getters the same way?

    public HashSet getBuddies() {
      return buddies;
    }
    

    This would have to be changed, too!

    public LinkedHashSet getBuddies() {
      return buddies;
    }
    

    Hopefully you see, even with a small program like this you have far-reaching implications on what you declare the type of the variable to be. With objects going back and forth so much it definitely helps make the program easier to code and maintain if you just rely on a variable being declared as an interface, not as a specific implementation of that interface (in this case, declare it to be a Set, not a LinkedHashSet or whatever). It can be just this:

    public Set getBuddies() {
      return buddies;
    }
    

    There’s another benefit too, in that (well at least for me) the difference helps me design a program better. But hopefully my examples give you some idea… hope it helps.

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

Sidebar

Ask A Question

Stats

  • Questions 227k
  • Answers 227k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use _atoi64, _strtoi64 functions in stdlib.h May 13, 2026 at 1:25 am
  • Editorial Team
    Editorial Team added an answer There is no direct way to position a Window from… May 13, 2026 at 1:25 am
  • Editorial Team
    Editorial Team added an answer I've never actually seen anyone combine XmlWriter objects this way… May 13, 2026 at 1:25 am

Related Questions

Since I have started using this site, I keep hearing about the Boost library.
What is this SWX data format that I keep hearing about? The Wikipedia article
If a Windows application has the IMAGE_FILE_LARGE_ADDRESS_AWARE set in the image header (via the
I keep hearing how Test Driven Development is an unfortunate name for the technique

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.