Today we built on the previous lecture about inheritance in Java. I learned about how there can be base classes which are extended by other classes. Designing this seems to be like modeling classifications in real life.

The base class is also known as the superclass. The class which extends it using the extends keyword is called the subclass.

public class Animal {
    private String name;
    private int brain;
    private int body;
    private int size;
    private int weight;

    public Animal(String name, int brain, int body, int size, int weight) {
        this.name = name;
        this.brain = brain;
        this.body = body;
        this.size = size;
        this.weight = weight;
    }

    public void eat(){
        System.out.println("Animal.eat called");
    }

    public void move(int speed){
        System.out.println("Animal.move called. Animal is moving at " + speed);
    }

    public String getName() {
        return name;
    }

    public int getBrain() {
        return brain;
    }

    public int getBody() {
        return body;
    }

    public int getSize() {
        return size;
    }

    public int getWeight() {
        return weight;
    }
}

Here you have the superclass of animal. It is extended by the subclass of fish and referred to in the main class. You can imagine how this was designed: they first thought about characteristics which all animals have. These are stored as variables.

In the next code block, there is the constructor. For my money, this is because the variables are private. And it shows the method for declaring new animals? I don’t fully understand why they have ‘this.body’ etc.

There are two methods which the developer decided were the actions which all animals take. Eat and move. These are extended upon in subclasses such as dog and fish.

Then there are getter methods for each of the variables declared for this class. So this is because the variables are private I suppose. Now the main can access variables within the animal class?

public class Fish extends Animal {

    private int gills;
    private int eyes;
    private int fins;

    public Fish(String name, int size, int weight, int gills, int eyes, int fins) {
        super(name, 1, 1, size, weight);
        this.gills = gills;
        this.eyes = eyes;
        this.fins = fins;
    }

    private void rest(){
    }

    private void moveMuscles(){
    }

    private void moveBackFin(){
    }

    private void swim(int speed){
        moveMuscles();
        moveBackFin();
        super.move(speed);
    }
}

Here we have the class fish which extends the animal class and therefore is a subclass. Notice the extends keyword in the class declaration.

It has variables which fishes have but animals don’t necessarily. Thus any instance of fish declared in main will have animal characteristics (from the animal class) and fish characteristics (from the fish class).

I used Generate to create a constructor. Was this second code block how it came out? I don’t remember. I added default values for body and brain for the superclass variables and removed those from the first line of the constructor. So you won’t have to add the number of bodies or brains when declaring a fish now: they’ll default to 1.

Again, I don’t fully understand what the lines where it says ‘this.gills’ etc are for. What would happen if you left this out?

I notice there aren’t getter methods. Is that a mistake by the lecturer? Or are they not required for subclasses?

Next there are various methods which describe what a fish ‘does’. They rest, move their muscles, move their back fin and swim.

The last method is a bit tricky. Apparently swim accept the integer of speed, because that is a relevant descriptor of swimming. I don’t why the other methods are there although I logically understand those actions are involved in the larger action of swimming.

I do see how swimming is a type of movement by animals. So I guess that having ‘super.move();’ there indicates that this is like an extension of the animal.move method. The animal.move method also accepts an integer of speed, is that why swimming does as well?

I feel a bit like I’m trying to drink out of a firehose here. Taking the time to carefully read and analyse this code is helping me a great deal to understand it, however, there are still some gaps in my knowledge mostly related to syntax. I will stay the course and hopefully have my questions answered in time.

Remaining questions:
– Why do you need lines such as ‘this.name’ in the constructor?
– I’m not sure what’s going on in the swim method where you’re extending a super – method but it also refers to other methods within the fish class before that. How does this all fit together?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s