I was practising the past year question just now. I wanted the program to show this:

Continent name: Australia
Number of Countries: 1
Country name: Australia
Population: 22118256

But my code makes the program to show the first 2 lines.

package jan2012q5b;

import java.util.*;

class Continent
{
    protected String continentName;
    protected int countriesNo;

    public Continent()
    {
        continentName = "??";
        countriesNo = 0;
    }

    public Continent(String continentName, int countriesNo) {
        this.continentName = continentName;
        this.countriesNo = countriesNo;
    }

    public String getContinentName() {
        return continentName;
    }

    public void setContinentName(String continentName) {
        this.continentName = continentName;
    }

    public int getCountriesNo() {
        return countriesNo;
    }

    public void setCountriesNo(int countriesNo) {
        this.countriesNo = countriesNo;
    }

    @Override
    public String toString()
    {
        return "Continent name: " + continentName + "\n" +
                "Number of countries: " + countriesNo + "\n";
    }
}

class Country extends Continent
{
    private String countryName;
    private int population;

    public Country()
    {
        countryName = "???";
        population = 0;
    }

    public Country(String countryName, int population)
    {
        this.countryName = countryName;
        this.population = population;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public int getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }

    public Country(String count_name, int p, String continentName, int countriesNo)
    {
        super(continentName, countriesNo);
        countryName = count_name;
        population = p;
    }

    @Override
    public String toString()
    {
        return "Country name: " + countryName + "\n" +
               "Population: " + population;
    }
}

public class Jan2012q5b 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);

        Country cnt;
        cnt = new Country ("Australia", 1, "Australia", 22118256);

        System.out.println(cnt.toString());
    }

}

Your subclass overrides the superclass's toString() so that doesn't get executed. You could do something like

public String toString() {
  return super.toString() + "\n Country name: " + countryName + "\n" +
         "Population: " + population;
}

ps: I guess you realise that a Country is NOT a subclass of Continent in the real world. The relationship is Continent has-a Country, not Country is-a Continent

It works, but it shows the number of countries as 22118256 and population as 1.

OK, so you have the data the other way round from how the constructor is defined!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.