Review of Previous Lesson

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 8 More Object Concepts
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Abstract Classes Java. Learning Objectives Be able to understand and write programs using Abstract Classes.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
Objects as a programming concept
Inheritance and Polymorphism
An Introduction to Inheritance
Types of Programming Languages
Continuing Chapter 11 Inheritance and Polymorphism
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object Oriented Programming
Abstract Classes Java.
Inheritance Basics Programming with Inheritance
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Computer Programming with JAVA
Java – Inheritance.
Java Programming, Second Edition
Object-Oriented Programming in PHP
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Review of Previous Lesson
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
Review of Previous Lesson
Review of Previous Lesson
Review of Previous Lesson
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
Review of Previous Lesson
Review of Previous Lesson
Review of Previous Lesson
CMPE212 – Reminders Assignment 2 due next Friday.
Review of Previous Lesson
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

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 0 - 3.

Object Orientated Programming Paradigm (OOP) 30/04/2019 Object Orientated Programming Paradigm (OOP) Own Classes - abstract

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

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.

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

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.

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.

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

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.

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.

What cannot be abstract? 30/04/2019 What cannot be abstract? Constructors Static methods Private methods Methods that are declared “final”

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.

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.

abstract class Card 30/04/2019

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.

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

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)

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.

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.

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.

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.

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

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

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.

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!

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.

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.

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.

4/30/2019 Grade yourself Grade yourself on the vocabulary and learning objectives of the presentation.