Use of abstract classes continued

Run Settings
LanguageJava
Language Version
Run Command
abstract class Animal { private String scientificName; private String countryOfOrigin; // Constructors public Animal(String scientificName, String countryOfOrigin) { this.scientificName = scientificName; this.countryOfOrigin = countryOfOrigin; } // Getter methods public String getScientificName() { return scientificName; } public String getCountryOfOrigin() { return countryOfOrigin; } // Setter methods public void setScientificName(String scientificName) { this.scientificName = scientificName; } public void setCountryOfOrigin(String countryOfOrigin) { this.countryOfOrigin = countryOfOrigin; } // Abstract behavior method abstract void behavior(); } abstract class Human extends Animal { private String ethnicity; public Human(String ethnicity, String countryOfOrigin) { super("Homo Sapiens", countryOfOrigin); this.ethnicity = ethnicity; } // Getter and Setter for ethnicity public String getEthnicity() { return ethnicity; } public void setEthnicity(String ethnicity) { this.ethnicity = ethnicity; } @Override void behavior() { System.out.println("Bipedal walking, advanced cognitive abilities, etc."); } abstract void culturalPractice(); } class Asian extends Human { public Asian(String ethnicity, String countryOfOrigin) { super(ethnicity, countryOfOrigin); } @Override void culturalPractice() { System.out.println("Has cultural practices specific to Asia."); } } class European extends Human { public European(String ethnicity, String countryOfOrigin) { super(ethnicity, countryOfOrigin); } @Override void culturalPractice() { System.out.println("Has cultural practices specific to Europe."); } } public class Main { public static void main(String[] args) { // Create an Asian person with initial values Asian asianPerson = new Asian("East Asian", "China"); displayPersonInfo(asianPerson); asianPerson.behavior(); asianPerson.culturalPractice(); System.out.println("\nUpdating information...\n"); // Let's say this person discovered through a DNA test that they have significant Southeast Asian heritage // and they also migrated to Thailand. We'll update the object's values using setter methods: asianPerson.setEthnicity("Southeast Asian"); asianPerson.setCountryOfOrigin("Thailand"); // Display the updated information displayPersonInfo(asianPerson); asianPerson.behavior(); asianPerson.culturalPractice(); System.out.println(); // Create a European person European europeanPerson = new European("Nordic", "Sweden"); displayPersonInfo(europeanPerson); europeanPerson.behavior(); europeanPerson.culturalPractice(); } public static void displayPersonInfo(Human person) { System.out.println("Scientific Name: " + person.getScientificName()); System.out.println("Ethnicity: " + person.getEthnicity()); System.out.println("Country of Origin: " + person.getCountryOfOrigin()); } } // public class Main { // public static void main(String[] args) { // Asian asianPerson = new Asian("East Asian", "China"); // System.out.println("Scientific Name: " + asianPerson.getScientificName()); // System.out.println("Ethnicity: " + asianPerson.getEthnicity()); // System.out.println("Country of Origin: " + asianPerson.getCountryOfOrigin()); // asianPerson.behavior(); // asianPerson.culturalPractice(); // System.out.println(); // European europeanPerson = new European("Nordic", "Sweden"); // System.out.println("Scientific Name: " + europeanPerson.getScientificName()); // System.out.println("Ethnicity: " + europeanPerson.getEthnicity()); // System.out.println("Country of Origin: " + europeanPerson.getCountryOfOrigin()); // europeanPerson.behavior(); // europeanPerson.culturalPractice(); // } // }
Editor Settings
Theme
Key bindings
Full width
Lines