1 of 25

Review of Previous Lesson

  • State as many Vocabulary words and Learning Objectives that you remember from the last lesson as you can.
  • Remember to grade yourself from 0 - 3.

1

29/05/2020

2 of 25

Object Orientated Programming Paradigm (OOP)

Own Classes - abstract

2

29/05/2020

3 of 25

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

4 of 25

Language Features and other Testable Topics

  • Notes:

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

5 of 25

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

6 of 25

Cards (Valentine cards, birthday cards, holiday cards, ….)

  • If you had a box full of greeting cards you had received over the years, what would be a good label to write on the box?
    • Cards
  • Which of the card types should have a greeting() method?
    • All of them.

6

29/05/2020

Continued on the next slide.

?

?

7 of 25

Abstract Classes

abstract class ClassName {

    • Abstract classes cannot be instantiated (objects cannot be created from them) but they can be the parent of other classes.

7

29/05/2020

8 of 25

Abstract Methods

  • Have no body, meaning they have no statements.

  • e.g.

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

9 of 25

Nothing in an abstract Class has to be abstract

  • Can, but don't have to, contain abstract methods.
    • Can contain non-abstract methods, which will be inherited by the children.
      • However, if a class contains even one abstract method, then the class itself has to be declared to be abstract.

9

29/05/2020

10 of 25

Why use Abstract Classes?

  • To represent the general idea of a group of classes & organize programs.
    • Programs can be written without them, but a real-world application might have hundreds of classes, grouping classes together is important in keeping a program organized and understandable.
      • Using an abstract class means that you can group several related classes together as siblings.
  • Can be used to put some kind of compulsion on child classes by writing abstract methods in it.
    • Classes that inherit must provide the implementation of all the abstract methods of the abstract parent class else be another abstract subclass.
      • This means that there may be several steps between an abstract base class to a child class that is completely non-abstract.
      • As with all children classes, child classes of an abstract class inherit anything public from the abstract parent class.

10

29/05/2020

11 of 25

What cannot be abstract?

  • Constructors
  • Static methods
  • Private methods
  • Methods that are declared “final”

11

29/05/2020

12 of 25

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.

}

        • Note that the order indicated above is standard practice only, to make for clear readability.

12

29/05/2020

13 of 25

Abstract Cards

  • Write an abstract Card class that represents the abstract concept of a "Card“.
    • There will never be an object that is merely a “Card”.
    • A good label to write on a box full of different greeting cards would be "Cards“.
      • Even though everything in the box is a specific type of card, the box could be labelled with the general idea of “Cards”.
    • The parent class Card will be used only to group them into a hierarchy.
      • This is useful to do, just as a store has all its greeting cards in one display, but grouped into several categories.
    • A card object will have a greeting() method that writes out a greeting to a specific recipient.
      • So in the abstract Card class, there should be a private instance variable to hold the name of the recipient of the card, a constructor to initialise the recipient name, a getter method for recipient and an abstract greeting() method.

13

29/05/2020

Continued on the next slide.

14 of 25

abstract class Card

14

29/05/2020

15 of 25

Valentine, Holiday and Birthday Cards

  • All actual card objects must be more specific and will be an instance of one of the 3 child types Card: Valentine, Holiday, and Birthday.
  • Each type of card should contain its own version of the greeting() method to display an appropriate greeting.
    • So each class will implement the greeting() method differently.

15

29/05/2020

  • The Holiday card should say:

"Dear " + getrecipient() + ",

Season's Greetings. "

  • The Birthday card should say:

"Dear " + getrecipient() + ",

Happy " + age + “ Birthday."

  • The Valentine cards should say:

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

16 of 25

Card Program 1

  • Write a class with a main() method which asks the user for required information and creates at least one object from each of the 3 classes and show each object’s greeting.
    • Use reference variables of each child class type (not the parent Card class type).
      • e.g.

Holiday hol = new Holiday( “me” );

hol.greeting();

Birthday bd = new Birthday( “me”, 21 );

bd.greeting()

….

    • Now try:
      • Card card = new Card();
      • card.greeting();

16

29/05/2020

17 of 25

Answer the following questions in your comments:

  1. Do all classes that inherit from Card have a greeting() method?
  2. Can a programmer write this program without using an abstract class?
  3. How many Holiday cards can be constructed from the Holiday class?
  4. Each greeting() method from each of the sibling classes is different. Do they all meet the requirement of the abstract parent class, Card?
  5. In main() can we write? (try it if necessary)

Card card = new Card();

card.greeting();

Why?

  • Can we create a child class of Card without a greeting() method? �Why? (try it if necessary)

17

29/05/2020

18 of 25

Card Program 2

  • Write 2 classes which extend the Birthday class:
    • A YouthBirthday birthday card for young people.
      • This card will add the line "How you have grown!" to the usual birthday greeting.
    • An AdultBirthday birthday card for old people.
      • This card will add the line "You haven't changed at all!" to the usual birthday greeting.
    • Use an array of Card objects to all the different card types (each type at least once).
    • Make sure the main() method prints out all the contents of the array & demonstrates all methods of each class.

18

29/05/2020

19 of 25

Card Program 3

  • Extend the previous version so that there are 2 possible greeting() methods for YouthBirthday:
    • The previous method.
    • An additional method with the name of the sender as a parameter.
      • This method's signature is different from the parent's.
      • This will be an additional method―it will not override the parent's method.
      • In addition to what the parent's method does, this method writes out "How you have grown!! Love, " followed by the name of the sender.
      • Use an array of YouthBirthday objects to hold YouthBirthday cards.
      • Make sure the main() method prints out all the contents of the array & demonstrates both methods of YouthBirthday.

19

29/05/2020

20 of 25

Answer the following question in your comments:

  1. In the YouthBirthday class there are two methods called greeting(), what is this called?
  2. What other concept could be confused with the answer to Q7. above? Describe their differences.
  3. What is automatically added by Javac to following?
    • class AnyClass

20

29/05/2020

21 of 25

Card Program 4

  • As in the previous version but:
    • Use an array of Card objects to hold all different types of cards.
      • Write code in main() to demonstrate the standard greeting() - with no sender - method of each class.
        • e.g.

21

29/05/2020

22 of 25

Card Program 4

  • Now try using the 2nd parameterised greeting() method of the YouthBirthday class on a YouthBirthday object reference in the Card array:
    • e.g.

Card[] cardArr2 = new Card[4];

….

cardArr2[2] = new YouthBirthday ("Dante", 3);

…..

System.out.println(cardArr2[2].greeting("me"));

    • Answer the following question in your comments:
      1. Does the code above compile? Why?

22

29/05/2020

  • The problem above is on your syllabus, the solution is not.
    • See after the next slide for the solution if you are curious!

23 of 25

Class Typecasting

  • 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.
    • To determine an object reference class type:
      • if (obj instanceof C)
        • e.g. if (cardArr2CellRef instanceof YouthBirthday)
    • To Class Typecast:
      • ((ClassTypeRequired)referenceVariable)
        • e.g. ( (YouthBirthday) cardArr2CellRef ).greeting(“mum")

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.

24 of 25

Card Program 5

  • Extend the previous version so that all cards are held in an array of Objects.
    • Note that you will have typecast in the main() method to allow all object methods to be accessible.

24

29/05/2020

  • Again, the problem above is on your syllabus, the solution is not.

25 of 25

Grade yourself

  • Grade yourself on the vocabulary and learning objectives of the presentation.

25

5/29/2020