Today I began the Java lecture series about inheritance. After having completed the first lecture, I feel quite confident in my understanding of inheritance as a theory. It basically means that certain classes can be like subclasses, or extend upon higher-level classes.

Below is the main class. In it, I am basically testing the methods in the Animal and Dog classes. I create a new instance of class animal, declaring its specified variables. It prints out “animal.eat called” and I think that’s because there’s a method in the animal class called eat which prints out that string, for testing purposes. Similarly, it also prints out “dog.eat called” and “dog.chew called”. Does this happen because instances of these classes are declared, so methods in those classes automatically run?

public class Main {

    public static void main(String[] args) {
   Animal animal = new Animal("Animal", 1, 1, 5, 5);

    Dog dog = new Dog("Yorkie", 8, 20, 2, 4, 1, 20, "Long silky");

    dog.eat();

    }

    private void chew(){
        System.out.println("Dog.chew() called");
    }

}

Below we have the animal class. It is the class which will be extended by the dog class. It has five variables. It uses “this.name” syntax which was never really explained. I suppose it’s just setting the value of those variables to what you declare when declaring the new instance of the animal. Below are two methods, one of which prints out a line. Beneath that are the getter methods, generated by IntelliJ.

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(){

    }

    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 is where the new subject matter is. I declared a new class called dog which extends animal. you can see that the keyword/syntax here is “extend”. It extends it by adding further variables. Interestingly, it removes three of the variables in the dog method constructor and then in the super part sets them as defaults. It uses “this.eyes” syntax as well.  There’s a new method which prints out “dog.chew called”.

public class Dog extends Animal{

    private int eyes;
    private int legs;
    private int tail;
    private int teeth;
    private String coat;

    public Dog(String name, int size, int weight, int eyes, int legs, int tail, int teeth, String coat) {
        super(name, 1, 1, size, weight);
        this.eyes = eyes;
        this.legs = legs;
        this.tail = tail;
        this.teeth = teeth;
        this.coat = coat;
    }
    private void chew(){
        System.out.println("Dog.chew called");

    }

    @Override
    public void eat() {
        System.out.println("Dog.eat called");
        chew();
        super.eat();
    }
}

I used a tool in the IDE called generate -> override method. I chose the eat method in animal. I’m not entirely sure how that works but in theory, it contains more granularity than the class’s method which it inherits from.

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