Announcements
• Quiz on Friday, 9/28
• Prog1: see email, see me as soon as possible with questions/concerns
• Prog2: do not add any public methods to any classes
Using lessThan()public boolean lessThan(Date rhs) { . . . }. . .
public static void main(String args[]){ Date fall2012Start = new Date(2012, 9, 4); Date fall2012End = new Date(2012, 12, 21); // How to count the number of days in the semester?
Date date = new Date(fall2012Start); int count; for (count = 1; date.lessThan(fall2012End); count++) date = date.tomorrow(); System.out.println("Days in 2012 fall semester: " + count);}
Implementing lessThan()public boolean lessThan(Date rhs){ if (year < rhs.year) return true; if (year > rhs.year) return false; if (month < rhs.month) return true; if (month > rhs.month) return false; return day < rhs.day;}
Using addDays()public void addDays(int numDays) { . . . } // modifies the object
public static void main(String args[]){ Date prog2Grace = new Date(2012, 10, 1);
// Some students want a two-day extension: prog2Grace.addDays(2); }
Implementing addDays()public void addDays(int numDays){ // How to do the add? // Wrong: this.day += numDays
Date date = new Date(this); // make a copy of current Date for (int count = 0; count < numDays; count++) date = date.tomorrow(); this.year = d.year; this.month = d.month; this.day = d.day;}
Class Date is no longer “immutable”!
Immutable objects
Definition: objects whose state (i.e., the object's data) cannot change after construction.
For now, we will say that Date, without addDays(), is immutable, although technically, it is not.
Example: Strings are immutable
A new keyword: extendspublic class A{ private int x = 0;
public void doIt(int y) { x += y; }
public void print() { System.out.println(x); }}
public class B extends A{ // empty!}
public static void main(String args[]){ B b = new B();
// The method is inherited: b.doIt(10);
// x is printed out! b.print();}
Inheritance examle
UML: Class B inherits from class A
A B
Inheritance represents an “is a” relationship
public class A
{
. . .
}
public class B extends A
{
. . .
}
Is-a relationship
• public class Student extends Person{ . . . }
• public class Square extends Rectangle{ . . . }
• public class Civic extends Car { . . . }
An example project// Assume Class A and B exist and B a sub-class of A
public class C
{
public static void main(String args[])
{
A a = new A();
B b = new B();
a.doIt(5);
a.print();
b.doIt(10);
b.print();
}
}
Why use inheritance?
• Basic idea: when you want to create a new class and there is already a class that includes some of the code you want
• Simply extend the existing class!
• Can reuse the fields and methods in the existing class
• Less typing!
Subclass and superclass
• A class that is derived from another class is called a subclass (a.k.a., child class)
• The class from which a class is derived is called a superclass (a.k.a., parent class)
Subclasses and constructors
• A subclass inherits all members of its parent
• Constructors are (technically) NOT members, so they are not inherited by subclasses
• The constructor of the superclass can (and MUST) be invoked from the subclass using super()– Must be invoked on the first line of the
constructor!