
Object-Oriented Programming Paradigm Overview
Explore the fundamentals of Object-Oriented Programming (OOP) paradigm, abstract classes, inheritance, method overriding, polymorphism, and more. Dive into creating subclasses, understanding abstract methods, class casts, and writing programs from scratch. Gain insights into greeting card types and the concepts of abstract classes and methods.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
01/07/2025 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
01/07/2025 O Object O Orientated P Programming P Paradigm (OOP OOP) Own Classes - abstract 2
Language Features and other Testable Topics Language Features and other Testable Topics 01/07/2025 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 3
Language Features and other Testable Topics Language Features and other Testable Topics 01/07/2025 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
01/07/2025 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
01/07/2025 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. 6
01/07/2025 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
01/07/2025 Abstract Methods Have no body, meaning they have no 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). e.g. abstract void myMethod(); // No {} with enclosed statements. 8
01/07/2025 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
01/07/2025 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
01/07/2025 What cannot be abstract? Constructors Static methods Private methods Methods that are declared final 11
An abstract class is defined like this: 01/07/2025 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
01/07/2025 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 privateinstancevariable to hold the name of the recipient of the card, a constructor to initialise the recipient name, a getter method for recipient and an abstractgreeting() method. Continued on the next slide. 13
01/07/2025 abstract class Card 14
01/07/2025 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. "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. The Holiday card should say: Note the line breaks! Continued on the next slide. 15
01/07/2025 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
01/07/2025 Answer the following questions in your comments: 1. 2. 3. 4. 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) 5. 6. 17
01/07/2025 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
01/07/2025 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
01/07/2025 Answer the following question in your comments: 7. In the YouthBirthday class there are two methods called greeting(), what is this called? 8. What other concept could be confused with the answer to Q7. above? Describe their differences. 9. What is automatically added by Javac to following? class AnyClass 20
01/07/2025 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
01/07/2025 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: 10. 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! 22
01/07/2025 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") for ( Card cardArr2CellRef : cardArr2) { System.out.println(cardArr2CellRef.greeting()); if (cardArr2CellRef instanceof YouthBirthday) System.out.println(((YouthBirthday)cardArr2CellRef).greeting("me")); } } e.g. 23
01/07/2025 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. 24
7/1/2025 Grade yourself Grade yourself on the vocabulary and learning objectives of the presentation. 25