3

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?

1
  • you want to set the value of attributes of class Animal from Dog class or want to access it in Dog class ? Commented Mar 4, 2014 at 6:43

4 Answers 4

9

The Dog class's responsibility should be to initialize whatever new functionality it is adding, and let the super class initialize all properties that the Dog class has inherited.

You could do something like this:

public class Dog extends Animal
{
    private String breed;

    public Dog(int color, int weight, String breed)
    {
        super(color,weight); //let superclass initialize these.
        this.breed = breed;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

You use the keyword super.

public Dog(int c, int w){
    super(c,w);
    breed = "Shelty";
}

Comments

2

Additional to answer of manish, You can also write a NoArg constructor to provide default initialization values.

public class Dog extends Animal
{
    private String breed;

    public Dog(int color, int weight, String breed)
    {
        super(color,weight); //let superclass initialize these.
        this.breed = breed;
    }

    // Default initialization if required
    public Dog() {
        this ( 0, 0, "Shelty")
    }
}

Comments

1
public class Dog extends Animal
{
    private String breed;

    //Declaring default constructor is a good practice

    public Dog() 
    {
      //Some default values
       int color=3,weight =8;
       super(color,weight);
       this.breed = "pug";
    }

    public Dog(int color, int weight, String breed)
    {
       super(color,weight); 
       this.breed = breed;
    }
}

2 Comments

May we know the reason for providing same ans again ?
@KishanSarsechaGajjar It might be due to You're editing your answer in the same time where i post my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.