Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Tolls

Thread: instance of an object

  1. #1
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Rizal Province, Philippiines
    Rep Power

    Default instance of an object

    Hi I can't figure out whats the difference of an instance that his data type is another class/superclass like this:

    Example 1. List a = new ArrayList() versus ArrayList a = new ArrayList()
    Example 2. SuperclassA a = new subclassA() versus subclassA a = new subclassA

    Whats the difference and when should I use one of each?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

    Default Re: instance of an object


    If you use

    Java Code:
    SuperType a = new SubTypeA();
    Later you can easily change this to new SubTypeB() without having to change much code.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Rep Power

    Default Re: instance of an object

    I would prefer to show the difference with another example:
    Let`s say you use everywhere ArrayList instead of List, in methods too, perhaps even in several different classes, because you want to inject your list to another objects and so on. And after a few tests you realize that the ArrayList implementation is not the correct one and you want to replace it with the LinkedList as an example, then you have to change every line, every method signature and so on!

    right because you have to change only the line of the instantiation (see Fubarable) :
    public void foo(List<?> list){

    not so good, because you have to change the arraylist here and the line with the instantiation:
    public void foo(ArrayList<?> list){

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: instance of an object

    This is otherwise known as "coding to/against interfaces".
    Here's a (slightly old) article from Java World on the concepts.

    Newer advantages come int he area of testing, if you use something like Spring, whereby you can easily swap an implementation of your (say) database interaction interface for a dummy one so you can do unit tests on some code without actually requiring a database.
    Fubarable likes this.

Similar Threads

  1. difference between instance and object
    By JavaJunkie in forum New To Java
    Replies: 50
    Last Post: 05-14-2013, 12:00 AM
  2. object and instance
    By anurag in forum New To Java
    Replies: 1
    Last Post: 05-14-2010, 09:51 PM
  3. Instance of GNUPlot object of the package JavaPlot
    By ‹bermenschen in forum Advanced Java
    Replies: 2
    Last Post: 03-30-2010, 07:51 PM
  4. [SOLVED] printing address of current instance of object?
    By emceenugget in forum New To Java
    Replies: 1
    Last Post: 02-09-2009, 10:36 PM
  5. [SOLVED] Non-synchronized instance method of an Object
    By piyu.sha in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-06-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts