Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 2430 Day 9. 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.

Similar presentations


Presentation on theme: "CS 2430 Day 9. 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."— Presentation transcript:

1 CS 2430 Day 9

2 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

3 Agenda for today Finish Date methods Introduction to inheritance Inheritance details

4 Go to THE THING: https://xray.ion.uwplatt.edu/summerss

5 Method lessThan()

6 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); }

7 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; }

8 Method addDays()

9 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); }

10 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”!

11 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: String s are immutable

12 Any questions?

13 Go to THE THING: https://xray.ion.uwplatt.edu/summerss

14 Features of OOP Encapsulation Data hiding Abstraction / abstract data type NEW: Inheritance

15 A new keyword: extends public 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(); }

16 Inheritance examle UML: Class B inherits from class A AB Inheritance represents an “is a” relationship public class A {... } public class B extends A {... }

17 Is-a relationship public class Student extends Person {... } public class Square extends Rectangle {... } public class Civic extends Car {... }

18 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(); } }

19 UML for example project Class C uses classes A and B Class B inherits from class A AB C

20 Any questions?

21 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!

22 Some inheritance details

23 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)

24 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!

25 Any questions?

26 Go to THE THING: https://xray.ion.uwplatt.edu/summerss


Download ppt "CS 2430 Day 9. 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."

Similar presentations


Ads by Google