Need help on Language families

link to the project: https://www.codecademy.com/courses/learn-java/projects/java-language-families

Hi i have been having problem with the Mayan.java file. Specifically with step 8 of the project. I ran the code on the Language.java file and i get this output.

output: 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.

My source code:
Language.java:
public class Language{

// instance fields
protected String name;
protected int numSpeakers;
protected String regionsSpeaker;
protected String wordOrder;

//constructor
public Language(String name, int numSpeakers, String regionsSpeaker, String wordOrder){
this.name = name;
this.numSpeakers = numSpeakers;
this.regionsSpeaker = regionsSpeaker;
this.wordOrder = wordOrder;
}

public void getInfo(){
System.out.println(name + " is spoken by\n" +
numSpeakers + " people mainly in " + regionsSpeaker + “\n”
+ "The language follows the word order: " + wordOrder);
}

public static void main(String args){
Language greek = new Language(“Greek”, 200000, “Europe”,“subject-verb-object”);

// call 
greek.getInfo();

// call mayan lang
Mayan kiche = new Mayan("Ki'che'",2330000);
kiche.getInfo();

}

}

Mayan.java
public class Mayan extends Language{
int speakers;
String languageName;

	public Mayan(String languageName, int speakers) {
		super(languageName, speakers, "Central America", "verb-object-subject");
		
}


	@Override
	public void getInfo() {
	System.out.println(languageName + " is spoken by " + speakers + " people mainly in " + regionsSpeaker + ".");
	System.out.println("The language follows the word order: " + wordOrder);
	System.out.println("Fun fact: " + languageName + " is an ergative language.");
	}

}

I am clueless as to how to get around this problem. any help is appreaciated.
Edit: I have found the solution, after just typing things out randomly.
add : this.languageName =this.name; this.speakers = this.numSpeakers;
I still don’t understand how this works, but it works. But any explanation at all is appreaciated.

In which case, did you really “find” the solution? Or did you just stumble upon something which seemed to do what you want, and assume that it worked without questioning it further? :slight_smile:

You may need to re-visit, or spend some more time on, classes if this is confusing you.

Inside of your declaration for public Mayan, you’re invoking the super() function to call the constructor for the parent class. This initialises the class attributes defined in Language, so that:

Language.name = languageName
Language.numSpeakers = speakers
Language.regionsSpeaker = "Central America"
Language.wordOrder = "verb-object-subject"

Nowhere do you actually initialise the class attributes for Mayan, so when you’re calling kiche.getInfo(), these attributes are null/zero because you’ve never done anything with them - but it does have the four attributes listed above through inheritance.

That’s why, when you do - for example - this.languageName = this.name, what you’re actually doing is saying Mayan.languageName = Language.name. The this keyword refers to the current object, that being an instance of Mayan.

Mayan has inherited the attribute name from the superclass Language, which you initialised, and that’s why you’re able to assign that value to Mayan.languageName as you have done.

You either need to change your Mayan constructor to initialise the class attributes languageName / speakers or just re-use the existing attributes inherited from Language. (I’m unsure which approach is appropriate, as I can’t see the project because I don’t have Pro!)

Does that make any sense? :slight_smile: