I am trying to understand if the following is just style or function. In the code below Shape is an abstract, which is extended by Circle. In some examples, the Circle class is instantiated with "Shape circle = new Circle(); " in other examples I see "Circle circle = new Circle();" Both seem to compile and work fine. Is this just style, or is there a technical reason one should be used over the other.
And the real beauty is that you can change the first line to, say,
Shape c = new Rectangle(1.2, 42);
and be sure that the rest of the code still compiles, without even looking at it!
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: May 16, 2008
Thank you very much!!! I plugged in some methods and I can see now how that all works. Thanks all.
There are benefits to it, but there is a cost that people seem to like to gloss over.
The cost is that you don't get direct access to the subclasses methods. Is this a big deal? Depends. Making use of polymorphisms is not always the best solution, understanding when and when it is not is the goal you should be working towards.
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
I am reading Beginning Java Game Programming 2nd ed. (by Jonathan S. Harbour), and the book shows a class which imports java.awt.Shape.
It then instantiates a Shape, like so:
private Shape shape;
Basically, the class allows you to set/get a Shape instance by setting it to the kind of Shape you want (e.g., Rectangle, Polygon, etc.). I understand that a Rectangle is a Shape, but isn't Shape an Interface? Why can this generic Shape object be used like this?
Joined: Oct 13, 2005
You would have done better to start a new thread rather than opening an old thread; look at this
Joined: Oct 13, 2005
You would have done better to start a new thread rather than opening an old thread; look at this FAQ; if you don't agree at least have a laugh at its title.
In Java you can call an object reference any type which is "above" the object in its inheritance hierarchy. You already know that the compiler adds "extends Object" to any class which doesn't have an "extends" already. So if you follow every object back, you will eventually come to "extends Object" and you can call any object of any type an Object. It always has all the methods in the Object class. If you make a class implement an interface, then it has all the methods of that interface; you can then tell the compiler you want a reference with the type called the same as that interface, and the compiler will take it happily. But you can only use the methods in that interface. So your classes can have several types. If you choose any class haphazardly from the API, you can see at the top left there is an inheritance tree. For example, java.sql.Timestamp has 2 superclasses and implements 3 interfaces. Depending what you want to do with it, you now have 6 options for what type you can call its references:
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com