• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java object declaration beyond the common idiom  RSS feed

 
Alex Haywood
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally, when a new object is created in Java it follows the idiom:


where the Object() constructor matches the object type Object.

But what if it doesn't? I understand from the Oracle Docs on creating objects and polymorphism that the constructor must be in that object's class or one of its subclasses. However, suppose we wanted to declare a new stack. My first instinct would be:


But I assume it's valid to do it this way, too:


Is there a difference here? What are we really saying about s2? I'm guessing s2 is simply an empty stack, but only has access to the Object class methods? I'm not sure why someone would ever do this, but I want to solidify my understanding of the Java hierarchy. Are there really any circumstances where someone would use a subclass's constructor when creating a new object?

Any and all informative responses will be appreciated. Thanks in advance!
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a very common way to instantiate an object where the object is declared with a super class:



Why would we do this? List is an interface and cannot be instantiated, but is defines the behavior or all Lists. You are now free to pick an implementation of List that fits your needs.

This doesn't make as much sense if you are the only one working on a private project, but what if it's a public API? Then anyone who uses the API you wrote can pick the implementation of List.

Also, if you are maintaining this code for some time, you may change your mind about which implementation to use. If you declare the object with the interface, you can change just the instantiation and you're done.
 
Alex Haywood
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute,

Wow! I think the light bulb just came on! This really hit home with me:
it defines the behavior for all Lists
.

Just a quick followup question to solidify my understanding--lets stick with your example. Suppose you declare

It appears myList can perform any methods defined in List, but cannot use any methods unique to ArrayLists? So why does it matter which implementation you choose, since you can only use methods defined in List? My guess is that an ArrayList is treated differently at runtime than, say, a LinkedList. Depending on your problem or requirements, it may be more beneficial to use ArrayList vs LinkedList despite only being able to use the methods in List?

Thanks!
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You have been unlucky there, choosing Stack, because Stack is notorious for the mistake made in its creation. Rather than there being a Stack interface there is only a Stack class, so
Stack myStack = new ArrayStack();
will never work.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that an ArrayList is treated differently at runtime than, say, a LinkedList. Depending on your problem or requirements, it may be more beneficial to use ArrayList vs LinkedList despite only being able to use the methods in List?


This is correct. ArrayList merely implements the methods (behavior) of List, just as LinkedList does. In general, ArrayList is a good choice -- it's fast at retrieval -- but LinkedList is sometimes better when there is a lot of insertions.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to look at it is that a List is a sort of black box. Imagine it has add() and get() written against buttons and you push the buttons. Then the insides of the black box work out how to implement the buttons.

Come to think of it, every object is rather like that.
 
Junilu Lacar
Sheriff
Posts: 11489
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to get really technical about it, look up the Liskov Substitution Principle and preferring to program to an interface, not a concrete implementation.

Take the steering wheel of a car as a real-world example: When you get in the driver's seat of a car, you will most likely find a wheel. From your experience with every other car you've driven, you probably know how to use this wheel to steer the car in the direction you want it to go. You don't really care if the "implementation" of the steering mechanism is rack & pinion or recirculating ball or two elves pulling each others' ears; as the driver, your only concern is the wheel (the "interface") and the behaviors that result when you manipulate it.

(Sorry if this is just rehashing what has already been said in previous responses -- I really just wanted to write that "two elves" part )
 
Alex Haywood
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much guys. I really appreciate the responses.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

But surely it is two elephants in the steering box, Junilu?
 
Junilu Lacar
Sheriff
Posts: 11489
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:But surely it is two elephants in the steering box, Junilu?

Only if you're driving a Volkswagen
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only an old VW with the engine at the back, surely.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!