Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instantiating a sub-class

 
Larry Frissell
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yes, there is a technical issue but it resolves to keeping track of where everything is and what it does on a large project.

Keep going, good so far.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Larry.

Yes, there is a technical reason. If you declare it as Shape then you only use the methods and fields inherited from Shape (inherited unchanged or overridden). That means you can write . . .

Shape c = new Circle(1.2);
Shape s = new Square(3.4);
Shape t = new Triangle(5, 6, 7);
double a1 = c.getArea();
double a2 = s.getArea();
double a3 = t.getArea();

. . . secure in the knowledge that all the subclasses of Shape will have their own getArea() method, even though it may differ. [We call this polymorphism.]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Larry Frissell
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!!! I plugged in some methods and I can see now how that all works. Thanks all.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darren Brooks
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have done better to start a new thread rather than opening an old thread; look at this
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
  • Object
  • Date
  • Timestamp
  • Serializable
  • Cloneable
  • Comparable<Date>
  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic