3

I'm a non-programmer with a very minimal coding exposure, but I'd like to modify an existing code base. I've greatly simplified the code I'm working with below. Please let me know if I can provide any further information or if this makes no sense at all! Vocab is hard. :)

In ClassA, I'm instantiating a subclass of ClassB. The change I'd like to make requires that a new variable, "myVar" (set in ClassA), is available to subclass ClassC but not subclass ClassD. What would be the most appropriate way to make that variable available to ClassC?

ClassA:

public class ClassA { private String myVar = "hi"; private String myStuff = "bye"; private int myOption = 1; private String getMyClass(String myStuff) throws Exception { final MyClassChoice myClass = getMyClassChoice(myOption); return myClass.getResponse(myStuff); } private MyClassChoice getMyClassChoice(myOption) { switch(myOption) { case 1: return new ClassC(); case 2: return new ClassD(); } } } 

ClassB:

public class ClassB { public abstract String getResponse(String myStuff) throws IOException; } 

ClassC:

public class ClassC extends ClassB { // do stuff with myStuff // do stuff with myVar } 

ClassD:

public class ClassD extends ClassB { // do stuff with myStuff } 
4
  • Declare the field private - private fields are not inherited Commented Jun 7, 2016 at 0:19
  • @MaciejCygan But then how would ClassC gain access to it? Commented Jun 7, 2016 at 0:25
  • Why don't you pass myVar into the constructor of ClassC? Commented Jun 7, 2016 at 0:26
  • @DrewKennedy yeah true... :(, What Shmosel actually suggested isn't a bad idea. Commented Jun 7, 2016 at 0:29

2 Answers 2

3

You either pass it to the constructor when instantiating the object and save it in an instance variable,

public class ClassA { // ... private MyClassChoice getMyClassChoice(myOption) { switch(myOption) { case 1: return new ClassC(myVar); case 2: return new ClassD(); } } } public final class ClassC extends ClassB { private String myVar; // constructor: public ClassC(String myVar) { this.myVar = myVar; } // do stuff with myStuff // do stuff with myVar private void doStuff() { System.out.println(myVar); } } 

Or you pass it to the method when you use it,

public final class ClassA { // ... public void someMethodUsingClassCDoStuff() { myClass.doStuff(myVar); } } public final class ClassC extends ClassB { // do stuff with myStuff // do stuff with myVar public void doStuff(String myVar) { System.out.println(myVar); } } 
Sign up to request clarification or add additional context in comments.

Comments

-1

First of all, make your variable myVar in calss A protected. Put your class A,B,C in the same package. Put your class D in a different package.

6 Comments

The person who gave -1 to this answer does not understand OOP.
Well what you suggested is not correct really. You don't know the OP program structure so you can not tell him to move things around just like that. Also there are more 'appropriate' ways of solving OP issues - like the solution provided by Shmosel
Any variable can be accessed in a class by passing that variable through the constructor and it is not specific solution to the particular variable myVar in this case. Again, if several other types of variables need to be accessed in the same way in class C, than multiple constructors need to be defined which is also not a good idea. It is good I think to think it from variable scope perspective.
You don't need multiple constructors - you can have a single constructor with multiple variables but this again is debatable and out of the scope as far as OP is concerned. His question was regarding a specific example and he was looking for a specific answer.
Yes, I agree with you 100 % about "single constructor with multiple variables and this is debatable". That's why I suggested that way. In fact we no body know here the perfect OP structure of this code what you said before and people are answering from different thinking. -1 is not expected. :-)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.