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
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    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
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: instance of an object

    Flexibility.

    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
    Location
    Germany
    Posts
    787
    Rep Power
    5

    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 online now Moderator
    Join Date
    Apr 2009
    Posts
    11,826
    Rep Power
    19

    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-13-2013, 11:00 PM
  2. object and instance
    By anurag in forum New To Java
    Replies: 1
    Last Post: 05-14-2010, 08:51 PM
  3. Instance of GNUPlot object of the package JavaPlot
    By ‹bermenschen in forum Advanced Java
    Replies: 2
    Last Post: 03-30-2010, 06: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, 09: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, 06: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
  •