Alon
Alon

Reputation: 2929

Java inheritance class

I have main class:

public class MainClass{
    private int firstVal;
    private int secVal;

    public MainClass(int firstVal,int secVal){
      this.firstVal=firstVal;
      this.secVal=secVal;
    }
}

and second class that extend this class

 public class SecClass extends MainClass{
     public SecClass(int firstVal,int secVal){
         super(firstVal,secVal)
     }
 }

I want to know How can I use the values firstVal and secVal in the SecClass? super.firstVal ?? or that I have to defind the values in agian?

Thanks

Upvotes: 0

Views: 65

Answers (5)

betteroutthanin
betteroutthanin

Reputation: 7606

Here is a table about the scope of public, private and protected, from where you'll understand why private variable cannot be used in the child class, (from this post)

Modifier    | Class | Package | Subclass | World
————————————+———————+—————————+——————————+———————
public      |  ✔    |    ✔    |    ✔     |   ✔
————————————+———————+—————————+——————————+———————
protected   |  ✔    |    ✔    |    ✔     |   ✘
————————————+———————+—————————+——————————+———————
no modifier |  ✔    |    ✔    |    ✘     |   ✘
————————————+———————+—————————+——————————+———————
private     |  ✔    |    ✘    |    ✘     |   ✘

Upvotes: 1

ps-aux
ps-aux

Reputation: 12174

If they are in the same package, just remove private access modifier. If not, you need to add protected or public (very bad idea). In general you should not expose your fields to the sub-classes like this as you cannot know what they will do with them.

Upvotes: 0

Joel
Joel

Reputation: 4772

You could do this with getters and setters. But if you believe the subclass should be able to access them, then declare firstVal and secVal protected. Then you can access them directly in the subclass:

subclassInstance.firstVal
subclassInstance.secVal

Private variables are not allowed to be accessed by anyone or any subclass for that matter. Protected variables are similar except subclasses may access them. You can still call the super() constructor; it is good practice to let parent constructors initialize their own varibles.

Upvotes: 0

Brian
Brian

Reputation: 7326

You need getters and setters in your MainClass. Then you can call super.getFirstVal()

Add these to your MainClass

public int getFirstVal() {
  return firstVal;
}

public int getSecVal() {
  return secVal;
}

Upvotes: 0

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85789

Just mark them as protected instead of private:

public class MainClass {
    protected int firstVal;
    protected int secVal;
    //rest of your class...
}

If you want to keep them as private, you can make getters and setters for this fields and mark these methods as public or protected, depending the visibility you want/need for them:

public class MainClass {
    private int firstVal;
    //its value can be retrieved by any class
    public int getFirstVal() {
        return this.firstVal;
    }
    //its value can only be modified by subclasses of MainClass
    protected void setFirstVal(int firstVal) {
        this.firstVal = firstVal;
    }
    //similar for the other field...
}

Upvotes: 3

Related Questions