0

Generally, when a new object is created in Java it follows the idiom:

Object obj = new Object();

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:

Stack s1 = new Stack();

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

Object s2 = new Stack(); //This

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!

heez
  • 2,029
  • 3
  • 27
  • 39
  • It has access to all of `Object`'s methods but executes the implementation of those methods that `Stack` provides.. This is a classic java (or any object oriented language) best practice; code to an interface not an implementation. (or in this case a superclass) – takendarkk Feb 05 '15 at 05:35
  • 1
    This question is definitely __not a duplicate__ of the one cited. – user949300 Feb 05 '15 at 05:37
  • The right side of OP's examples are __not an interface__. Thus, how can this question be about "coding to an interface?" – user949300 Feb 05 '15 at 06:45
  • @user The term _interface_ in the expression _programming to an interface_ isn't used to represent Java's interface types. It's used to describe a contract that we can expect an object to fulfill. It just so happens that Java interfaces provide that, but so do class types (and their subtypes). – Sotirios Delimanolis Feb 05 '15 at 07:20
  • @SotiriosDelimanolis The question you cited actually has clarified a lot for me, and I guess my question is (conceptually) a subset of it. However, I can only see the benefit of this practice when you actually have an `interface` or if you have a custom `class` that extends methods to its subclasses. So for my question above, you would only declare `Object s2 = new Stack()` if `s2` only needed the methods provided in `Object()`, but for performance (or by requirement) you want to treat it as a stack at runtime? – heez Feb 05 '15 at 18:29
  • There's no performance gain. It's good programming practice. Always be the most generic you can be. If you don't need any `Stack` methods, use its parent type to hold the reference to the object. – Sotirios Delimanolis Feb 06 '15 at 15:46
  • Very cool! Got it. Thank you, I really appreciate it. – heez Feb 06 '15 at 18:06
  • Also, if it's not something you're already doing, it's important to distinguish between a variable, an object, and a reference value. A variable (of a reference types) holds a reference value. A reference value references an object (it's more or less a pointer to an object). And an object is a data structure that has state and behavior. – Sotirios Delimanolis Feb 06 '15 at 19:30

0 Answers0