Review of Previous Lesson
1
29/05/2020
Object Orientated Programming Paradigm (OOP)
Own Classes - abstract
2
29/05/2020
Language Features and other Testable Topics
3
29/05/2020
Tested in the AP CS A Exam | Notes | Not tested in the AP CS A Exam, but potentially relevant/useful |
Classes Create subclass of a superclass (abstract, non-abstract). | 14 | |
Miscellaneous OOP | 15 | |
Language Features and other Testable Topics
14. Students are expected to extend classes. Students are also expected to have knowledge of inheritance that includes understanding the concepts of method overriding and polymorphism. Students are expected to implement their own subclasses.
Students are expected to read the definition of an abstract class and understand that the abstract methods need to be implemented in a subclass.
15. Students are expected to understand that conversion from a subclass reference
to a superclass reference is legal and does not require a cast. Class casts (generally from Object to another class) are not included in the AP Java subset. Array type compatibility and casts between array types are not included in the subset.
4
29/05/2020
Write your own programs:�Write your own programs from “scratch”.�Of course you should use previous programs for reference, but write your code from “scratch” (do not copy and paste).
5
29/05/2020
Cards (Valentine cards, birthday cards, holiday cards, ….)
6
29/05/2020
Continued on the next slide.
?
?
Abstract Classes
abstract class ClassName {
7
29/05/2020
Abstract Methods
abstract void myMethod(); // No {} with enclosed statements.
8
29/05/2020
access modifier, abstract, a return type or void, the method name (a parameter list) followed by a semicolon(;)
- No {} & so no implementation (no statements).
Nothing in an abstract Class has to be abstract
9
29/05/2020
Why use Abstract Classes?
10
29/05/2020
What cannot be abstract?
11
29/05/2020
An abstract class is defined like this:
abstract class ClassName {
. . . . . // access modifier instance variables (if absent default scope will be used).
. . . . . // Constructor (if absent a default constructor will be used).
/* Possible abstract method headers (if no access modifier, default scope will be used).
access modifier, abstract, a return type or void, the method name (a parameter list) followed by a
semicolon(;)
- No {} & no implementation (no code).
*/
// Possible non-abstract methods.
}
12
29/05/2020
Abstract Cards
13
29/05/2020
Continued on the next slide.
abstract class Card
14
29/05/2020
Valentine, Holiday and Birthday Cards
15
29/05/2020
"Dear " + getrecipient() + ",
Season's Greetings. "
"Dear " + getrecipient() + ",
Happy " + age + “ Birthday."
"Dear " + getrecipient() + ",
Love and Kisses. “
"XXXX" (number of kisses to be set when the object is created)
Write these classes.
Continued on the next slide.
Note the line breaks!
Card Program 1
Holiday hol = new Holiday( “me” );
hol.greeting();
Birthday bd = new Birthday( “me”, 21 );
bd.greeting()
….
16
29/05/2020
Answer the following questions in your comments:
Card card = new Card();
card.greeting();
Why?
17
29/05/2020
Card Program 2
18
29/05/2020
Card Program 3
19
29/05/2020
Answer the following question in your comments:
20
29/05/2020
Card Program 4
21
29/05/2020
Card Program 4
Card[] cardArr2 = new Card[4];
….
cardArr2[2] = new YouthBirthday ("Dante", 3);
…..
System.out.println(cardArr2[2].greeting("me"));
22
29/05/2020
Class Typecasting
23
29/05/2020
for ( Card cardArr2CellRef : cardArr2) {
System.out.println(cardArr2CellRef.greeting());
if (cardArr2CellRef instanceof YouthBirthday) System.out.println(((YouthBirthday)cardArr2CellRef).greeting("me"));
}
}
e.g.
Card Program 5
24
29/05/2020
Grade yourself
25
5/29/2020