Java UML Diagrams to Classes

Myka Thomas :

UML Diagram

I am brand new to Java, though I've taken other programming courses. I'm really struggling with the class concept from a UML diagram. I have a parent class and two child classes. My assignment is to create this class structure. I am struggling with the concept of class and relationships in general though.

Example: If my parent class is "Animal" and my child classes are "Monkey" and "Bear" - if the only choices that will be implemented are "Monkey" and "Bear", this makes the class "Animal" an abstract class distinction as there will never be just "Animal", it will always be a "Monkey" or a "Bear".

So, how would I create three files (Animal.java, Monkey.java, Bear.java) if Animal is abstract? I understand that the properties and traits of Animal are inherited by Monkey and Bear. Assuming that I have, for instance, name and age of the animal as attributes and getters and setters for each - if name and age of "Animal" class are private (code below), how does "Bear" class pick up the name and age if it is in its own java file/class? My code is below...

Animal.java

public class Animal {
    private String animalName;
    private int animalAge;

    public void setName (String name) {
        animalName = name;
    }

    public void setAge (int age) {
        animalAge = age;
    }

    public static String getName() {
        return animalName;
    }

    public static String getAge() {
        return animalAge;
    }
}

Bear.java

public class Bear {
    public int weight;

public static int weight() {
     return weight;
    }
}
// This is where I get stuck & don't know where to go from here. 

I understand that I am creating an object "Bear" which is part of the class "Animal", but as Bear is its own class, how does "Bear" get its assigned values from Animal? I can assign "Animal" and "Bear" with default values, but my brain cannot put together how they're talking to one another.

This might be outside the scope of this forum but my professor is unresponsive and I've read through books, ebooks, the course material, and several lectures and this is just not coming to me and I'm two weeks behind at this point trying to understand this concept so I can move forward with the actual code in the program.

Thanks in advance.

duffymo :

You forgot to do this:

public class Bear extends Animal {
    // ...
}

I would recommend that you add constructors:

public class Animal {
    private String name;
    private int age; 

    public Animal(String name, int age) {
       this.name = name;
       this.age = age;
    }

    public String getName() [ return this.name; }
    public int getAge() { return this.age; }
}

public class Bear extends Animal {
    private int weight;

    public Bear(String name, int age, int weight) {
        super(name, age);
        this.weight = weight;
    }

    public int getWeight() { return this.weight; }
}

You can do this, because Bear IS-A Animal:

Animal b = new Bear("Smokey", 10, 300);
System.out.println(b.getName()); // prints "Smokey"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=327180&siteId=1