FAQs on the exercise Review of Inheritance and Polymorphism
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
Difference between Polymorphism and Method Override
In the Cheatsheet examples, on Polymorphism, we have the ‘cat’ class definition:
class Cat extends Animal {
public void greeting() {
System.out.println(“The cat meows.”);
}
}
and in the Method Override example, we have the class definition:
class Dog extends Animal {
// Dog’s eating method overrides Animal’s eating method @Override
public void eating() {
System.out.println(“The dog is eating.”);
}
}
I’ve not understood the difference in the inheritance between these two. The example functionality appears the same.
Can you explain the difference between using ‘@Override’ and not…?
@Override isn’t necessary but it’s highly recommended. It improves code readability, helps you catch silly spelling mistakes, and stops you accidentally defining a brand new method without you realizing. It saves a lot of time while debugging as your compiler will check for you to make sure you’re actually overriding something, and will let you know if you aren’t.
Do I understand correctly that there’s no way for me to know the length of spaghetti without creating a Spaghetti object in the main method? Like this
public static void main(String[] args) {
Noodle spaghetti = new Spaghetti();
System.out.println(spaghetti.lengthInCentimeters);
P.S.: By the way, check out my eatNoodle method! Isn’t it great? In addition to the following, it requires adding one more parameter to each of the subclasses
import java.util.Random;
class Noodle {
[...]
protected boolean isPlural;
protected double amountLeft = 20;
protected boolean isEaten = false;
protected static boolean isAmbulanceComing = false;
Noodle(double lenInCent, double wthInCent, String shp, String ingr, boolean pl) {
[...]
this.isPlural = pl;
}
[...]
public void eatNoodle(double amnt){
double amountEaten = amnt;
String name = this.getClass().getSimpleName();
double timeToEat = lengthInCentimeters * widthInCentimeters * amountEaten;
Random random = new Random();
int randomInt = random.nextInt(10)+1;
if(!isAmbulanceComing){
if(amountEaten <= amountLeft){
amountLeft = amountLeft - amountEaten;
if(amountEaten < 10){
if(name == "Spaghetti"){
System.out.print("\n" + name + " have been eaten in " + timeToEat + " minutes!");
} else {
System.out.print("\n" + name + " has been eaten in " + timeToEat + " minutes!");
}
System.out.print(" You can feel those nice and " + shape + " " + ingredients + " noodles giving off their sweet nutrients in your gastrointestinal tract. ");
if(randomInt <= 3){
System.out.println("Magnificent!");
} else if (randomInt >= 7){
System.out.println("Marvelous!");
} else {
System.out.println("Delectable!");
}
if(amountEaten > 1){
System.out.println("\nBy the way, did you really eat " + amountEaten + " kilogram of " + name.toLowerCase() + "?");
}
} else {
System.out.println("\nDid you just eat " + amountEaten + " kilogram of " + name.toLowerCase() + "?! I'm calling the ambulance!");
isAmbulanceComing = true;
}
} else {
System.out.println("\nUnfortunately, there's only " + amountLeft + " kg of " + name.toLowerCase() + " left. You can't eat " + amountEaten + " kg of it!");
}
if (amountLeft == 0.0){
isEaten = true;
}
} else {
System.out.println("\nYou're not gonna consume one single noodle no more!");
}
}
P.P.S.: My eatNoodle method works perfectly, but it seems I encountered a bug. Do you have any idea why it happens?