Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfaces.

Similar presentations


Presentation on theme: "Interfaces."— Presentation transcript:

1 Interfaces

2 What is an interface? An interface is similar to an abstract class in java, but the body of an interface can only include abstract methods and final fields. Like an abstract class, an interface cannot be instantiated in a client class. interface person { String returnname(); }

3 interface person constant, cannot be changed {
Reserved word interface person constant, cannot be changed { int X = 0; // the equivalent of writing final int x = 0; String returnname(); method has no body } Like an abstract class, an interface cannot be instantiated.

4 Interfaces and Abstract Classes: Similarities
They cannot be instantiated They can both declare abstract methods

5 Interfaces and Abstract Classes: Differences
An interface is not a class, and therefore do not inherit any classes and classes cannot inherit them. All methods inside an interface are automatically abstract and public and cannot be given a body. In fact, you do not even need to put the words abstract or public in the method declaration, the compiler does it for you Interfaces cannot declare variables, just constants. Interfaces do not have constructors One class can implement multiple interfaces

6 interface pet { public final int MEALS = 2; public void eat(); }
class dog implements pet { public void eat() // code }

7 Purpose of a interface? The purpose of an interface is to establish a common relationship amongst classes. Interfaces form a “contract” between the outside world and the program. Whenever a user uses an particular interface, he/she knows what to expect.

8 Classes can implement multiple interfaces
interface A { void methodA(); } interface B void methodB(); class C implements A,B { void methodA() { // code} void methodB() }

9 Complicated? interface A { void methodA(); } interface B void methodB(); abstract class C implements A,B class D extends C { void methodA() { // code} void methodB() }

10 More Complicated? interface A { void methodA(); } interface B void methodB(); abstract class C class D extends C implements A,B { void methodA() { // code} void methodB() }

11 Even More Complicated? interface A { void methodA(); } interface B void methodB(); interface C void methodC(); abstract class D implements C { void methodC() { // code} } class E extends D implements A,B void methodA() void methodB()

12 Exceptions? interface A { int[ ][ ] array1 = new array[10][10]; ArrayList<Integer> list1 = new ArrayList<>(); }

13 Example 1 interface A { int NUM1; }

14 Example 2 interface A { void methodA() // body of code }

15 Example 2 interface A { int MAX = 30; void methodA(); }
class B implements A { MAX = 10; }

16 Example 3 interface A { int MAX = 30; void methodA(); }
void main() { A myname = new A(); }

17 Example 4 Example 5 interface A { double MIN = 10.5; private void methodA(); } interface B extends A { void methodB(); }

18 Exercise 1 Build an interface called Xbox1
Give it a constant called integer XBOXPRICE = 400 Give it two methods : A method called Subscribers that returns the number of online subscribers in the form of an integer A method called topGames that will display the top ten selling games for the system. Build an interface called PS4 Give it a constant called integer PS4PRICE = 500

19 Exercise 2 Build an abstract class called Shopper. Shopper will implement both Xbox and PS4. Here are the properties of shopper. Shopper has a constructor that takes no parameters but asks the user which system they want to buy. It stores their decision in an integer called decision. We will let 1 mean Xbox1 or 2 mean PS4. You will need to declare this variable outside of your constructor Define Subscribers. Xbox1 will return and PS4 will return Make this choice based on what decision they entered. Define TopGames. For Xbox1, the games are {A, B, C, D, E, F, G, H, I, J}. For PS4, the games are {AA, BB, CC, DD, EE, FF, GG, HH, II, JJ}. Again, it will display the list based on what decision was.

20 Exercise 3 Define an interface called Sale. Sale has 1 constant and one method. double SALESTAX = .15; A method called Total that will return a double

21 Exercise 4 Define a class called MySale. MySale inherits Shopper and implements Sale. no constructors define the method Total. Total takes either the value of the PS4 or Xbox1, adds on the sales tax, and then returns that value. In void main(), declare an instance of MySale. Output all the necessary things (what system is chosen, the total price, and the list of top 10 games).


Download ppt "Interfaces."

Similar presentations


Ads by Google