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!
No. Private variables are only accessible to the class members where it belongs.
Private field of a superclass can't be access (directly) by subclass. Protected fields on the other hand can be accessed directly by the subclass.
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. A nested class has access to all the private members of its enclosing class—both fields and methods.
We have used the getter and setter method to access the private variables. Here, the setter methods setAge() and setName() initializes the private variables. the getter methods getAge() and getName() returns the value of private variables.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With