CS2514

Introduction to Java

Dr Derek Bridge

School of Computer Science & Information Technology

University College Cork

Class hierarchies

Example class hierarchy

Mammals form a class hierarchy

Example class hierarchy

Bank accounts form a class hierarchy

Example class hierarchy

University members form a class hierarchy

Our main example

Publications form a class hierarchy

Duplicating the variables and methods: usually a bad approach

class Publication {
            
    private String title;
    private String authorName;
    private int yearOfPub;
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getTitle() {
        return title;
    }
    
    public String toString() {
        return author + "(" + yearOfPub + "): " + title;
    }
    
    // etc.
}
class ConferencePaper {
            
    private String title;
    private String authorName;
    private int yearOfPub;
    private String confName;
    private int startPage;
    private int endPage;
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getTitle() {
        return title;
    }
    
    public String toString() {
        return author + "(" + yearOfPub + "): " + title + ", in " + confName + ", pages " + startPage + "-" + endPage;
    }
    
    
    // etc.
    
    public void setConfName(String confName) {
        this.confName = confName;
    }
    
    public String getConfName() {
        return confName;
    }
    
    // etc.
}

Inheritance: a better approach

Using inheritance

class Publication {
            
    private String title;
    private String authorName;
    private int yearOfPub;
    
        
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getTitle() {
        return title;
    }
    
    public String toString() {
        return author + "(" + yearOfPub + "): " + title;
    }
    
    // etc.
}
class ConferencePaper extends Publication {
            
    private String confName;
    private int startPage;
    private int endPage;
    
    public void setConfName(String confName) {
        this.confName = confName;
    }
    
    public String getConfName() {
        return confName;
    }
    
    // etc.
}

– objects of class ConferencePaper still have 6 instance variables and similarly with instance methods

Visibility of inherited variables

Using protected

class Publication {
            
    protected String title;
    protected String authorName;
    protected int yearOfPub;
    
        
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getTitle() {
        return title;
    }
    
    public String toString() {
        return author + "(" + yearOfPub + "): " + title;
    }
    
    // etc.
}
class ConferencePaper extends Publication {
            
    private String confName;
    private int startPage;
    private int endPage;
    
    public void setConfName(String confName) {
        this.confName = confName;
    }
    
    public String getConfName() {
        return confName;
    }
    
    // etc.
}

– objects of class ConferencePaper now have 6 visible instance variables and similarly with instance methods

Questions

I don't want to inherit all the instance variables!

I don't want to inherit all the instance methods!

Example of overriding

class ConferencePaper extends Publication {
            
    private String confName;
    private int startPage;
    private int endPage;
    
    public void setConfName(String confName) {
        this.confName = confName;
    }
    
    public String getConfName() {
        return confName;
    }
    
    public String toString() {
        return author + "(" + yearOfPub +  "): " + title + ", in " + confName + ", pages " + startPage + "-" + endPage;
    }
    
    // etc.
}

A slightly different example of overriding

Publication's toString can still be called from ConferencePaper's instance methods by using super.toString() (if it is visible)

class ConferencePaper extends Publication {
            
    private String confName;
    private int startPage;
    private int endPage;
    
    public void setConfName(String confName) {
        this.confName = confName;
    }
    
    public String getConfName() {
        return confName;
    }
    
    public String toString() {
        return super.toString() + ", in " + confName  + ", pages " + startPage + "-" + endPage;
    }
    
    // etc.
}

Inheritance facilitates re-use

What about constructors?

Constructors are not inherited!

Calling the superclass constructor

More details on super

Advanced point: Multiple inheritance