Why my code is taking null as output value instead of the parameter passed on?
Parent Class:
class Language{
protected String name;
protected int numSpeakers;
protected String regionsSpoken;
protected String wordOrder;
public Language(String getName, int getNumSpeakers, String getRegionsSpoken, String getWordOrder){
this.name = getName;
this.numSpeakers = getNumSpeakers;
this.regionsSpoken = getRegionsSpoken;
this.wordOrder = getWordOrder;
}
public void getInfo(){
System.out.println(name+ " is spoken by "+numSpeakers+" people mainly in "+regionsSpoken);
System.out.println("The language follows the word order: "+wordOrder);
}
public static void main(String[] args){
Mayan mayanLanguage = new Mayan("Ki'che'",30000);
mayanLanguage.getInfo();
}
}
Child Class:
class Mayan extends Language {
protected String name;
protected int numSpeakers;
Mayan(String languageName,int speakers ){
super(languageName,speakers,"Central America","verb-object-subject");
}
@Override
public void getInfo() {
System.out.println(name+" is spoken by "+numSpeakers+" people mainly in Central America.");
System.out.println("The language follows the word order: verb-object-subject");
System.out.println("Fun fact: "+name+" is an ergative language.");
}
}
I have looked into the code and tried to resolve it by making changes but nothing seems to work, I am getting stuck into what is the mistake that I am not seeing in the code.
Expected is:
Ki'che' is spoken by 2330000 people mainly in Central America. The language follows the word order: verb-object-subject Fun fact: Ki'che' is an ergative language.
Actual is:
null is spoken by 0 people mainly in Central America. The language follows the word order: verb-object-subject Fun fact: null is an ergative language.
In Mayan
you have the fields
protected String name;
protected int numSpeakers;
Removing these will fix your issue. The reason this issue is happening is because when you define the two lines above, you are hiding the two fields from Language
and you would have to access the fields from Language
like super.name
, super.numSpeakers
, etc...
Something like the following is what you are probably after.
public class Mayan extends Language {
private static final String REGION = "Central America";
private static final String WORD_ORDER = "verb-object-subject";
public Mayan(String languageName, int speakers) {
super(languageName, speakers, REGION, WORD_ORDER);
}
@Override
public void getInfo() {
super.getInfo();
System.out.println("Fun fact: " + name + " is an ergative language.");
}
}
As Gavin pointed out, the access modifiers can be restricted. If you are working within a single package for your program, you might end up with something like
Language.java
class Language {
String name;
private int numSpeakers;
private String regionsSpoken;
private String wordOrder;
Language(String getName, int getNumSpeakers, String getRegionsSpoken, String getWordOrder) {
this.name = getName;
this.numSpeakers = getNumSpeakers;
this.regionsSpoken = getRegionsSpoken;
this.wordOrder = getWordOrder;
}
void getInfo() {
System.out.println(name + " is spoken by " + numSpeakers + " people mainly in " + regionsSpoken);
System.out.println("The language follows the word order: " + wordOrder);
}
public static void main(String[] args) {
Mayan mayanLanguage = new Mayan("Ki'che'",30000);
mayanLanguage.getInfo();
}
}
Mayan.java
class Mayan extends Language {
private static final String REGION = "Central America";
private static final String WORD_ORDER = "verb-object-subject";
Mayan(String languageName, int speakers) {
super(languageName, speakers, REGION, WORD_ORDER);
}
@Override
void getInfo() {
super.getInfo();
System.out.println("Fun fact: " + name + " is an ergative language.");
}
}