The reading and homework for this week can be found here: Lab 11
Inheritance
superclass
subclass
extends
override
Polymorphism
dynamic method look-up
Abstract (vs concrete) class
final methods
protected access
inheritance hierarchy
overriding equals
instanceof operator
interface
comparable interface
Function objects
super()
in the first line of constructor
super.method_name()
anywhere in a subclass method.
Shape[] shapes = new Shape[3];
shapes[0] = new Circle(2);
shapes[1] = new Rectangle(2,3);
shapes[2] = new Square(5);
for (int i=0; i < shapes.length; i++) {
System.out.println("The area for " + shapes[i] + " is " + shapes[i].calcArea());
}
public abstract class Shape {
public abstract double calcArea();
...
Must define method in the subclass. Can't create a Shape object; can only create a subclass.
final
so that one can't override a method in the subclass.
Object
instanceof
operator can be used to check what type of object you have. For example, if you need to
cast an object. Should be used sparingly. Don't use as a way to implement different behavior, i.e. to bypass polymorphism.
implements
keyword.
Object Diagram Practice: Assume there is a Card class defined. Draw the object diagram after each line is executed:
Line 0: public static void main(String[] args) {
Line 1: Card[] myCards;
Line 2: myCards = new Card[4];
Line 3: for (int i = 0; i < myCards.length ; i++) {
Line 4: myCards[i] = new Card(i);
Line 5: System.out.print(myCards[i] + " ");
Line 6: }
Line 7: Card c = myCards[3];
Line 8: myCards[0] = myCards[1];
Line 9: myCards[2] = null;
Line 10: myCards[3] = null;
Line 11: myCards[3] = c;
Line 12: System.out.println("\n");
Line 13: for (int i = 0; i < myCards.length ; i++) {
Line 14: System.out.print(myCards[i] + " ");
Line 15: }
Line 16: }
Assuming that the output from Line 5 gives the A♠, 2♠ 3♠ 4♠,
what is the output at Line 14?
We will go over how to get your database to read from a file. See WISE for instructions.
Try the Chp 9 programming exercises: P9.8 and P9.9 on page 459
You may begin the new programming assignment on inheritance: assignment (pdf). This will be due sometime after the exam. Here is some code that you will also need (see instructions in assignment): the card class and main testing code.