The Artima Developer Community
Sponsored Link

Java Answers Forum
simple question on object creation

2 replies on 1 page. Most recent reply: Mar 4, 2005 1:27 AM by sudhakar

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Guido

Posts: 38
Nickname: kiethb
Registered: May, 2002

simple question on object creation Posted: Mar 3, 2005 7:01 AM
Reply to this message Reply
Advertisement
I would like to know the perceived benefit of instantiating an object as the type of its superclass... i.e.

 
public class Shape{}

public class Circle extends Shape{}

public class DoIt{
public static void main(String[] args){

Shape c = new Circle();
}
}


Why not do Circle c = new Circle(); I realise you COULD do it either way, but I have noticed a lot lately in reading that just creating an object is done this way and would like to know if this minimizes or maximizes some functionality or just what the technical 'rule' that is being applied here.

thanks!


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: simple question on object creation Posted: Mar 3, 2005 11:37 PM
Reply to this message Reply
Actually, you loose some functionality.

But: If you don't need special methods, it can come in handy, espescially if you have more than one object.


Look at this example:

public class GeometricObject {
  public void move (double difX, double difY) {
    //move the object on the x/y grid.
  }
}
 
public class A extends GeometricObject{
  public void specialFunctionA() {
  //do something very important
  }
}
 
public class B extends GeometricObject{
  public void specialFunctionB() {
  //do something very important
  }
}
 
 
public class container() {
  public static void main(String[] args) {
    GeometricObject[] geos = {new A(), new A(), new B(), new B()};
     for (int i = 0; i < geos.length; i++)
       geos[i].move (5,5);
  //As you see, it does not matter, if it's A or B.
  //this is the same as if a graphical method requests a Component Object,
  // because to this method it does not matter if it's a Button or a Textfield or whatever.
  }
}

sudhakar

Posts: 1
Nickname: sudhu72
Registered: Feb, 2005

Re: simple question on object creation Posted: Mar 4, 2005 1:27 AM
Reply to this message Reply
The idea of giving a superclass a subclass handle is governed by design requirements. It does not seem to maximize nor minimize the performance. But it does impose some constraints, omitting that, could result in compile-time errors.
For instance if using the Shape variable 'c' you reference a method in Circle class (which doesn't exist in its superclass Shape), then it would be a compile-time error.
But inheritance using 'extends' is a good technique in imposing top-down vertical inheritance. If you need a group of classes to behave in a defined manner this helps in referencing the common non-overridden behavior from the parent.

Flat View: This topic has 2 replies on 1 page
Topic: Adding and searching the data Task ! Previous Topic   Next Topic Topic: WSAD migration question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use