aspose file tools*
The moose likes Beginning Java and the fly likes Instantiating a sub-class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instantiating a sub-class" Watch "Instantiating a sub-class" New topic
Author

Instantiating a sub-class

Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
    
    2
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

Joined: Sep 17, 2006
Posts: 1282
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Jul 11, 2001
Posts: 14112
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
Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
    
    2
Thank you very much!!! I plugged in some methods and I can see now how that all works. Thanks all.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
You're welcome.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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
Darren Brooks
Ranch Hand

Joined: Dec 29, 2005
Posts: 31
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
You would have done better to start a new thread rather than opening an old thread; look at this
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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>
  •  
    Consider Paul's rocket mass heater.
     
    subject: Instantiating a sub-class
     
    Similar Threads
    Restriction on parent class
    Class cast exception
    Regarding Polymorphism
    Type problem
    Help fast plz!!!!!!