Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2006

Tip: Looking for answers? Try searching our database.

Array to subclass

Thread view: 
Mike - 29 Jul 2006 05:07 GMT
I am new to this and i don't understand how to transfer my arrays to a
subclass, alter the data and then send it back to the superclass. I
don't want to jump right in, I do some manipulating on the arrays in
the superclass using several methods. I want to pick the point in the
supercalss whent it sends to the subclass and I would like the altyered
data to drop back to the superclass right after the call to subclass.

My superclass: Inventory is passing data to my subclass: ProductXtras.

My supercalss has the following arrays which U want to send to the
subclass and retrieve them after I make changes to them in the
subclass.

     final int Array_Length = 50;
// Constant array size
     String DVDID[] = new String[Array_Length];            // DVD
identification code
     String DVDScreen[] = new String[Array_Length];     // Screen
format 4:3 or 16:9
     String DVDName[] = new String[Array_Length];       // DVD title
     Double DVDQty[] = new Double[Array_Length];       // Number of
DVDs per title
     Double DVDEach[] = new Double[Array_Length];     // Price of each
DVD
     Double DVDRestock[] = new Double[Array_Length]; // Restock Price
of each DVD
     Double DVDTotal[] = new Double[Array_Length];      // Total value
of DVDs of 1 title
     int index;
     // Number of array elements used
Bart Cremers - 29 Jul 2006 08:19 GMT
> I am new to this and i don't understand how to transfer my arrays to a
> subclass, alter the data and then send it back to the superclass. I
> don't want to jump right in, I do some manipulating on the arrays in
> the superclass using several methods. I want to pick the point in the
> supercalss whent it sends to the subclass and I would like the altyered
> data to drop back to the superclass right after the call to subclass.

I'm not sure how the classes are structured, but it's easy to do this
if your superclass is abstract.

public abstract class Super {

   public void process(Object[] array) {
       // Do a lot of work on the array here
       subProcess(array);
       // Do even more processing here
   }

   protected abstract void subProcess(Object[] array);
}

public class Sub  extends Super {
   protected abstract void subProcess(Object[] array) {
       // Do sub processing here
   }
}

The abstract makes sure the Sub class has to implement a certain method
for it to compile. You can easily create different subclasses which
perform different processing:

public class OtherSub  extends Super {
   protected abstract void subProcess(Object[] array) {
       // Do other sub processing here
   }
}

public class Tester {
   public static void main(String[] args) {
       String[] myArray = new String[] { "a", "b", "c"};
       Super one = new Sub();
       Super two = new OtherSub();

       one.process(myArray.clone());
       two.process(myArray.clone());
   }
}

I hope the examples show the power of using an abstract superclass.

Regards,

Bart
Greg R. Broderick - 29 Jul 2006 15:15 GMT
"Mike" <mparham1@austin.rr.com> wrote in news:1154146031.584543.305470
@i3g2000cwc.googlegroups.com:

>       final int Array_Length = 50;
> // Constant array size
[quoted text clipped - 13 lines]
>       int index;
>       // Number of array elements used

How about turning each of these arrays into an object, e.g.

public class DVD
{
       private string name;
        private long quantity;
       private BigDecimal price;
       private BigDecimal restock;

       // add getter and setter methods as appropriate
}

then you could have a single array (or better, use a List - a list can grow
or shrink) of these objects to pass back and forth.

DVD dvds[] = new DVD[ARRAY_LENGTH];

or

List dvds = new List();

Cheers
GRB

Signature

---------------------------------------------------------------------
Greg R. Broderick            gregb.usenet200606@blackholio.dyndns.org

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------

Hendrik Maryns - 31 Jul 2006 12:00 GMT
Mike schreef:
> I am new to this and i don't understand how to transfer my arrays to a
> subclass, alter the data and then send it back to the superclass. I
[quoted text clipped - 4 lines]
>
> My superclass: Inventory is passing data to my subclass: ProductXtras.

Is ProductXtras a special case of Inventory?  I don’t think so.  Then do
not make it inherit from it.  Rather pass the arrays explicitly to an
instance of it.

H.

- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.