Yesterday I posted my first solution to an interview problem here. I am now aware that I have many weak spots in Java and need to do extensive review before tackling any more interviews. Having said that, piecing together what people on here mentioned, I have come to what I hope is a good solution, and am asking to have it reviewed again.
Here is the problem:
You've gone back in time to 500BC Athens and Socrates wants you to build him an app to help classify animals.
- Build the classes
Animal
,Cat
, andBug
.- Define the properties "
color
" and "leg_number
" on the relevant and necessary classes. Have them be initialized within a constructor.- Add the functionality that would allow us to call a method "
move
" with theCat
andBug
classes. Have the method return a string "I'm moving with<number of legs>
legs!", with the "<number of legs>
" beingleg_number
as set on the class.- Add a new class called
Bird
. Add the property "wing_number
". Add the functionality that would allow us to call a method "move
" with theBird
class. Have the method return the string "I'm moving with<number of legs>
legs!" ifwing_number
doesn't have an applicable value. Ifwing_number
does have an applicable value, return the string "I'm flying".
Here was my final solution that I submitted:
public class SocratesApp {
public static void main(String[] args) {
Cat myCat = new Cat();
System.out.println(myCat.move());
Bug myBug = new Bug();
System.out.println(myBug.move());
Bird myBird1 = new Bird(2);
System.out.println(myBird1.move());
Bird myBird2 = new Bird(0);
System.out.println(myBird2.move());
}
}
class Animal {
protected String color;
protected int leg_number;
public Animal() {
}
public String move() {
return "I'm walking with " + leg_number + " legs!";
}
}
class Cat extends Animal {
public Cat() {
color = "orange";
leg_number = 4;
}
}
class Bug extends Animal {
public Bug() {
color = "green";
leg_number = 6;
}
}
class Bird extends Animal {
private int wing_number;
public Bird(int wing_number) {
color = "yellow";
leg_number = 2;
this.wing_number = wing_number;
}
// @Override
public String move() {
if (this.wing_number > 0) {
return "I'm flying";
} else {
return "I'm walking with " + leg_number + " legs!";
}
}
}
A couple of observations of my one:
First, since this consists of a superclass and subclasses, I thought I was required to reference the superclass constructor in my subclass constructors with super(). However, I found out the code worked without it. I don't know if I was wrong about this requirement or if it was an old requirement that changed with the evolution of Java.
Second, I thought that the @Override decoration was required for the move() method in the Bird class due to the move() method in the Animal superclass. However, the code works without it. I'm thinking now that because the move() method in Bird has a parameter and the one in Animal does not, the Bird one does not constitute a true override of the Animal one.
Third, I was sure that to instantiate my different objects, the instantiation would somehow have to include the Animal superclass. When reviewing code examples on the web, I would see superclasses referenced like Animal myCat = new Cat()
or, I think, Cat myCat = new Animal()
, maybe one other way of referencing Animal that I don't remember. It turns out that Cat myCat = new Cat()
was the only one that worked correctly. I'm not sure what the difference is between the code examples I saw on the Internet and my code, but I went with what worked.
Any comments on my observations as well as my code are welcome.