

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Gries; Class: Introduction to Computing Using Java; Subject: Computer Science; University: Cornell University; Term: Fall 2008;
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


1
2
Help: Get it now if you need it!!
website and click "Staff-info".
click on "student services". On the page that comes up, click
on ”Engineering Learning Initiatives (ELI.) in the left column,
upper part. Then, click on "peer tutoring" in the left column.
3
Content of this lecture
This lecture contains some final miscellaneous points to round out
your knowledge of classes and subclasses. There are a few more
things to learn after this, but we will handle them much later.
Sec. 4.1 and 4.1.1: pp. 142–
Sec. 3.1.3, pp. 110–112.
4
a
Object
name “Gries”^ start 1969
salary 50,000.
getName() setName(String n) …
toString()
equals(Object) toString()
Employee
Sec. 4.1,
page 142
This class is on
page 105 of the
text.
5
Purpose of super and this
this refers to the name of the object in which it appears.
super is similar but refers only to components in the partitions above.
/** = String representation of this
Employee */
public String toString() {
return this. getName() + ", year ” +
getStart() + ", salary ” + salary;
ok, but unnecessary
/** = toString value from superclass */
public String toStringUp() {
return super .toString();
necessary
Sec. 4.1, pages
a
Object
name “Gries”
start 1969
salary 50,000.
getName()
setName(String n) {…}
toString()
toStringUp() { …}
equals(Object)
toString()
Employee
6
A second constructor in Employee
Provide flexibility, ease of use, to user
/** Constructor: a person with name n, year hired d, salary 50,000 */
public Employee(String n, int d) {
name= n; start= d; salary= 50000;
Second constructor;
salary is always 50,
/** Constructor: a person with name n, year hired d, salary 50,000 */
public Employee(String n, int d) {
this (n, d, 50000);
Another version of second
constructor; calls first constructor
Here, this refers to the other constructor.
You HAVE to do it this way
Sec. 3.1.3,
page 110
7
a
Object
name “Gries”^ start 1969
salary
Employee(String, int)
toString() getCompensation()
toString() …
Employee
Executive bonus
Executive(String, int, double)
getBonus() getCompensation()
toString()
Calling a superclass
constructor from the
subclass constructor
public class Executive extends Employee {
private double bonus;
/** Constructor: name n, year hired
d, salary 50,000, bonus b */
public Executive(String n, int d, double b) {
super (n, d);
bonus= b;
The first (and only the first) statement in
a constructor has to be a call to a
constructor of the superclass. If you
don’t put one in, then this one is
automatically used:
super ();
Sec. 4.1.3, page 147
8
Anglicizing an Integer
9