Document 11586314

advertisement
1.00 Tutorial 5
Agenda
•
•
•
•
Quiz 1 Results
Inheritance
Abstract Classes
Interfaces
Quiz 1 Results
• Mean: 87.2
• Std dev: 13.4
• n = 82 (# students)
• Quizzes will be available during office
hours this week, or you can pick
them up at Active Learning
session
Inheritance
• All Java® classes inherit implicitly from
Object
• Classes that inherit from other classes can
call methods from those classes, and can
access their data members and methods
(except for those that are private)
• Inheritance is useful because it allows you
to reuse functionality and to manage
complexity
Inheritance Example
class
int
}
class
int
}
class
int
}
A {
x;
B extends A {
y;
C extends B {
z;
Object
A
B
C
Inheritance Example
x
Instance of A
has just A's
fields. It's
type is A
(and Object).
Instance of B
has fields from
A and B. It's
type is A and B
(and Object).
x
y
Instance of C
has fields from
A, B and C. It's
type is A, B and
C (and Object).
Inheritance Example
static public void main(String[] args)
A aa = new A();
A ab = new B(); /* a B is also an A
A ac = new C(); /* a C is also an A
B bb = new B();
B bc = new C();
C cc = new C();
/* not legal -> an A is not a B! */
// B ba = new A(); /* compile error
}
{
*/
*/
*/
Abstract Classes
• Abstract classes are used to group related
subclasses.
• An abstract class cannot itself be
instantiated, but its concrete subclasses
can.
• Unlike interfaces, abstract classes can
have data fields and concrete methods
• Abstract classes also contain abstract
methods
Abstract Classes
// Security.java (abstract class)
public abstract class Security {
}
// Bond.java (concrete class)
public class Bond extends Security {
}
// Stock.java (concrete class)
public class Stock extends Security {
}
Example Abstract Class
abstract public class Security {
private String owner;
/* concrete methods */
public String getOwner() { return owner; }
public void setOwner(String o)
{ owner = o; }
/* abstract methods */
abstract public int getCurrentValue();
}
Questions
• Why did we choose to make Security
abstract? (Why not concrete?)
• Why couldn't we have used an interface
instead of an abstract class?
Interfaces (1)
• An interface is a collection of method
declarations which can be implemented by
classes
• An interface describes what classes
should do, without specifying how they
should do it
• The how will be defined in the classes that
implement the interface
• Each class defines the implementation
differently
Interfaces (2)
• A class can implement one or more
interfaces
• An interface can contain both methods
and constants
• To use an interface, a class must
– implement that interface
– define ALL methods in that interface
Example Interface
public interface HasColor {
public Color getBackgroundColor();
public void setBackgroundColor(Color bg);
public Color getForegroundColor();
public void setForegroundColor(Color fg);
}
// This interfaces allows us to add
// a background & foreground color to
// any class.
Example Interface Use
class Colorizer {
static void colorMeRed(HasColor c) {
c.setBackgroundColor(Color.red);
}
static void swapColors(HasColor c) {
Color tmp = c.getBackgroundColor();
c.setBackgroundColor(c.getForegroundColor());
c.setForegroundColor(tmp);
}
public static void main(String[] args) {
StudentWithColor s = new StudentWithColor();
StringWithColor t = new StringWithColor("HI");
colorMeRed(s);
swapColors(s);
t.setBackground(Color.green);
swapColors(t);}}
Example Interface Use
class StringWithColor extends String
implements HasColor {
private Color bgColor = Color.white;
private Color fgColor = Color.black;
public void setBackgroundColor(Color c) {
bgColor = c; }
public Color getBackgroundColor() {
return bgColor; }
public void setForegroundColor(Color c) {
fgColor = c; }
public Color getForegroundColor() {
return fgColor; } }
Java® is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries.
Download