Results 1 to 3 of 3
  1. #1
    pompeez is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default Class and SuperClass

    I have a super class: public static class StructClass(){} // this is empty

    Now a class is declared which is extends a Structclass:

    Java Code:
    public static class myStruct extends structClass {
          public DV v1 = new DV(..);
          public DV v2 = new DV(..);
     
          public myStruct(DV v1copy, DV v2copy) {
                 v1 = v1copy;
                 v2 = v2copy;
           }
    }

    DV is a class object I have created.

    Now I am declaring a myStruct variable:
    DV myS = new DV(4, "shared", (structClass)new myStruct(DV.Constant(1), DV.Constant(2)));

    This DV is different. The DV function is:

    Java Code:
    public DV(int vtype, String scope, structClass value) {
     
          ......
          DVStructVal = new StructClass();
          DVStructVal = value;
    }

    This is compiling fine. I need the super class to generalize the DV function for different classes like myStruct classes declared. But, this way I cannot access the class variable.

    I am trying: myS.DVStructVal.v1. I am getting the error that v1 does not exist. myS.v1 is also not working. Does this mean DVStructVal is just a structClass and not myStruct. How can I access v1 and v2?

    I need a generalized class. The DV class is needed. I am supposed to declare different classes like myStruct class and use the same DV class for them. Can you help me with that?

    Any help will be appreciated! Thanks!
    Kajari
    Last edited by Fubarable; 08-05-2009 at 02:12 AM. Reason: code tags added for readability

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Welcome to the forum!

    From my understanding of your post, I see a problem here:
    Now I am declaring a myStruct variable:
    DV myS = ....
    myS is most definitely not a myStruct variable but rather a DV variable. Perhaps you're declaring this wrong? Perhaps you want to do it like so:
    Java Code:
    myStruct myS = new myStruct(/*  etc...  */);
    If you did this, then myS.v1 would compile and make sense although it goes against the OOP principle of encapsulation.

    Also, I added code tags to make your code retain its formatting and thus be more readable. You might want to do this yourself next time you post code. It's easy to do: either highlight your pasted code and press the "CODE" button, or place the tag [code] above your code and [/code] below your code.

    Best of luck and hope this helps.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Unless these are inner classes then this can't be compiling, because "static" is not a valid keyword for a class declaration. As well as you're casing being all over the place. You really ought to cut and paste the actual code you're working on.

    Also there's no need to cast from "new MyStruct" to "StructClass" since it is already a StructClass.

    Onto the actual problem, what are the different things that DV is supposed to work with, and what common things do they do? What is DV supposed to do with the DV objects held in MyStruct? If there are always going to be DV objects in these different classes then they probably should be in the StructClass.

    All in all we have little information here to actually make an informed answer.

    ETA: You also don't need to be instantiating the DV variables in MyStruct at the top, since they get a value in the constructor it's a complete waste of time.

Similar Threads

  1. superclass and subclass
    By mr idiot in forum New To Java
    Replies: 19
    Last Post: 01-03-2009, 07:29 AM
  2. which class is superclass and subclass?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 08:55 PM
  3. SuperClass of an Object
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 02:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •