get same 12 variables from 8 classes in different packages java
I am wondering if there is a way to get multiple variables (12 of them) from 8 different classes in different packages:
(Humans.Old (3 of 8), Humans.Adult (2 of 8), and Humans.Child (3 of 8)).
Here is an example of one of the classes:
package Humans.Adult;
import Humans.BaseHuman;
import java.util.ArrayList;
import java.util.List;
public class Brother extends BaseHuman {
public void init() {
List<Integer> startingItemCodes = new ArrayList<Integer>();
startingItemCodes.add(0);
setMAX_SANE(110); // Part of getters and setters in BaseHuman.java
setMAX_WATER(110); // This also
setMAX_HUNGER(110); // This also
setHEALTH(100); // This also
setHUNGER_DEC(10); // This also
setSANE_DEC(10); // This also
setWATER_DEC(10); // This also
setHEALTH_DEC_HUNGER(10); // This also
setHEALTH_DEC_THIRST(10); // This also
setSICK_CHANCE(0.05f); // This also
setNAME("Brother"); // This also
setSTARTING_ITEMS(startingItemCodes); // This also
}
}
So, for each of the 8 classes like the top one, can I call all of the 12 getter methods extended by BaseHuman, so I would end up calling 96 getter methods. Sorry, I'm fairly new to Java.
How would I do this?
--Thanks!
java
|
show 1 more comment
I am wondering if there is a way to get multiple variables (12 of them) from 8 different classes in different packages:
(Humans.Old (3 of 8), Humans.Adult (2 of 8), and Humans.Child (3 of 8)).
Here is an example of one of the classes:
package Humans.Adult;
import Humans.BaseHuman;
import java.util.ArrayList;
import java.util.List;
public class Brother extends BaseHuman {
public void init() {
List<Integer> startingItemCodes = new ArrayList<Integer>();
startingItemCodes.add(0);
setMAX_SANE(110); // Part of getters and setters in BaseHuman.java
setMAX_WATER(110); // This also
setMAX_HUNGER(110); // This also
setHEALTH(100); // This also
setHUNGER_DEC(10); // This also
setSANE_DEC(10); // This also
setWATER_DEC(10); // This also
setHEALTH_DEC_HUNGER(10); // This also
setHEALTH_DEC_THIRST(10); // This also
setSICK_CHANCE(0.05f); // This also
setNAME("Brother"); // This also
setSTARTING_ITEMS(startingItemCodes); // This also
}
}
So, for each of the 8 classes like the top one, can I call all of the 12 getter methods extended by BaseHuman, so I would end up calling 96 getter methods. Sorry, I'm fairly new to Java.
How would I do this?
--Thanks!
java
Does this make sense? I tried to explain it the best I could...
– Sushila Jyothi Lévêque
Jan 2 at 22:12
no it doesnt make sense. i think u should first go ahead and give u some knowledge on oop principles. and for this problem specialy u need to know about inheritence principle well. also what is class, object. so just go through oop paradigm. as if u dont have this knowledge it quite impossible to give u a solution in a single answer.
– Golam Rahman Tushar
Jan 2 at 22:18
No, it does not make sense. Not to me, anyway. I see no getter methods, but I don't understand why you're concerned that there might be an issue with invoking public methods on an object that that object's class in fact has.
– John Bollinger
Jan 2 at 22:18
Why do you need the getters? Are you trying to create an instance of the inherited object?
– Stalemate Of Tuning
Jan 2 at 22:21
Also, separately, please use consensus Java naming conventions. In particular, method names should be in camelCase, with the initial letter lowercase. Example:setMaxSane()
. That makes it much easier on everyone reading your code -- maybe even you.
– John Bollinger
Jan 2 at 22:21
|
show 1 more comment
I am wondering if there is a way to get multiple variables (12 of them) from 8 different classes in different packages:
(Humans.Old (3 of 8), Humans.Adult (2 of 8), and Humans.Child (3 of 8)).
Here is an example of one of the classes:
package Humans.Adult;
import Humans.BaseHuman;
import java.util.ArrayList;
import java.util.List;
public class Brother extends BaseHuman {
public void init() {
List<Integer> startingItemCodes = new ArrayList<Integer>();
startingItemCodes.add(0);
setMAX_SANE(110); // Part of getters and setters in BaseHuman.java
setMAX_WATER(110); // This also
setMAX_HUNGER(110); // This also
setHEALTH(100); // This also
setHUNGER_DEC(10); // This also
setSANE_DEC(10); // This also
setWATER_DEC(10); // This also
setHEALTH_DEC_HUNGER(10); // This also
setHEALTH_DEC_THIRST(10); // This also
setSICK_CHANCE(0.05f); // This also
setNAME("Brother"); // This also
setSTARTING_ITEMS(startingItemCodes); // This also
}
}
So, for each of the 8 classes like the top one, can I call all of the 12 getter methods extended by BaseHuman, so I would end up calling 96 getter methods. Sorry, I'm fairly new to Java.
How would I do this?
--Thanks!
java
I am wondering if there is a way to get multiple variables (12 of them) from 8 different classes in different packages:
(Humans.Old (3 of 8), Humans.Adult (2 of 8), and Humans.Child (3 of 8)).
Here is an example of one of the classes:
package Humans.Adult;
import Humans.BaseHuman;
import java.util.ArrayList;
import java.util.List;
public class Brother extends BaseHuman {
public void init() {
List<Integer> startingItemCodes = new ArrayList<Integer>();
startingItemCodes.add(0);
setMAX_SANE(110); // Part of getters and setters in BaseHuman.java
setMAX_WATER(110); // This also
setMAX_HUNGER(110); // This also
setHEALTH(100); // This also
setHUNGER_DEC(10); // This also
setSANE_DEC(10); // This also
setWATER_DEC(10); // This also
setHEALTH_DEC_HUNGER(10); // This also
setHEALTH_DEC_THIRST(10); // This also
setSICK_CHANCE(0.05f); // This also
setNAME("Brother"); // This also
setSTARTING_ITEMS(startingItemCodes); // This also
}
}
So, for each of the 8 classes like the top one, can I call all of the 12 getter methods extended by BaseHuman, so I would end up calling 96 getter methods. Sorry, I'm fairly new to Java.
How would I do this?
--Thanks!
java
java
asked Jan 2 at 22:08
Sushila Jyothi LévêqueSushila Jyothi Lévêque
4410
4410
Does this make sense? I tried to explain it the best I could...
– Sushila Jyothi Lévêque
Jan 2 at 22:12
no it doesnt make sense. i think u should first go ahead and give u some knowledge on oop principles. and for this problem specialy u need to know about inheritence principle well. also what is class, object. so just go through oop paradigm. as if u dont have this knowledge it quite impossible to give u a solution in a single answer.
– Golam Rahman Tushar
Jan 2 at 22:18
No, it does not make sense. Not to me, anyway. I see no getter methods, but I don't understand why you're concerned that there might be an issue with invoking public methods on an object that that object's class in fact has.
– John Bollinger
Jan 2 at 22:18
Why do you need the getters? Are you trying to create an instance of the inherited object?
– Stalemate Of Tuning
Jan 2 at 22:21
Also, separately, please use consensus Java naming conventions. In particular, method names should be in camelCase, with the initial letter lowercase. Example:setMaxSane()
. That makes it much easier on everyone reading your code -- maybe even you.
– John Bollinger
Jan 2 at 22:21
|
show 1 more comment
Does this make sense? I tried to explain it the best I could...
– Sushila Jyothi Lévêque
Jan 2 at 22:12
no it doesnt make sense. i think u should first go ahead and give u some knowledge on oop principles. and for this problem specialy u need to know about inheritence principle well. also what is class, object. so just go through oop paradigm. as if u dont have this knowledge it quite impossible to give u a solution in a single answer.
– Golam Rahman Tushar
Jan 2 at 22:18
No, it does not make sense. Not to me, anyway. I see no getter methods, but I don't understand why you're concerned that there might be an issue with invoking public methods on an object that that object's class in fact has.
– John Bollinger
Jan 2 at 22:18
Why do you need the getters? Are you trying to create an instance of the inherited object?
– Stalemate Of Tuning
Jan 2 at 22:21
Also, separately, please use consensus Java naming conventions. In particular, method names should be in camelCase, with the initial letter lowercase. Example:setMaxSane()
. That makes it much easier on everyone reading your code -- maybe even you.
– John Bollinger
Jan 2 at 22:21
Does this make sense? I tried to explain it the best I could...
– Sushila Jyothi Lévêque
Jan 2 at 22:12
Does this make sense? I tried to explain it the best I could...
– Sushila Jyothi Lévêque
Jan 2 at 22:12
no it doesnt make sense. i think u should first go ahead and give u some knowledge on oop principles. and for this problem specialy u need to know about inheritence principle well. also what is class, object. so just go through oop paradigm. as if u dont have this knowledge it quite impossible to give u a solution in a single answer.
– Golam Rahman Tushar
Jan 2 at 22:18
no it doesnt make sense. i think u should first go ahead and give u some knowledge on oop principles. and for this problem specialy u need to know about inheritence principle well. also what is class, object. so just go through oop paradigm. as if u dont have this knowledge it quite impossible to give u a solution in a single answer.
– Golam Rahman Tushar
Jan 2 at 22:18
No, it does not make sense. Not to me, anyway. I see no getter methods, but I don't understand why you're concerned that there might be an issue with invoking public methods on an object that that object's class in fact has.
– John Bollinger
Jan 2 at 22:18
No, it does not make sense. Not to me, anyway. I see no getter methods, but I don't understand why you're concerned that there might be an issue with invoking public methods on an object that that object's class in fact has.
– John Bollinger
Jan 2 at 22:18
Why do you need the getters? Are you trying to create an instance of the inherited object?
– Stalemate Of Tuning
Jan 2 at 22:21
Why do you need the getters? Are you trying to create an instance of the inherited object?
– Stalemate Of Tuning
Jan 2 at 22:21
Also, separately, please use consensus Java naming conventions. In particular, method names should be in camelCase, with the initial letter lowercase. Example:
setMaxSane()
. That makes it much easier on everyone reading your code -- maybe even you.– John Bollinger
Jan 2 at 22:21
Also, separately, please use consensus Java naming conventions. In particular, method names should be in camelCase, with the initial letter lowercase. Example:
setMaxSane()
. That makes it much easier on everyone reading your code -- maybe even you.– John Bollinger
Jan 2 at 22:21
|
show 1 more comment
2 Answers
2
active
oldest
votes
One of the advantages of inheritance is, that you can call the methods of BaseHuman on every Class that is inheriting from it (e.g. Brother). So you can make a list of BaseHuman like this:
ArrayList<BaseHuman> bhList = new ArrayList<BaseHuman>();
and then add Brothers and every thing else to the list.
Brother broA = new Brother();
Sister sisA = new Sister();
bhList.add(broA);
bhList.add(sisA);
then you could call getters from every Object that is stored in the list like this:
for(BaseHuman bh : bhList){
System.out.println(bh.get());
}
The requirement is, that you overwrite the getter Method from the Superclass BaseHuman.
If you want to know more you can look into "Polymorphism".
Thank you so much!
– Sushila Jyothi Lévêque
Jan 2 at 22:23
add a comment |
I'm not entirely sure I understand the purpose behind the question, so if I misunderstood please let me know.
If you really just want to call all of the getters in every class, you can use @M.Dan's solution as he provided. But, to me it sounds like you are trying to create an instance of a class that inherits from BaseHuman
, so you can change its properties. For example, maybe OldHuman
has a lower value for HEALTH
but is otherwise the same.
Instead of calling all the getters again to set the fields, you can simply use the BaseHuman
class constructor by calling super()
:
class OldHuman extends BaseHuman {
public OldHuman() {
super(); //invokes parent constructor which you supplied default values
this.HEALTH = 75; //change HEALTH to desired amount instead
}
}
This way, you save yourself rewriting all the fields 96 times, and only change what you need. And, your OldHuman
will otherwise behave exactly the same (getters, setters, etc.)
If you still REALLY need to call all of the getters, you could at the very least write a method in BaseHuman
that returns all fields in a Collection of some sort, and let your classes inherit that instead.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013813%2fget-same-12-variables-from-8-classes-in-different-packages-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
One of the advantages of inheritance is, that you can call the methods of BaseHuman on every Class that is inheriting from it (e.g. Brother). So you can make a list of BaseHuman like this:
ArrayList<BaseHuman> bhList = new ArrayList<BaseHuman>();
and then add Brothers and every thing else to the list.
Brother broA = new Brother();
Sister sisA = new Sister();
bhList.add(broA);
bhList.add(sisA);
then you could call getters from every Object that is stored in the list like this:
for(BaseHuman bh : bhList){
System.out.println(bh.get());
}
The requirement is, that you overwrite the getter Method from the Superclass BaseHuman.
If you want to know more you can look into "Polymorphism".
Thank you so much!
– Sushila Jyothi Lévêque
Jan 2 at 22:23
add a comment |
One of the advantages of inheritance is, that you can call the methods of BaseHuman on every Class that is inheriting from it (e.g. Brother). So you can make a list of BaseHuman like this:
ArrayList<BaseHuman> bhList = new ArrayList<BaseHuman>();
and then add Brothers and every thing else to the list.
Brother broA = new Brother();
Sister sisA = new Sister();
bhList.add(broA);
bhList.add(sisA);
then you could call getters from every Object that is stored in the list like this:
for(BaseHuman bh : bhList){
System.out.println(bh.get());
}
The requirement is, that you overwrite the getter Method from the Superclass BaseHuman.
If you want to know more you can look into "Polymorphism".
Thank you so much!
– Sushila Jyothi Lévêque
Jan 2 at 22:23
add a comment |
One of the advantages of inheritance is, that you can call the methods of BaseHuman on every Class that is inheriting from it (e.g. Brother). So you can make a list of BaseHuman like this:
ArrayList<BaseHuman> bhList = new ArrayList<BaseHuman>();
and then add Brothers and every thing else to the list.
Brother broA = new Brother();
Sister sisA = new Sister();
bhList.add(broA);
bhList.add(sisA);
then you could call getters from every Object that is stored in the list like this:
for(BaseHuman bh : bhList){
System.out.println(bh.get());
}
The requirement is, that you overwrite the getter Method from the Superclass BaseHuman.
If you want to know more you can look into "Polymorphism".
One of the advantages of inheritance is, that you can call the methods of BaseHuman on every Class that is inheriting from it (e.g. Brother). So you can make a list of BaseHuman like this:
ArrayList<BaseHuman> bhList = new ArrayList<BaseHuman>();
and then add Brothers and every thing else to the list.
Brother broA = new Brother();
Sister sisA = new Sister();
bhList.add(broA);
bhList.add(sisA);
then you could call getters from every Object that is stored in the list like this:
for(BaseHuman bh : bhList){
System.out.println(bh.get());
}
The requirement is, that you overwrite the getter Method from the Superclass BaseHuman.
If you want to know more you can look into "Polymorphism".
answered Jan 2 at 22:17
M.DanM.Dan
267215
267215
Thank you so much!
– Sushila Jyothi Lévêque
Jan 2 at 22:23
add a comment |
Thank you so much!
– Sushila Jyothi Lévêque
Jan 2 at 22:23
Thank you so much!
– Sushila Jyothi Lévêque
Jan 2 at 22:23
Thank you so much!
– Sushila Jyothi Lévêque
Jan 2 at 22:23
add a comment |
I'm not entirely sure I understand the purpose behind the question, so if I misunderstood please let me know.
If you really just want to call all of the getters in every class, you can use @M.Dan's solution as he provided. But, to me it sounds like you are trying to create an instance of a class that inherits from BaseHuman
, so you can change its properties. For example, maybe OldHuman
has a lower value for HEALTH
but is otherwise the same.
Instead of calling all the getters again to set the fields, you can simply use the BaseHuman
class constructor by calling super()
:
class OldHuman extends BaseHuman {
public OldHuman() {
super(); //invokes parent constructor which you supplied default values
this.HEALTH = 75; //change HEALTH to desired amount instead
}
}
This way, you save yourself rewriting all the fields 96 times, and only change what you need. And, your OldHuman
will otherwise behave exactly the same (getters, setters, etc.)
If you still REALLY need to call all of the getters, you could at the very least write a method in BaseHuman
that returns all fields in a Collection of some sort, and let your classes inherit that instead.
add a comment |
I'm not entirely sure I understand the purpose behind the question, so if I misunderstood please let me know.
If you really just want to call all of the getters in every class, you can use @M.Dan's solution as he provided. But, to me it sounds like you are trying to create an instance of a class that inherits from BaseHuman
, so you can change its properties. For example, maybe OldHuman
has a lower value for HEALTH
but is otherwise the same.
Instead of calling all the getters again to set the fields, you can simply use the BaseHuman
class constructor by calling super()
:
class OldHuman extends BaseHuman {
public OldHuman() {
super(); //invokes parent constructor which you supplied default values
this.HEALTH = 75; //change HEALTH to desired amount instead
}
}
This way, you save yourself rewriting all the fields 96 times, and only change what you need. And, your OldHuman
will otherwise behave exactly the same (getters, setters, etc.)
If you still REALLY need to call all of the getters, you could at the very least write a method in BaseHuman
that returns all fields in a Collection of some sort, and let your classes inherit that instead.
add a comment |
I'm not entirely sure I understand the purpose behind the question, so if I misunderstood please let me know.
If you really just want to call all of the getters in every class, you can use @M.Dan's solution as he provided. But, to me it sounds like you are trying to create an instance of a class that inherits from BaseHuman
, so you can change its properties. For example, maybe OldHuman
has a lower value for HEALTH
but is otherwise the same.
Instead of calling all the getters again to set the fields, you can simply use the BaseHuman
class constructor by calling super()
:
class OldHuman extends BaseHuman {
public OldHuman() {
super(); //invokes parent constructor which you supplied default values
this.HEALTH = 75; //change HEALTH to desired amount instead
}
}
This way, you save yourself rewriting all the fields 96 times, and only change what you need. And, your OldHuman
will otherwise behave exactly the same (getters, setters, etc.)
If you still REALLY need to call all of the getters, you could at the very least write a method in BaseHuman
that returns all fields in a Collection of some sort, and let your classes inherit that instead.
I'm not entirely sure I understand the purpose behind the question, so if I misunderstood please let me know.
If you really just want to call all of the getters in every class, you can use @M.Dan's solution as he provided. But, to me it sounds like you are trying to create an instance of a class that inherits from BaseHuman
, so you can change its properties. For example, maybe OldHuman
has a lower value for HEALTH
but is otherwise the same.
Instead of calling all the getters again to set the fields, you can simply use the BaseHuman
class constructor by calling super()
:
class OldHuman extends BaseHuman {
public OldHuman() {
super(); //invokes parent constructor which you supplied default values
this.HEALTH = 75; //change HEALTH to desired amount instead
}
}
This way, you save yourself rewriting all the fields 96 times, and only change what you need. And, your OldHuman
will otherwise behave exactly the same (getters, setters, etc.)
If you still REALLY need to call all of the getters, you could at the very least write a method in BaseHuman
that returns all fields in a Collection of some sort, and let your classes inherit that instead.
answered Jan 2 at 22:34
Stalemate Of TuningStalemate Of Tuning
542315
542315
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013813%2fget-same-12-variables-from-8-classes-in-different-packages-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does this make sense? I tried to explain it the best I could...
– Sushila Jyothi Lévêque
Jan 2 at 22:12
no it doesnt make sense. i think u should first go ahead and give u some knowledge on oop principles. and for this problem specialy u need to know about inheritence principle well. also what is class, object. so just go through oop paradigm. as if u dont have this knowledge it quite impossible to give u a solution in a single answer.
– Golam Rahman Tushar
Jan 2 at 22:18
No, it does not make sense. Not to me, anyway. I see no getter methods, but I don't understand why you're concerned that there might be an issue with invoking public methods on an object that that object's class in fact has.
– John Bollinger
Jan 2 at 22:18
Why do you need the getters? Are you trying to create an instance of the inherited object?
– Stalemate Of Tuning
Jan 2 at 22:21
Also, separately, please use consensus Java naming conventions. In particular, method names should be in camelCase, with the initial letter lowercase. Example:
setMaxSane()
. That makes it much easier on everyone reading your code -- maybe even you.– John Bollinger
Jan 2 at 22:21