简体   繁体   中英

Access a private variable from the superclass (JAVA)

Ok so I have studied java all semester and thought I had a clear understanding about inheritance and super/sub classes. Today we were given as assignment for making a superclass called enemy, with sub classes of different types of enemies. I did everything fine and all of my subclasses are working, but when I went back to read the guidelines we must follow, I found this sentence:

"All member variables of the super class must be private. Any access to a variable must be done through protected methods in the subclasses."

From what I have learned, this makes no sense to me. If a variable is private within the superclass, doesn't that disallow access even from a subclass? The last part that talks about protected methods in the subclasses also doesn't make any sense to me. How does this help and/or allow any access whatsoever to the super class?

From what I've learned about inheritance, Below is what i thought was true:

                Access Levels
 Modifier    Class  Package Subclass    World
 public        Y      Y        Y        Y
 protected     Y      Y        Y        N
 no modifier   Y      Y        N        N
 private       Y      N        N        N

If I'm understanding something wrong here please do explain, I don't want to confront the instructor about giving us faulty instructions, if I'm the one not understanding it correctly!

The part

Any access to an a variable must be done through protected methods in the sub classes.

... just means that the subclasses have to call protected methods that are defined in the superclass. Since these methods are protected they can be accessed by the subclasses.

First you would define a base class like this:

public class Base {

    private int x;  // field is private

    protected int getX() {  // define getter
        return x;
    }

    protected void setX(int x) {  // define setter
        this.x = x;
    }
}

Then you would use it in your child class like this:

class Child extends Base{

    void foo() {
        int x = getX(); // we can access the method since it is protected.
        setX(42);  // this works too.
    }
}

Probably the sentence is not worded correctly, from what I understand it makes perfect sense to me :
1. the superclass has private fields and protected methods to access them
2. the subclasses access the fields by using that methods.

Those are just the constraints of the assignment, not Java itself. You could give the superclass protected data members, and then access those directly from the subclass. However, the professor likely wishes to teach you about how a superclass can protect its data members from direct access by a subclass, and protected methods would be a way to do that.

When you inheritance other class, you cannot access your private attributes directly. So, if you have a class named "A" and other called "B", and make B extends A, B cannot access private attributes of A.

Think this like a protection. This way, you can write some attributes in class "A" that you dont want others classes access it through inheritance.

The "B" class can access only public, protected and default attributes directly in "A" class. But if you want to access a private attribute in "A" class for any reasons, you can write a method in "A" to return this attribute.

public class A{
    private int foo;
    public int getFoo(){
       return this.foo;
    }
}

public class B extends A{
    public void doSomething(){
       getFoo(); //return the private foo attribute of superclass
    }
}

You are right in your thinking that, literally speaking, you can't access a superclass's private field. The text of your assignment uses wording which is not 100% strict, but is 100% customary in Java parlance: the so-called "getter" methods, also called "accessor" methods, are seen as "accessing the field", even though, strictly speaking, they merely return the current value of the field—which is definitely not the same as giving access to the field itself. You just need to get used to this (and many more) conventions in the Java jargon.

package com.action.product;

public class ProductAction{
  public static String strCategoryName;

  public String getStrCategoryName() {
    return strCategoryName;
  }
  public void setStrCategoryName(String strCategoryName) {
    this.strCategoryName = strCategoryName;
  }

}

-----------------------------------------------------------------------

package com.DAO.product;

public class ProductDAO extends ProductAction {

   @SuppressWarnings("static-access")
   public String addnewProductValidation(){
       System.out.println("======through_name======="+super.strCategoryName);

       System.out.println("======through_getters======="+super.getStrCategoryName());

   }
}

Can initialize variables as static. No need to create object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM