1

Explanation - As you can see the below code is the super class in one package as an API, and child class in another package, now what I want to do is access the value of "level" variable in child class.

The problem is the super class doesn't have any getters for the below mentioned variable as it contains only setters so when I am trying to access the value using child class it always give "Debug" as value.

However while debugging in eclipse it shows the original runtime value. How can I achieve that

public class CustomLogger {

     protected String level = "Debug";
     protected String category;

}

public class DisplayLogger extends CustomLogger{

public void childMethod(){
    CustomLogger customLogger = someMethod(); // Return object at runtime
    customLogger.level; // give compiletime error as is protected
    System.out.println(this.level); // gives always "Debug" as output
}
}
Zahid
  • 59
  • 10
  • You can access it using "super.level". Also, the only way to access it from object is to make it public or create public getter – Oleksandr Riznyk Oct 11 '18 at 10:27
  • You example contains a syntax error - you need to assign this value to something. – Hulk Oct 11 '18 at 10:31
  • 2
    `super.level` (which is the same as `this.level` or just `level` here) is different from `customLogger.level`. @OleksandrRiznyk – Thilo Oct 11 '18 at 10:32
  • @Thilo of course different, I am just saying how protected members are used. If author wants to access level of specific object he should use getter – Oleksandr Riznyk Oct 11 '18 at 10:34
  • @OleksandrRiznyk Nonsense. You use getters when you decide to make the field private. If you make it protected, then of course it is visible in any subclass. That is whole point of *protected*. – GhostCat Oct 11 '18 at 10:48
  • @Hulk Ignore the syntax, the Idea is to access the variables value at runtime – Zahid Oct 11 '18 at 10:50

2 Answers2

0

You can do it by Reflection. Other option is to create public getters/setters.

B_Osipiuk
  • 888
  • 7
  • 16
  • 1
    The **essence** of protected is that it makes things available in subclasses, no matter what package they reside in. The OP has some other problem, and your suggestion to use reflection instead just adds to that confusion. – GhostCat Oct 11 '18 at 10:47
  • Not possible to change the super class – Zahid Oct 11 '18 at 10:55
0

Try this!

package A;
public class CustomLogger {
    protected String level = "Debug";
}

package B;
import A.CustomLogger;
public class DisplayLogger extends CustomLogger {
    public void childMethod(){
        CustomLogger customLogger = new CustomLogger();
        System.out.println(customLogger.level;);
    }
}
Karthik P
  • 21
  • 2
  • 12
  • @Karthik - The value of logger will still be "Debug". The question is object of CustomLogger is dynamic at runtime and therefor value is changed by that object, The superClass has Setter for the level variable however doesn't have getter – Zahid Oct 11 '18 at 10:53
  • Hi Zahid, I understood the question. The only way to access using dynamic object is to use getters and y the way the protected members is alwyas accessible directly ryt/ – Karthik P Oct 11 '18 at 11:56
  • @Kartik - Outside the package only the child class will be able to access the protected member directly however the value will always be the default or assigned one as you can typecast superclass to subclass so child class will not be able to access the runtime value of the superclass object. As I said the super class is a third party API, it is pointless to change implementation – Zahid Oct 12 '18 at 07:16