Review of Previous Lesson

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Java Interfaces Warm-up: What is an abstract class? What is type casting?
Lecture 5:Interfaces and Abstract Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Classes and Objects.
Concepts of Object Oriented Programming
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Final and Abstract Classes
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Java Unit 11: Inheritance I
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Designing for Inheritance
Class Inheritance (Cont.)
Inheritance Basics Programming with Inheritance
Interface.
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Object-Oriented Programming
Chapter 9: Polymorphism and Inheritance
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Packages and Interfaces
Advanced Programming Behnam Hatami Fall 2017.
Computer Programming with JAVA
Object-Oriented Programming
Overriding Methods & Class Hierarchies
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,
CMSC 202 Generics.
Review of Previous Lesson
Object-Oriented Programming
Chapter 14 Abstract Classes and Interfaces
Review of Previous Lesson
Review of Previous Lesson
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Chapter 8 Inheritance Part 2.
Final and Abstract Classes
Review of Previous Lesson
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 - Interfaces

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 class that implements an interface. 13 Interfaces Design/create/modify an interface. Inheritance Design/create/modify classes that implement interfaces.

Language Features and other Testable Topics 30/04/2019 Notes: 13. Students are expected to write interfaces or class declarations when given a general description of the interface or class.

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

Interfaces Java has single inheritance, only. 30/04/2019 Interfaces Java has single inheritance, only. This means that a child class inherits from only one parent class. Usually this is all you need. But sometimes multiple inheritance would be convenient, where a child class inherits characteristics from several parent classes. But this can be confusing. What happens when two parents have different versions of the same method? Interfaces give Java some of the advantages of multiple inheritance without the disadvantages.

30/04/2019 Interfaces With object oriented programming, the idea is to define software objects that mimic "real world" objects. This is supposed to make programs easier to think about and more reliable. In the real world, objects are often thought about in several different ways. e.g. A car is a both a vehicle and a taxable property. It would be convenient if software objects, also, could be thought of in several ways. But a Java object belongs to just one class (single inheritance).

30/04/2019 An Interface Describes aspects of a class other than those that it inherits from its parent. Is a set of requirements that the class must implement (through code, not through inheritance). A list of constants and method headers. The methods are not implemented in the interface (there is no method body). A class that implements an interface must implement each of the methods listed in the interface. Similar to abstract classes in that they can used to put some kind of compulsion on classes which ‘implement’ them.

Interface Definition 30/04/2019 interface InterfaceName { constant definitions // method headers (without implementations). //Access modifier, a return type or void, the method name (a parameter list) followed by a semicolon(;) - (without implementations). } The constants in an interface are public static final by default and can be used in a class that implements it as if they were defined in the class (as they are final they cannot be changed after their definitions in the interface). The methods in an interface are public by default, so that modifier may be left out; they cannot be private nor protected. They must be implemented and declared public in a class that implements the interface. An interface looks somewhat like a class definition but no objects can be constructed from it. However, you can define a class that implements an interface, and once you have done so, you can construct objects of that class.

A class implements an interface by doing this: 30/04/2019 A class implements an interface by doing this: class SomeClass extends SomeParent implements interfaceName1, interfaceName2, … { …. } A class extends just one parent, but may implement multiple interfaces. Abstract classes can also implement interfaces. Optional Optional: Possible to implement multiple interfaces.

30/04/2019 Interface as a Type An interface can be used as a data type for a reference variable. The interface will tell the compiler that all objects will have the methods in the interface, so those methods can be used with all reference variables of that interface type.

Database Program Create a database program for a store. 30/04/2019 Database Program Create a database program for a store. The store sells: Goods, each of which has the attributes: description price The types of goods are: Food — with an attribute calories. Food objects are not taxable. Toy — with an attribute minimumAge. Toy objects are taxable. Book — with an attribute author. Book objects are taxable. There are many things that are taxable that are not goods, such as services or entertainment. Also, not all goods are taxable. So we want to have the concept taxable as a separate concept, not part of the concept of Goods. Continued on the next slide.

Parent Class, Child Class, or Interface? 30/04/2019 Database Program Here is what the concept Taxable looks like: A Taxable item, has a taxRate of 6 percent, has a calculateTax() method. Implement these concepts as a class hierarchy and an interface. Concept Parent Class, Child Class, or Interface? Goods Parent Food Child Toy Book Taxable Interface ? ? ? ? ? Continued on the next slide.

Database Program 30/04/2019 Write these classes. toString() Note: Typically: price*taxRate Display toString(); All child classes should override the Goods toString() method by displaying the Goods instance variables & its own instance variable. Write these classes. Continued on the next slide.

red = new keywords regarding interfaces Database Program 30/04/2019 red = new keywords regarding interfaces

30/04/2019 Database Program 1 Write a class with a main() method to create the objects shown above and implement each object’s toString() method and calculateTax() method when appropriate. Continued on the next slide.

Database Program 1 Answer the following questions in your comments: 30/04/2019 Database Program 1 Answer the following questions in your comments: Can an interface include instance variables? Why can’t the methods in an interface be private? Inspect the interface below. Is it correct? interface SomeInterface { public final int x = 32; public double y; public double addup( ); }

Database Program 1 Answer the following questions in your comments: 30/04/2019 Database Program 1 Answer the following questions in your comments: Is the class definition below correct? What parent class does it extend? public class SmallClass implements InterfaceA { class definition body } Which method(s) does the class Food has(have)? Which method(s) does the class Toy have to have? Why? Which other method can it use?

Database Program 1 Answer the following questions in your comments: 30/04/2019 Database Program 1 Answer the following questions in your comments: What has to be the 1st line in the constructor for the Food class? Why must calculateTax() be made public? Is the taxRate for Book the same as for Toy? Could the four objects in the program be kept in an array? What would the array type be? Can the interfaces contain several definitions of the same constant? Can an interface be made private? Can a class extend an interface?

Database Program 1 Answer the following questions in your comments: 30/04/2019 Database Program 1 Answer the following questions in your comments: Can an interface include instance variables? No. An interface includes only constants and method headers. Why can’t the methods in an interface be private? A private method can only be used by other methods of a class. But an interface is not a class and there are no methods that use other methods. The methods cannot be protected for a similar reason.

Database Program 1 Answer the following questions in your comments: 30/04/2019 Database Program 1 Answer the following questions in your comments: Inspect the interface below. Is it correct? interface SomeInterface { public final int x = 32; public double y; public double addup( ); } No. Variables (such as y) cannot be put in an interface. Only constants and method headers. public double y; // No variables allowed

Database Program 1 Answer the following questions in your comments: 30/04/2019 Database Program 1 Answer the following questions in your comments: Is the class definition below correct? What parent class does it extend? public class SmallClass implements InterfaceA { class definition body } Yes, the definition is correct. You might wonder that it does not extend a base class, but it does. If no other class is extended, Object is the base class. So SmallClass extends Object and implements InterfaceA.

Database Program 1 Answer the following questions in your comments: 30/04/2019 Database Program 1 Answer the following questions in your comments: Which method(s) does the class Food has(have)? toString() & getPrice() Which method(s) does the class Toy have to have? Why? Which other method can it use? As the Toy class implements the Taxable & Display interfaces, it has to have calculateTax() from the Taxable Interface & toString() from the Display interface, it can use the getprice() method from the parent Goods class. What has to be the 1st line in the constructor for the Food class? Super() Why must calculateTax() be made public? Methods in an interface are public by default, but the implementation in a class must explicitly say public. Is the taxRate for Book the same as for Toy? Yes. By using an interface, a constant can be used by several classes. This helps keep the classes consistent.

Database Program 1 Answer the following questions in your comments: 30/04/2019 Database Program 1 Answer the following questions in your comments: Could the four objects in the program be kept in an array? Yes. What would the array type be? Goods[] Can the interfaces contain several definitions of the same constant? No. To ensure consistency, a constant should be defined only once, in one interface. Can an interface be made private? No. private would mean that nobody could use it, which is not sensible. Can a class extend an interface? No. Only classes are extended, and a class may extend only one class. More questions later.

Database Program 2 Store all objects in a Goods[] array. 30/04/2019 Database Program 2 Store all objects in a Goods[] array. Answer the following question in your comments: When each cell of the array is printed is the same toString() method executed?

Database Program 2 Answer the following question in your comments: 30/04/2019 Database Program 2 Answer the following question in your comments: When each cell of the array is printed is the same toString() method executed? No, the toString() of each different object is used. More questions later.

30/04/2019 Database Program 3 Store a variety of Toy & Book objects in a Taxable array. e.g. Taxable[] taxableObjects = new Taxable […]; Try: Using the calculateTax() method on these Taxable references. Printing taxableObjects. Storing Food objects in the Taxable array. Using the getPrice() method on these Taxable references. Note that some of the requests above will not compile, the problems are on your syllabus, the solutions are not.

Database Program 3 Answer the following questions in your comments: 30/04/2019 Answer the following questions in your comments: Will the following compile? Explain Taxable[] taxableObjects = new Taxable [10]; taxableObjects[0] = new Book ("Book Name", 6.5, "Me"); System.out.println(taxableObjects[0].calculateTax()); taxableObjects[1] = new Toy ("Leggos", 54.45, 8); System.out.println(taxableObjects[1].getPrice()); taxableObjects[2] = new Book ("My New Book", 7.4, "Me"); System.out.println(taxableObjects[2]); taxableObjects[3] = new Food ("ox tails", 4.45, 1500 );

Database Program 3 Answer the following question in your comments: 30/04/2019 Database Program 3 Answer the following question in your comments: Will the following compile? Explain Taxable[] taxableObjects = new Taxable [10]; taxableObjects[0] = new Book ("Book Name", 6.5, "Me"); System.out.println(taxableObjects[0].calculateTax()); The code above does compile because Taxable interface tells the compiler that all Taxable objects will have a calculateTax() method, so this method can be used with Taxable references.

Database Program 3 Answer the following question in your comments: 30/04/2019 Database Program 3 Answer the following question in your comments: Will the following compile? Explain Taxable[] taxableObjects = new Taxable [10]; taxableObjects[1] = new Toy ("Leggos", 54.45, 8); System.out.println(taxableObjects[1].getPrice()); When a variable is of type Taxable all the compiler knows is the "taxable" aspect of the object. The statements above fail to compile because Taxable objects do not necessarily have the requested getPrice() method. The solution to this is optional as (as seen in the Card program 5 in the previous presentation) typecasting in the way proposed here is not included in AP CS: Use class typecasting to access these methods.

Database Program 3 Answer the following question in your comments: 30/04/2019 Database Program 3 Answer the following question in your comments: Will the following compile? Explain Taxable[] taxableObjects = new Taxable [10]; taxableObjects[2] = new Book ("My New Book", 7.4, "Me"); System.out.println(taxableObjects[2]); The code above does compile basically due to polymorphism, since all objects inherit from Object so all objects have a toString() method which the Book class overrides.

Database Program 3 Answer the following question in your comments: 30/04/2019 Answer the following question in your comments: Will the following compile? Explain Taxable[] taxableObjects = new Taxable [10]; taxableObjects[3] = new Food ("ox tails", 4.45, 1500 ); The code above does not compile because the Food class does not implement the Taxable interface.

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