inheritance question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: inheritance question

  1. #1
    Join Date
    Jul 2004
    Posts
    92

    inheritance question

    i have a class Bag that has local variables . . . .

    private int i=10;
    private int [] x;
    private int top_pos=0;

    class Bag has a get method for an array value, a set method for an array value, a toString override, etc.

    i want to make a class BagExt that EXTENDS the Bag class. BagExt will inherit all of Bag's methods, but will override one and maybe overload one (just trying to get familiar with how inheritance works).

    so i tried to do the following . . .

    public class BagExt extends Bag{

    public BagExt (){
    super();
    }

    public void add (int val){
    i += val;
    }

    public int show_i (){
    return i;
    }

    but when i try to compile, i get an error that "i" has private access in Bag. so do i have to declare similar variables of my own? and how is it that the default constructor will work? . . . it uses the Bag private varialbe of "int [] x".

    how do i get this to work?

    thanks
    crq

  2. #2
    Join Date
    Sep 2004
    Posts
    12
    Change the private declarations to protected. This will keep other classes from changing the variables, but allow classes that extend it to modify the variables. For example:
    Code:
    protected int i=10;
    protected int [] x;
    protected int top_pos=0;

  3. #3
    Join Date
    Jul 2004
    Posts
    92
    so if i change the modifiers to protected, each object of Bag and of SetExtBag will have it's own "i". am i thinking correctly about that? and changing the value of "i" in one object will not change the value of "i" in another object since they are not static.

    is that right?

    thanks
    crq

  4. #4
    Join Date
    Sep 2004
    Posts
    12
    You only need one set of variables. Changing in one class will affect the value in the other class. The code would be as follows:

    Bag.java
    Code:
    public class Bag {
      protected int i=10;
      protected int [] x;
      protected int top_pos=0;
    }
    BagExt.java
    Code:
    public class BagExt extends Bag {
      public void add (int val){
        i += val;
      }
      public int show_i (){
        return i;
      }
    }
    By executing the "add" function in BagExt, you will change the value in Bag. When you execute the "show_i" function, it will return the value of i from the Bag class.

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    public gives any class access to that variable
    private gives only the current class access
    protected gives all subclasses access
    and friendly (no specifier) gives access to all classes in the same package

    you declared a private i (har har) in your Bag, and then attempted to use it from BagExt. THough it exists in BagExt, BagExt may not acces sit, because only Bag can access it. There is nothing stopping you re-declaring it in BagExt, BUT the two will NOT be linked. THis is called variable shadowing, and it is NOT RECOMMENDED because it makes programs very very hard to understand.
    As noted, Bag and BagExt share i, so when you declare it as protected, if BagExt changes i, Bag feels the change too. There is only one variable called i, assigned to Bag, but accessible to BagExt.
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center