Download presentation
Presentation is loading. Please wait.
1
Review of Previous Lesson
30/04/2019 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
2
Object Orientated Programming Paradigm (OOP)
30/04/2019 Object Orientated Programming Paradigm (OOP) Own Classes - abstract
3
Language Features and other Testable Topics
30/04/2019 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
Language Features and other Testable Topics
30/04/2019 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.
5
30/04/2019 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).
6
Cards (Valentine cards, birthday cards, holiday cards, ….)
30/04/2019 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. ? ? Continued on the next slide.
7
Abstract Classes abstract class ClassName { 30/04/2019
Abstract classes cannot be instantiated (objects cannot be created from them) but they can be the parent of other classes.
8
Abstract Methods Have no body, meaning they have no statements. e.g.
30/04/2019 Abstract Methods Have no body, meaning they have no statements. e.g. abstract void myMethod(); // No {} with enclosed statements. 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
Nothing in an abstract Class has to be abstract
30/04/2019 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.
10
Why use Abstract Classes?
30/04/2019 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.
11
What cannot be abstract?
30/04/2019 What cannot be abstract? Constructors Static methods Private methods Methods that are declared “final”
12
An abstract class is defined like this:
30/04/2019 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.
13
30/04/2019 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. Continued on the next slide.
14
abstract class Card 30/04/2019
15
Valentine, Holiday and Birthday Cards
30/04/2019 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. The Holiday card should say: "Dear " + getrecipient() + ", Season's Greetings. " The Birthday card should say: Happy " + age + “ Birthday." The Valentine cards should say: Love and Kisses. “ "XXXX" (number of kisses to be set when the object is created) Write these classes. Note the line breaks! Continued on the next slide.
16
30/04/2019 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();
17
Answer the following questions in your comments:
30/04/2019 Answer the following questions in your comments: Do all classes that inherit from Card have a greeting() method? Can a programmer write this program without using an abstract class? How many Holiday cards can be constructed from the Holiday class? Each greeting() method from each of the sibling classes is different. Do they all meet the requirement of the abstract parent class, Card? 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)
18
Answer the following questions in your comments:
30/04/2019 Do all classes that inherit from Card have a greeting() method? Yes — by using an abstract class a programmer can make all children of that class look alike in important ways. Can a programmer write this program without using an abstract class? Yes. How many Holiday cards can be constructed from the Holiday class? As many as your program needs. Each greeting() method from each of the sibling classes is different. Do they all meet the requirement of the abstract parent class, Card? Yes. All that Card asked for was: public abstract void greeting(); Each sibling did that in its own way. In main() can we write? (try it if necessary) Card card = new Card(); card.greeting(); Why? This will not compile because class Card is an abstract class and therefore cannot be instantiated.
19
Answer the following questions in your comments:
30/04/2019 Answer the following questions in your comments: Can we create a child class of Card without a greeting() method? Why? (try it if necessary) No we cannot, as the Card class has an abstract greeting() method, all child classes are obliged to implement greeting() or be abstract again. More questions later.
20
Card Program 2 Write 2 classes which extend the Birthday class:
30/04/2019 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.
21
30/04/2019 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.
22
Answer the following question in your comments:
30/04/2019 Answer the following question in your comments: In the YouthBirthday class there are two methods called greeting(), what is this called? What other concept could be confused with the answer to Q7. above? Describe their differences. What is automatically added by Javac to following? class AnyClass
23
Answer the following question in your comments:
30/04/2019 Answer the following question in your comments: In the YouthBirthday class there are two methods called greeting(), what is this called? Overloading What other concept could easily be confused with the answer to Q7. above? Describe their differences. Overriding and overloading could easily be confused: Overloading occurs when two or more methods in one class have the same method name but different parameters. Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class. What is added by Javac to following? class AnyClass extends Object
24
Card Program 4 As in the previous version but: 30/04/2019
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.
25
30/04/2019 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: Does the code above compile? Why? The problem above is on your syllabus, the solution is not. See after the next slide for the solution if you are curious!
26
Answer the following question in your comments:
30/04/2019 Answer the following question in your comments: Card[] cardArr2 = new Card[4]; …. cardArr2[2] = new YouthBirthday ("Dante", 3); ….. System.out.println(cardArr2[2].greeting("me")); System.out.println(cardArr2CellRef).greeting("me")); Does the code above compile? Why? No it does not as this method signature does not exist in the Card class so cannot be overridden, it only exists in the child YouthBirthday class. Therefore, Polymorphism (the concept of “many forms”) does not apply.
27
Class Typecasting 30/04/2019 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") for ( Card cardArr2CellRef : cardArr2) { System.out.println(cardArr2CellRef.greeting()); if (cardArr2CellRef instanceof YouthBirthday) System.out.println(((YouthBirthday)cardArr2CellRef).greeting("me")); } e.g.
28
30/04/2019 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. Again, the problem above is on your syllabus, the solution is not.
29
4/30/2019 Grade yourself Grade yourself on the vocabulary and learning objectives of the presentation.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.