I know this is very basic but I am having trouble putting the pieces together in an unfamiliar language. I'm mapping UML into Java code and inheritance is throwing me. I have an ERD like this:
Animal
----------------------------
-color: int
-weight: int
----------------------------
+ getColor() : int
+ getWeight(): int
----------------------------
^(Inheritance Triangle)
|
|
----------------------------
Dog
----------------------------
-breed: string
----------------------------
+ getBreed()
----------------------------
So of course a dog IS-A animal, and I can call getColor from a dog class, etc. My problem is about the variables, in particular contructors. When I implement this, I have
public class Animal
{
private int color;
private int weight;
public Animal(int c, int w)
{
color = c;
weight = w;
}
...
}
public class Dog extends Animal
{
private string breed;
public Dog()
{
breed = "Shelty";
}
}
My question is, what is the proper way to use color and weight in the dog class? Looking at the UML, I know I could put color and weight in the dog entity, but I understand that's optional. Would I just also have a private color and weight attribute in the dog class? Would I call the Animal constructor (which throws errors) What is the proper form here?