CS2514

Introduction to Java

Dr Derek Bridge

School of Computer Science & Information Technology

University College Cork

A class hierarchy

class Animal {
            
    protected String breed;
    
    public Animal(String breed) {
        this.breed = breed;
    }
    
    public String getBreed() {
        return breed;
    }
    
    public void talk() {
        System.out.print("");
    }
    
}

A class hierarchy

class Cow extends Animal {
            
    public Cow() {
        super("cows");
    }
    
    public void talk() {
        System.out.print("Moo");
    }
 
}
class Pig extends Animal {
            
    public Pig() {
        super("pigs");
    }
    
    public void talk() {
        System.out.print("Oink");
    }
    
}
class Aardvark extends Animal {
            
    public Aardvark() {
        super("aardvarks");
    }
    
    public void talk() {
        System.out.print("Snaffle");
    }
    
}

Assignment

Polymorphism

Method binding

Dynamic method binding

Illustrating all of this

Animal a = new Cow();
a.talk();
a = new Pig();
a.talk();
System.out.println(a.getBreed());
a = new Animal("Centipede");
a.talk();
System.out.println(a.getBreed());

Although variable a is of type Animal, Java didn't repeatedly execute methods in Animal.
During the program, a references various different classes of object and the method that was executed depended on what a was referencing

The power of polymorphism and dynamic method binding

Teaching the world to sing

In the following, livestock is a polymorphic data structure: it can hold objects of different forms – objects that are any subclass of Animal

public class SingAlong {

    public static void main(String[] args) {
        Animal[] livestock = new Animal[3];
        livestock[0] = new Cow();
        livestock[1] = new Pig();
        livestock[2] = new Aardvark();
        
        for (int i = 0; i < livestock.length; i = i + 1) {
            System.out.print("Old MacDonald had a farm. ");
            System.out.println("Eee Eye, Eee Eye, Oh!");
            System.out.print("And on that farm he had some ");
            System.out.print(livestock[i].getBreed() + ". ");
            System.out.println("Eee Eye, Eee Eye, Oh!");
            
            for (int j = i; j >= 0; j = j - 1) {
                Animal a = livestock[j];
                System.out.print("With a '");
                a.talk();
                a.talk();
                System.out.print("' here, and a '");
                a.talk();
                a.talk();
                System.out.println("' there.");
                System.out.print("Here a '"):
                a.talk();
                System.out.print("', there a '");
                a.talk();
                System.out.println("'.");
                System.out.println("Everywhere a '");
                a.talk();
                a.talk();
                System.out.println("'.");
            }
            
            System.out.print("Old MacDonald had a farm. ");
            System.out.println("Eee Eye, Eee Eye, Oh!");
            System.out.println();
        }
    }
    
}

Discussion

Advanced point: An admission