0

i know that constructors are not inherited in java and we have to implicitly or explicitly call them and private instance variables only accessible within the class's that they are declared. assume that we have a superclass with one private instance variable and we initialize it with superclass constructor for example

       public class SuperClass

       {

            private int a;

           public SuperClass ( int a )
           {

             this.a = a;

           }
          .
          .
          .
       }

and our subclass is like this

    public class SubClass extends SuperClass

   {

       public int b;


      public SubClass ( int a, int b )
      {

         super( a );
         this.b = b;

      }
      .
      .
      .
    }

so here public SubClass ( int a, int b ) there is no problem even one of it's argument is private and belong to SuperClass??? if yes, how could it possible to access a private instance variable through another class???

2
  • If you notice none of the code you wrote other than this.a = a; actually touches the private a. Parameters aren't the same as the instance. Commented Apr 8, 2015 at 21:49
  • The variable "a" in SubClass is not the same as the instance variable in SuperClass, they have no relationship to each other Commented Apr 8, 2015 at 21:57

3 Answers 3

1

When you call super(a);, you're not accessing the superclass from the subclass persay. You're calling the superclass's constructor, which in turn accesses it's own variable. It's completely valid, and makes logical sense.

Also, all variables and functions of the superclass are inherited by the subclass, such that the subclass is able to access the variables of the superclass. Note that you can't access it directly if it's private, but that doesn't mean that the subclass doesn't inherit the field.

Consider the following classes:

public class A {
    private int a = 5;

    public int getA() {
        return a;
    }
}

and

public class B extends A {
    public int getnumber() {
        return this.getA();
    }
}

If I were to do this:

B x = new B();

then

System.out.println(x.a); //invalid

It would be invalid. You cannot directly access the a field inside the B object, but even if you can't see it, it still exists. We can validate that by doing this:

System.out.println(x.getnumber());

Which prints out 5.

Sign up to request clarification or add additional context in comments.

1 Comment

Inherited, but not accessible the way you think it is. I'll put an example into the answer.
0
public SubClass ( int a, int b )   {

     super( a );
     this.b = b;

  }

int a is a new variable, different from private int a decalred in SuperClass. When you call super(a), you are passing the value inside of the parameter variable int a to the superclass's constructor. This is then stored in private int a

Comments

0

so here public SubClass ( int a, int b ) there is no problem even one of it's argument is private and belong to SuperClass??? if yes, how could it possible to access a private instance variable through another class???

The scope of those parameters in that constructor is limited to that code block. int a, and int b are in no way related to class level variables, they just happen to be named the same.

You should probably make a public getter for the variable, but you could also simply declare the variable as "protected" (accessible to other classes in the same package, and by subclasses), or "public" - accessible by everything.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.