public class Country {
protected String countryName;
protected double countryType;
protected double countryLocation;
protected double GDP;
// Constructor for the attributes present in the superclass
public Country(String countryName, double countryType, double countryLocation, double GDP) {
this.countryName = countryName;
this.countryType = countryType;
this.countryLocation = countryLocation;
this.GDP = GDP;
}
public void name () {
System.out.println("Country: America");
}
public void type () {
System.out.println("Type: Advanced");
}
public void size () {
System.out.println("Size: Huge");
}
public void location () {
System.out.println("Continent: North America");
}
public void GDP () {
System.out.print("Money: Rich");
}
}
public class America extends Country {
// Additional attribute not present in the superclass
protected String GDPtype;
// Constructor for Subclass
public America(String countryName, double countryType, double countryLocation, double GDP, String GDPtype) {
// We use the Superclass constructor for the shared attributes through the keyword "super"
super(countryName, countryType, countryLocation, GDP);
// hornSound is not in the Superclass, so we add it separately in the constructor
this.GDPtype = GDPtype;
}
// We use override to change the functionality in the subclass of an existing method in the superclass
@Override
public void size () {
System.out.println("3.797 Million Miles^2 ");
}
public void location () {
System.out.println("Between Canada and Mexico");
}
// Here, we don't fully change the functionality of the existing horn method in the superclass
// Instead, we take all of the functionality of the superclass method, and then add on to it
public void GDP () {
super.GDP();
System.out.println(GDPtype);
}
public static void main(String[] args) {
// 5 argument constructor
America modelS = new America("Tesla", 396, 4, 200, "Asf");
// Example of late binding
Country country = new America("Tesla", 396, 4, 200, "Asf");
// We can still use the methods from the child class, even though we didn't mention them in the subclass!
modelS.name();
// Using the overridden method
modelS.size();
modelS.type();
modelS.location();
// Using the method we added on to
modelS.GDP();
country.GDP();
}
}
America.main(null);
public class Acountry extends Country {
public Acountry (String countryName, double countryType, double countryLocation, double GDP) {
super(countryName, countryType, countryLocation, GDP);
}
@Override
public void size () {
System.out.println("The Fourth Largest Country!");
}
public void dimensions (int a) {
System.out.println("Length is " + a);
}
public void dimensions (int a, int b) {
System.out.println("Length is " + a + " and height is " + b);
}
public static void main(String[] args) {
// 4 superclass argument constructor
Acountry country2 = new Acountry("Apple", 348, 4, 145);
// Using the overridden method
country2.size();
// Using the overloaded method
country2.dimensions(3000);
country2.dimensions(3000, 6100);
}
}
Acountry.main(null);