I'm fairly new to Java and still practicing by basics with object-oriented design and programming. I have a few questions regarding inheritance, object creation, and the correct usage of a super() call.
Please consider the following:
Superclass,
package pet
public class Dog {
private String foodDogEats;
public Dog() {
this.foodDogEats;
}
public String getDogFood() {
return foodDogEats;
}
public void setDogFood() {
foodDogEats = "kibble";
}
}
subclass,
package pet
public class Fido extends Dog {
public Fido() {
super();
Dog dog = New Dog();
this.whatDoesFidoEat();
}
public whatDoesFidoEat(Dog dog) {
System.out.println("Fido eats " + dog.getDogFood());
}
}
and controller.
package pet
public class MyDogFido {
public void MyDogFido() {
Fido fido = new Fido();
}
}
The object in this example is, to instantiate a new Fido object and in the process of doing so perform the print behavior in whatDoesFidoEat(). I have a few questions -
Since I am already invoking a new Fido object (which is an extension of Dog, and if I recall correctly inherits its methods and variables), is instantiation of a new Dog object in the Fido() constructor redundant?
What about use of super()?
Which is the correct behavior - instantiating a Dog object in Fido(), calling super(), or both?
I initially wrote some similar code using both in the controlling class. After writing this question, though, my gut is telling me that I should only call super().
Feel free to comment on my sample code (particularly the this.foodDogEats; line in the Dog() constructor - also unsure about the usage there) as well.
Thanks!