Download Lecture 21

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lecture 21
Inheritance
One class is defined as a subclass of another class by having it
extend the other class.
class Sub extends Base
{ // definition of class Sub
}
 Sub is a subclass of Base.
 Base is the superclass of Sub.
Base b = new Base();
b
Sub s = new Sub();
s
The object to which s refers is both a Sub and a Base object.
Every Sub object "is-a" Base object also.
Upcasting
b = s;
Going the other way is called downcasting, requires
an explicit cast, and may be illegal.
Copyright © 2001 by Ken Slonneger
21-1
A Subclass May
 Have new instance variables that extend the state.
 Hide instance variables in its base class by providing
new declarations for them (rare).
 Have new instance methods extending the behavior.
 Override instance methods in its base class by defining methods
with the same signature, but with new behavior.
These are the ways that Sub may extend Base.
Important
An object of a subclass will have copies of all the instance variables
defined inside its class and inside each of its superclasses.
Visibility Modifiers
 protected members are visible inside their class, inside any
subclass of their class, and from other classes in the same
package (file).
Class Definitions without extends
A class definition without an extends clause automatically becomes
a subclass of java.lang.Object, the superclass of
all classes in Java.
class Domino
{
is in fact
Copyright © 2001 by Ken Slonneger
21-2
class Domino extends Object
{
Copyright © 2001 by Ken Slonneger
21-3
this
1. Inside a constructor, this is used to invoke another
constructor of the same class.
It must be the first command of the constructor.
this (x, y);
2. In an instance method, this refers to the object, called the
receiver, that is currently executing the method.
Useful when a local variable hides an instance variable
of the object.
super
1. Inside a constructor, super is used to invoke a constructor
of the parent class.
It must be the first command of the constructor.
super (a, b, c);
2. In an instance method, super can be used to access instance
variables and methods belonging to the
superclass of the current object's class.
super.upMethod();
// Instance method in parent
Useful when a subclass overrides a method in the parent.
Copyright © 2001 by Ken Slonneger
21-4
Constructors
 If no constructor is written in a class, Java provides a default
(no parameters) constructor.
 If any constructor is defined in a class, Java provides no
constructor by default.
In a subclass constructor, super() is called automatically if not
done explicitly.
super() calls the superclass default constructor, provided
by Java if no other constructor exists or a no-parameter
constructor provided by the programmer otherwise.
Beware:
Adding a constructor with a parameter will make
invocations of a parameterless constructor illegal
unless you have defined one.
Example: Colored Domino
Define a subclass of Domino that provides colored dominoes
indicated by an additional instance variable of type String in
the subclass.
Constructors
ColoredDomino(int, int, boolean, String)
ColoredDomino()
Copyright © 2001 by Ken Slonneger
21-5
Instance Methods
getColor()
a new method
toString()
overrides parent method
equals()
overloads grandparent method
Observe how super is used to invoke behavior in
the superclass, Domino.
The Subclass
class ColoredDomino extends Domino
{
String color;
// Instance Variable
// Constructors
ColoredDomino(int val1, int val2, boolean up, String c)
{
super(val1, val2, up);
color = c;
}
ColoredDomino()
{
this(0, 0, false, "Black");
}
Copyright © 2001 by Ken Slonneger
21-6
// Instance Methods
String getColor()
{
return color;
}
// accessor
public String toString()
{
String dom = super.toString();
return dom + " " + color;
}
boolean equals(ColoredDomino otherDomino)
{
return getHigh() == otherDomino.getHigh()
&& getLow() == otherDomino.getLow()
&& color.equals(otherDomino.color);
}
}
Template for ColoredDomino
getLow()
getColor()
getHigh()
spots1
spots2
faceUp
color
toString()
Copyright © 2001 by Ken Slonneger
equals()
21-7
matches()
Using ColoredDomino
public class UseColoredDomino
{
public static void main(String [] args)
{
ColoredDomino cd1 = new ColoredDomino(3, 5, true, "White");
ColoredDomino cd2 = new ColoredDomino(5, 3, false, "Red");
Domino d3 = new Domino(0, 9, true);
Domino d4 = new ColoredDomino(5, 3, true, "Blue");
System.out.println("Domino 1: " + cd1);
System.out.println("Domino 2: " + cd2);
System.out.println("Domino 3: " + d3);
System.out.println("Domino 4: " + d4);
System.out.println("Matches 1 and 2: " + cd1.matches(cd2));
System.out.println("Equals 1 and 2: " + cd1.equals(cd2));
System.out.println("Matches 4 and 1: " + d4.matches(cd1));
System.out.println("Matches 4 and 3: " + d4.matches(d3));
}
}
Output
Domino 1: <3, 5> UP White
Domino 2: <3, 5> DOWN Red
Domino 3: <0, 9> UP
Domino 4: <3, 5> UP Blue
Matches 1 and 2: true
Equals 1 and 2: false
Matches 4 and 1: true
Matches 4 and 3: false
Copyright © 2001 by Ken Slonneger
21-8