Object-Oriented Programming with Java, part I + II

cc

This material is licensed under the Creative Commons BY-NC-SA license, which means that you can use it and distribute it freely so long as you do not erase the names of the original authors. If you make changes in the material and want to distribute this altered version of the material, you have to license it with a similar free license. The use of the material for commercial use is prohibited without a separate agreement.

Authors: Arto Hellas, Matti Luukkainen
Translators to English: Emilia Hjelm, Alex H. Virtanen, Matti Luukkainen, Virpi Sumu, Birunthan Mohanathas, Etiënne Goossens
Extra material added by: Etiënne Goossens, Maurice Snoeren, Johan Talboom

The course is maintained by Technische Informatica Breda


Extending Classes

In java it’s possible to extend classes to add functionality. By extending a class, you take over all the attributes, methods and constructors of the other class. By reusing the class in the other class, you only have to write this code once, and can reuse it in other classes, as if it were part of that class. An example could be classes about animals.

In the animal kingdom are lots of different animals, like a Sparrow, Seagull, Cow and Pig. All of these animals can make sounds. We can group these animals together though, into Birds and Mammals. A major difference between these 2 groups of animals, is that birds can fly. We can model this in the following scheme

animals

In this example, all animals can make a sound, so we will want to share this functionality among all classes

class Animal {
    private String sound;

    public Animal() {
        this.sound = "";
    }

    public void setSound(String sound) {
        this.sound = sound;
    }

    public void makeSound() {
        System.out.println(this.sound + "!!!!");
    }
}

With this class, all the animals share the functionality to make a sound. We can extend this to groups of animals, using the extends keyword. We can add functionality to the Bird and Mammal classes, birds can fly, while mammals can walk (birds usually hop instead of walking)

class Bird extends Animal {
    public Bird() {
    }

    public void fly() {
        System.out.println("The bird is flying");
    }
}

class Mammal extends Animal {
    public Mammal() {
    }

    public void walk() {
        System.out.println("The mammal is walking");
    }
}

Now we can make some animals to go with these classes.

class Sparrow extends Bird {
    public Sparrow() {
        this.setSound("chirp");
    }
}

class Seagull extends Bird {
    public Seagull() {
        this.setSound("Squaaa");
    }
}

class Cow {
    public Cow() {
        this.setSound("moo");
    }
}

class Pig {
    public Pig() {
        this.setSound("oink");
    }
}

Now we can start using these classes. All classes can only do the things they are supposed to do


public static void main(String[] args) {
    Cow cow = new Cow();
    cow.makeSound();
    cow.walk();
    //cow.fly();        cows can't fly

    Sparrow spwarrow = new Sparrow();
    sparrow.makeSound();
    sparrow.fly();
    //sparrow.walk()    sparrows can't walk
}
moo!!!!
the mammal is walking
chirp!!!!
The bird is flying

Superclass

The class that is being extended, is called the superclass. The class that is extending, is called the subclass

In the example of the animals,

In java, the superclass can be used by using the super keyword. With super, we can call the constructor of the superclass, or call a method in a superclass. Methods can also be called using the this keyword.

Constructors in the superclass

If a superclass has a constructor with parameters, and no constructor without parameters, the superclass’s constructor must be called using super(...) in the constructor of the subclass.

By calling the super constructor, we can shorten the example of the animal kingdom, by adding the sound to the constructor of the Animal class.

class Animal {
    private String sound;
    
    public Animal(String sound) {
        this.sound = sound;
    }
}

class Bird extends Animal {
    public Bird(String sound) {
        super(sound);
    }
}

class Mammal extends Animal {
    public Mammal(String sound) {
        super(sound);
    }
}

class Sparrow extends Bird {
    public Sparrow() {
        super("chirp");
    }
}

class Seagull extends Bird {
    public Seagull() {
        super("Squaaa");
    }
}

class Cow {
    public Cow() {
        super("moo");
    }
}

class Pig {
    public Pig() {
        super("oink");
    }
}

By adding the sound as a parameter of the Animal class constructor, we enforce that the sound is always set. This makes for a better design, as this makes it hard to make an Animal that does not have a sound because the programmer forgot to add a sound.

Note: The super constructor should always be called before all other code in a constructor. If you do not put the super constructor first, java will throw an error while compiling.

Exercise specializing-classes-1: People

We’ve seen a lot of Person classes so far. In this exercise we’re going to work on printing different kinds of students, and reusing existing code. We will start off with a Person class, that stores a name and age. Then we’ll extend this class for Students and Teachers, and add extra functionality

specializing-classes-1.1 Person class

Write a class Person with the attributes String name and int age. Also add the constructor public Person(String name, int age)

specializing-classes-1.2 Printing a person

Add a method printPerson to the Person class that prints the name and age of this person

public static void main(String[] args) {
   Person person = new Person("John Doe", 35);
   person.printPerson();
}
Name: John Doe
Age: 35

specializing-classes-1.3 Extending the Person to a student

Make a new class Student, that extends the Person class and adds an attribute int studentNumber. Also add the constructor public Student(String name, int age, int studentNumber)

specializing-classes-1.4 Adding a student printing method

Add a method printStudent to the Student class that prints the name, age and student number of this person

public static void main(String[] args) {
   Student student = new Student("John Doe", 35, 1337);
   student.printStudent();
}
Name: John Doe
Age: 35
Student number: 1337

Make sure you reuse the code of the printPerson() method in the printStudent method. do not copy/paste the code from Person to Student