Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java - protected members accessed in derived class using base class instance

I created instance of base class in derived class and tried to access protected members.

I can directly access protected members in a derived class without instantiating the base class.

Base class:

package com.core;

public class MyCollection {

      protected Integer intg;
}

A derived class in the same package -

package com.core;

public class MyCollection3 extends MyCollection { 

 public void test(){

  MyCollection mc = new MyCollection();
  mc.intg=1; // Works
 }
}

A derived class in a different package -

package secondary;

import com.core.MyCollection;

public class MyCollection2 extends MyCollection{ 

 public void test(){
  MyCollection mc = new MyCollection();
  mc.intg = 1; //!!! compile time error - change visibility of "intg" to protected
 }
}

How it is possible to access a protected member of a base class in a derived class using instance of base class when derived class is also in same package but not when derived class is in different package?

If I mark protected member as "static" then I am able to access protected member of base class using instance of base class in a derived class which resides in a different package.

like image 582
Tarun Avatar asked Jun 30 '10 11:06

Tarun


People also ask

Can base class access protected members of derived class?

A class can only access protected members of instances of this class or a derived class. It cannot access protected members of instances of a parent class or cousin class. In your case, the Derived class can only access the b protected member of Derived instances, not that of Base instances.

Can we access protected member derived class in Java?

Protected Access Modifier - Protected Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. The protected access modifier cannot be applied to class and interfaces.

How do you use a protected variable in a derived class?

The code snippet for this is given as follows. class Base { protected : int num = 7; }; class Derived : public Base { public : void func() { cout << "The value of num is: " <<< num; } }; In the function main(), the object obj of class Derived is created. Then the function func() is called.

How are protected members of a base class accessed in the derived class when inherited privately in C++? Privately publicly?

If a class is derived privately from a base class, all protected base class members become private members of the derived class. Class A contains one protected data member, an integer i . Because B derives from A , the members of B have access to the protected member of A .


1 Answers

You're right that you can't do this. The reason why you can't access the field, is that you're not in the same package as the class, nor are you accessing an inherited member of the same class.

The last point is the critical one - if you'd written

MyCollection2 mc = new MyCollection2();
mc.intg = 1;

then this would work, as you're changing a protected member of your own class (which is present in that class through inheritance). However, in your case you're trying to change a protected member of a different class in a different package. Thus it should come as no surprise that you're denied access.

like image 111
Andrzej Doyle Avatar answered Sep 22 '22 04:09

Andrzej Doyle