Results 1 to 4 of 4
- 03-28-2011, 04:40 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
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.
And then in a different class file.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); } }
I hope this all makes sense, please let me know if you can help.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(); }
Thanks
Mungo
- 03-28-2011, 04:55 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,447
- Rep Power
- 16
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.
- 03-28-2011, 11:20 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Is there any way of doing what I'm trying to do then?
- 03-29-2011, 08:29 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,447
- Rep Power
- 16
Similar Threads
-
Help with creating new class as a subclass in Java
By Monkeeboy in forum New To JavaReplies: 3Last Post: 01-22-2011, 03:06 PM -
Inheriting two kinds of Lists into one Class
By rsiddharth in forum New To JavaReplies: 22Last Post: 12-26-2010, 03:39 PM -
Custom Class, Wrapper/Subclass?
By Moncleared in forum Advanced JavaReplies: 3Last Post: 02-09-2009, 11:08 PM -
subclass vs inner class
By bugger in forum New To JavaReplies: 1Last Post: 01-13-2008, 07:31 PM -
which class is superclass and subclass?
By java_fun2007 in forum New To JavaReplies: 0Last Post: 12-11-2007, 08:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks