










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: Project; Professor: Cleaveland; Class: OBJECT-ORIENTED PROG I; Subject: Computer Science; University: University of Maryland; Term: Fall 2006;
Typology: Study Guides, Projects, Research
1 / 18
This page cannot be seen from the preview
Don't miss anything!











11/17/
CMSC 131 Fall 2006Rance Cleaveland
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Project #7 Assigned!
Project due Wednesday, 11/29 at 11 pm
Project is
closed
You must complete the project by yourself
Assistance can only be provided by teaching assistants(TAs) and instructors
You must not look at other students' code
Start now!
Read entire assignment from beginning to end beforestarting to code
Check out assignment now from CVS
Follow the instructions
exactly
, as much of grading is
automated
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Recap ^
Inheritance occurs when one class (
derived class, subclass
) is
defined from another class (
base / parent class, superclass
).
^
To derive a class D from a base class B, use:
public class
D^
extends
B^
{ …
}
^
Derived class inherits all instance variables, methods from baseclass. It can also define new instance variables, methods ^
In derived-class constructor,
super
(
…
)
can be used to invoke
constructor from base class ^
Derived class can explicitly refer to entities from base class using super
, e.g.
super.toString(
)
^
Polymorphism
: object in derived class can be used anywhere
base class is expected (a
Student
“
is a
”^
Person
!)
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
University Person Recap
class
:^
Person
instance variables
:
String
name
String
idNum
methods
:
Person( … )
[various]
String
getName( )
String
getIdNum( )
void
setName(
String
)
void
setIdNum(
String
)
String
toString( )
boolean
equals(
Person
)
class
:^
Student
instance variables
:
int
admitYear double
gpa
methods
:
Student( … )
[various]
int
getAdmitYear( ) double
getGpa( )
void
setAdmitYear(
int
)
void
setGpa(
double
)
String
toString( )
boolean
equals(
Student
)
extends Person
class
:^
Faculty
instance variables
:
int
hireYear methods
:
Faculty( … )
[various]
int
hireYear( ) void
setHireYear(
int
)
String
toString( )
boolean
equals(
Student
)
extends Person
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
super
vs.
this
^
super
: refers to the base class
^
^
^
this
: refers to current class / object
^
^
^
Can only be done within a constructor ^
Must be the first statement of the constructor ^
Example public
Faculty(
Faculty
f
this(
f.getName(
f.getIdNum(
f.hireYear
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Method Overriding ^
^
public
class
Person
… public
String
toString(
} public
class
Student
extends
Person
… public
String
toString(
} Student
bob
new
Student("Bob
Goodstudent","123-45-6789",2004,4.
System.out.println(
"Bob's
info:
bob.toString(
Overrides base-classdefinition of this method
Since bob is Student,Student toString used
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Early vs. Late Binding ^
Consider:
Faculty
carol
new
Faculty("Carol
Tuffteacher","999-99-9999",
Person
p
carol;
System.out.println(
p.toString()
^
Which version of
toString
Person
or
Faculty
^
p^
is declared to be of type
Person
^
Therefore, the
Person
version of
toString
is used
^
The object to which
p
refers was created as
Faculty
object
^
Therefore, the
Faculty
version of
toString
is used
^
Java uses late binding
(C++ uses early binding)
^
^
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Disabling Overriding with final
Sometimes you do not want to allow methodoverriding.
Correctness Redefining a method for a derived class might break it
Efficiency ^
Late binding is less efficient than early binding ^
If you know that no subclass will redefine your method, youcan force early binding by disabling overriding
We can disable overriding by declaring a method tobe
final
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Shadowing ^
Can we override instance variables just like methods? ^
Yes, but be careful! ^
^
^
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Inheritance and
private
^
Student
inherits all private data (
name
and
idNum
) from
Person
^
However, private members of base class cannot be accessed directly
public
class
Student
extends
Person
… public
void
someMethod(
name
“Mr.
Foobar”;
Illegal!
} public
void
someMethod2(
setName(
“Mr.
Foobar”
^
Why? ^
^
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Package access
When a class member is not given any accessmodifier (
private, public, protected
) it is
said to have
package access
Such members are accessible any class in the samepackage
Example:
package
person;
public
class
Person
{
int computeAge
()
{
…
}
//
Package
access
}
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Which Access Policy To Use? ^
public ^
^
^
private ^
^
^
protected ^
Helper / utility methods that may be useful in subclasses ^
Some style gurus discourage
protected
.^
They consider package
access safer, since trouble can be localized to current package.
^
Package ^