Results 1 to 4 of 4
  1. #1
    loki20884 is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Inheriting an inner class whilst using the variables from a subclass

    Hello,

    I'm having a problem at the moment where I'm attempting to inherit an inner class from the superclass. The problem I'm having is that I'm defining the variables that are in use from the subclass in the superclass but not initializing them. When I instantiate the innerclass from the subclass it just uses the uninitialized variables from the superclass rather than the initialized ones in the subclass and so I'm getting lots of null pointer exceptions. Is there an easy way to do this. Below is an example of the kind of thing I am trying to do.

    Java Code:
    class Parent {
    int var1;
    int var2;
    Vector vector;
    
    class ParentInnerClass {
    
    public ParentInnerClass{
    super();
    }
    
    private void useVariables() {
    for(int i; i<vector.size(); i++) {
    System.out.println(var1 + " " + var2);
       }
    }
    And then in a different class file.
    Java Code:
    class Child extends Parent {
    int var1 = 1;
    int var2 = 2;
    Vector vector = new Vector();
    vector.add(1);
    vector.add(1);
    ParentInnerClass parentInnerClass = new ParentInnerClass();
    parentInnerClass.useVariables();
    }
    I hope this all makes sense, please let me know if you can help.

    Thanks
    Mungo

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

    Default

    Olus points for usig code tags, but minus points because you have no indentation at all...rendering code tags a pointless exercise.

    Anyway, the attributes declared in your child class hide the attributes in the parent.
    They are different attributes.
    Your ParentInnerClass sees the attributes of Parent, not Child.

    Hiding attributes like this is generally Bad Code.

  3. #3
    loki20884 is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    Is there any way of doing what I'm trying to do then?

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

    Default

    Don't redeclare the attributes?

    Besides that, you'll probably want (for safer code) to declare the attributes as private and provide at least a protected setter for each, or possibly a constructor that takes in the values you want to give them.

Similar Threads

  1. Help with creating new class as a subclass in Java
    By Monkeeboy in forum New To Java
    Replies: 3
    Last Post: 01-22-2011, 04:06 PM
  2. Inheriting two kinds of Lists into one Class
    By rsiddharth in forum New To Java
    Replies: 22
    Last Post: 12-26-2010, 04:39 PM
  3. Custom Class, Wrapper/Subclass?
    By Moncleared in forum Advanced Java
    Replies: 3
    Last Post: 02-10-2009, 12:08 AM
  4. subclass vs inner class
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:31 PM
  5. which class is superclass and subclass?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 09:55 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
  •