Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER 2430 Object Oriented Programming and Data Structures I

Similar presentations


Presentation on theme: "COMPUTER 2430 Object Oriented Programming and Data Structures I"— Presentation transcript:

1 COMPUTER 2430 Object Oriented Programming and Data Structures I

2 One class can have multiple subclasses
Inheritance Class A Class B1 Class C11 Class C12 Class B2 Class C21 In any OO language One class can have multiple subclasses

3 Inheritance Class A Class B Class C
Can one class have more than one parent class? Allowed in C++, but not in Java or VB. Advantages and Disadvantages

4 Interface Inheritance
An interface is a special "class” No data Only “abstract” methods Cannot create objects of an interface Can declare variables to point objects of classes that implement the interface In Java and VB A class can “extend” only one class A class can implement multiple interfaces

5 Interface Comparable public interface Comparable { public int compareTo ( Object x ); } Return value: negative if (this) less than x 0 if (this) equal to x positive if (this) greater than x

6 Class Date public class Date implements Comparable { // All the stuff we had before /** What to do if not date? Could crash Throw an exception return a special number such as public int compareTo ( Object x ) // check instance of Date and cast // Date d = (Date)x; // check years, months, and days and return 0/+1/-1 }

7 Generic Interface Comparable
public interface Comparable <E> { public int compareTo ( E x ); } Return value: negative if (this) less than x 0 if (this) equal to x positive if (this) greater than x

8 Class Date public class Date implements Comparable<Date> { x an object of class Date public int compareTo ( Date x ) if (year == x.year && month == x.month && day == x.day) return 0; else if ((year < x.year) || (year == x.year) && (month < x.month)) || (year == x.year && month == x.month && day < x.day)) return -1; else return 1; }

9 Example String s = nextNonEmptyString(); Date d1 = new Date(s); // assuming in correct format s = nextNonEmptyString(); Date d2 = new Date(s); int result = d1.CompareTo(d2); if (result == 0) System.out.println(“Same date”) else if (result == 1) System.out.println(d1.toString() + “ is after ” + d2.toString()); else System.out.println(d1.toString() + “ is before ” + d2.toString()); Quiz is coming? Program due!

10 Example String s = nextNonEmptyString(); Date d1 = new Date(s); // assuming in correct format s = nextNonEmptyString(); Date d2 = new Date(s); int result = d1.CompareTo(d2); if (result == 0) System.out.println(“Same date”) else if (result > 0) System.out.println(d1.toString() + “ is after ” + d2.toString()); else System.out.println(d1.toString() + “ is before ” + d2.toString()); Quiz is coming? Program due!

11 Class Date public class Date implements Comparable<Date> { x an object of class Date public int compareTo ( Date x ) } A total ordering of all Date objects We can compare any two objects of Date and receive one of three possible results Should be consistent with equals, although not required No total ordering on FixedPoint class

12 Quiz 4


Download ppt "COMPUTER 2430 Object Oriented Programming and Data Structures I"

Similar presentations


Ads by Google