Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of Previous Lesson

Similar presentations


Presentation on theme: "Review of Previous Lesson"— Presentation transcript:

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 - Interfaces

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

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

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

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

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

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

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

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

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

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

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

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

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

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

18 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?

19 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?

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

21 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

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

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

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

25 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?

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

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

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

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

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

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

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

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


Download ppt "Review of Previous Lesson"

Similar presentations


Ads by Google