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.
}
extends
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
public variables are directly accessible from
all classes; private variables are directly
accessible only within the class in which they are defined
ConferencePaper
cannot directly assign to, retrieve from or compute with
title, authorName or yearOfPub
protected makes a variable/method
visible to subclasses and (advanced) to any classes in the same
package
protectedclass 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
ConferencePaper's own instance variables
– when might you make them protected?
protected, keeping
the superclass variables private, thus reducing
inter-dependencies between classes – but, then, how
can a subclass access the inherited variables?
Publication's toString can be overridden by a
definition in ConferencePaper
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.
}
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.
}
Publication p1 = new Publication("Rabbit plague",
"Dr Mick Somatosis", 2011);
ConferencePaper p2 = new ConferencePaper("Chicago weather patterns",
"Dr Wendy City", 2014, "World Climate Conference", 23, 45);
Publication would have a constructor like this:
public Publication(String title, String authorName, int yearOfPub) {
this.title = title;
this.authorName = authorName;
this.yearOfPub = yearOfPub;
}
ConferencePaper constructor?
public ConferencePaper(String title, String authorName,
int yearOfPub, String confName, int startPage, int endPage) {
this.title = title;
this.authorName = authorName;
this.yearOfPub = yearOfPub;
this.confName = confName;
this.startPage = startPage;
this.endPage = endPage;
}
ConferencePaper should make use of the one in
PublicationConferencePaper's constructor must call
a constructor in Publication
super:
public ConferencePaper(String title, String authorName,
int yearOfPub, String confName, int startPage, int endPage) {
super(title, authorName, yearOfPub);
this.confName = confName;
this.startPage = startPage;
this.endPage = endPage;
}
supersuper, which applies only to the very first
statement in a constructor body, with the other use of super
super, Java implicitly puts one in for you anyway
super()class BarbieDoll(Toy, Girl):
# etc.