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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:19:59+00:00 2026-05-27T21:19:59+00:00

So in java the way to initialise an interface, such as a collection or

  • 0

So in java the way to initialise an interface, such as a collection or list, is to create an instance of a class that implements it, eg:

Collection<Object> moo = new ArrayList();

If I wanted to specify an implementation at a later time in my code, I was thinking of doing this by creating another class like:

class ListList extends ArrayList{

}

and then initialise the variable with

Collection<Object> moo = new ListList();

And then all that’s required if I want to change the implementation later on is to change what ListList extends.

So, here’s the question.. is there a better way of doing this (I still feel as though I’m inexperienced with this type of thing).

  • 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-27T21:19:59+00:00Added an answer on May 27, 2026 at 9:19 pm

    is there a better way of doing this

    Yes: use a factory method:

    public static Collection<Object> createCollection() {
        return new ArrayList<Object>(); // change this later, if need be
    }
    

    Then, invoke the factory rather than instantiating:

    Collection<Object> moo = createCollection();
    

    Your suggestion of using a “dummy” subclass might appear attractive, but such abuses of inheritance invariably lead to pain and suffering later on. You really don’t want to do that.

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

Sidebar

Related Questions

Is it completely against the Java way to create struct like objects? class SomeData1
Is there any way to initialize anonymous inner class in Java? For example: new
I'm trying to initialize an instance of class consoleDroid with consoleDroid console = new
What's the best way to lazily initialize a collection, I'm specifically looking at Java.
In Java is there any way of writing a loop so that you can
I am trying to find a Clojure-idiomatic way to initialize a Java object. I
IDsmCore.java (interface class) public interface IDsmCore { public void Initialize( String path, String fileName
I recently read that Java now sports initialisation blocks like the following: class C
Working in C# and Java, I've seen basically one way everybody initializes singletons: static
I need a Java way to find a running Win process from which 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.