r/javahelp • u/[deleted] • Nov 03 '20
How to give subclass access to super private fields
I have a UML diagram which outlined how to write the code , including animal super class with private fields name size and breed. The subclass cat has an overloaded constructor which gives error Animal.name is not visible.
Constructor looks like
public Cat(String name, String breed, int size){
this.name = name;
this.breed = breed;
this.size = size;
} I tried changing it to setName(name); but that wouldn’t work does anyone have any ideas sticking to private fields and overloaded constructor as per the instructions
3
u/-themailerdoraemon- Nov 03 '20
per the instructions
What instructions? Why are you giving us incomplete information?
0
Nov 03 '20
Private fields in animal class and overloaded constructor Cat(String, String, int)
4
u/-themailerdoraemon- Nov 03 '20
Next time, do not just respond with incomplete information and sentences!
Read up on how to use the keyword super, and getters.
-1
Nov 03 '20
Super is giving me the same error I tried thay after posting
2
u/-themailerdoraemon- Nov 03 '20
Why don't you try posting the actual code, the whole class, you are having trouble with?
-2
Nov 03 '20
Because I’m on my phone and i don’t remember my Reddit password, but I just realized it’s because I also have a dog constructor who’s body I forgot to change. Thanks !
3
u/-themailerdoraemon- Nov 03 '20
AND? What exactly is your question????
-1
Nov 03 '20
I mentioned that the overloaded class gives an error Animal.name is not visible so I guess logically my question is how to work around this sticking to the instructions
2
u/t_a_a_1 Nov 03 '20
Aren't you using getters and setters?
1
Nov 03 '20
I am
2
u/t_a_a_1 Nov 03 '20
Then what's the issue? According to your code the subclass Cat has fields name, breed and size and not the super class Animal.
1
Nov 03 '20
I’m getting the error animal.name is not visible and it stops my programming from running compared to when I run with protected modifier but needs to be private
1
u/t_a_a_1 Nov 03 '20
Yeah, that's cos you can't access private variables directly that way. You need to access them via getters and setters, and not like Animal.name. Your Animal class should have getter and setter methods for all three variables - name, breed and size. Otherwise you won't be able to modify them.
1
1
1
3
u/mIb0t Nov 03 '20
Please add more code. The part you posted is not enought to figure out what the problem is.
1
u/Code_Talks Nov 03 '20
To get access to the super class fields you need to create public getters and setters in the super class for those fields.
1
u/evils_twin Nov 03 '20
Usually you would call the constructor of the Animal class from the Cat class
0
•
u/AutoModerator Nov 03 '20
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.