1 / 46

Chapter 21 Slides

Exposure Java-A 2007. Chapter 21 Slides. Interfaces, Abstract Classes and Polymorphism. PowerPoint Presentation created by: Mr. John L. M. Schram. From Materials Created by Mr. Leon Schram. // Java2101.java This program tests the features of the <Bank> class.

leola
Télécharger la présentation

Chapter 21 Slides

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exposure Java-A 2007 Chapter 21 Slides Interfaces, Abstract Classes and Polymorphism PowerPoint Presentation created by: Mr. John L. M. Schram From Materials Created by Mr. Leon Schram

  2. // Java2101.java This program tests the features of the <Bank> class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n"); Bank tom = new Bank(5000.0,10000.0); Bank sue = new Bank(3000.0,15000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom's savings balance: " + tom.getSavingsBalance()); System.out.println("Sue's checking balance: " + sue.getCheckingBalance()); System.out.println("Sue's savings balance: " + sue.getSavingsBalance()); System.out.println(); System.out.println("Tom makes a $1000.00 checking withdrawal"); tom.makeCheckingWithdrawal(1000.0); System.out.println("Tom makes a $2000.00 savings withdrawal"); tom.makeSavingsWithdrawal(2000.0); System.out.println("Sue makes a $1500.00 checking deposit"); sue.makeCheckingDeposit(1500.0); System.out.println("Sue makes a $3000.00 savings deposit"); sue.makeSavingsDeposit(3000.0); System.out.println(); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom's savings balance: " + tom.getSavingsBalance()); System.out.println("Sue's checking balance: " + sue.getCheckingBalance()); System.out.println("Sue's savings balance: " + sue.getSavingsBalance()); System.out.println(); } } Output & Bank class on the next 2 slides

  3. class Bank { private double checking; private double savings; public Bank() { checking = 0.0; savings = 0.0; } public Bank(double c, double s) { checking = c; savings = s; } public double getCheckingBalance() { return checking; } public double getSavingsBalance() { return savings; } public void makeCheckingDeposit(double amount) { checking += amount; } public void makeSavingsDeposit(double amount) { savings += amount; } public void makeCheckingWithdrawal(double amount) { checking -= amount; } public void makeSavingsWithdrawal(double amount) { savings -= amount; } public void closeCheckingAccount() { checking = 0; } public void closeSavingsAccount() { savings = 0; } } This is a more advanced version of the Bank class which you have seen before.

  4. // Java2102.java // This program uses a simplified <Bank> class, which will be used for future // program examples in this chapter. public class Java2102 { public static void main (String args[]) { System.out.println("\nJAVA2102.JAVA\n"); Bank tom = new Bank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $1500.00 checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $2500.00 checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); } } class Bank { private double checking; public Bank(double c) { checking = c; } public double getCheckingBalance() { return checking; } public void makeCheckingDeposit(double amount) { checking += amount; } public void makeCheckingWithdrawal(double amount) { checking -= amount; } }

  5. Interfaces

  6. // Java2103.java // The former <Bank> class is now a <Bank> interface. // Only the method headings are shown. The program does not compile. public class Java2103 { public static void main (String args[]) { System.out.println("\nJAVA2103.JAVA\n"); Bank tom = new Bank(); System.out.println(); } } interface Bank { public double getCheckingBalance(); public void makeCheckingDeposit(double amount); public void makeCheckingWithdrawal(double amount); }

  7. Program Differences

  8. Java Interfaces A Java Interface provides a group of method signatures that will be available for any client of a class that implements the interface. Implementation details of the interface methods are neither required nor desired at the interface level.

  9. Java Collection Hierarchy Collection Interface List Interface Set Interface ArrayList class LinkedList class HashSet class TreeSet class

  10. Collections A collection is a group of objects.

  11. Linear Collections A linear collection stores its elements in a specific order. Linear collections can have duplicate elements.

  12. Lists A list is a linear collection that allows access to any element in the list. Examples of list data structures are Java static arrays, ArrayLists, Strings and Files.

  13. Unordered Collections An unordered collection stores elements without order.

  14. Bags A bag is an unordered collection that can have duplicate elements.

  15. Sets A set is an unordered collection without any duplicate elements.

  16. // Java2104.java // The <MyBank> class implements the <Bank> interface. The program now compiles and executes. public class Java2104 { public static void main (String args[]) { System.out.println("\nJAVA2104.JAVA\n"); MyBank tom = new MyBank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $1500.00 checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $2500.00 checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); } } interface Bank { public double getCheckingBalance(); public void makeCheckingDeposit(double amount); public void makeCheckingWithdrawal(double amount); } class MyBank implements Bank { private double checking; public MyBank(double c) { checking = c; } public double getCheckingBalance() { return checking; } public void makeCheckingDeposit(double amount) { checking += amount; } public void makeCheckingWithdrawal(double amount) { checking -= amount; } }

  17. // Java2105.java // An interface is "abstract" and its methods are also "abstract". The <abstract> keyword is optional. public class Java2105 { public static void main (String args[]) { System.out.println("\nJAVA2105.JAVA\n"); MyBank tom = new MyBank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $1500.00 checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $2500.00 checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); } } abstract interface Bank { public abstract double getCheckingBalance(); public abstract void makeCheckingDeposit(double amount); public abstract void makeCheckingWithdrawal(double amount); } class MyBank implements Bank { private double checking; public MyBank(double c) { checking = c; } public double getCheckingBalance() { return checking; } public void makeCheckingDeposit(double amount) { checking += amount; } public void makeCheckingWithdrawal(double amount) { checking -= amount; } }

  18. Implementation Rule A class, which implements an interface, must implement every method declared in the interface.

  19. // Java2106.java // This program partially implements the <Bank> class. Now the program does not compile. public class Java2106 { public static void main (String args[]) { System.out.println("\nJAVA2106.JAVA\n"); MyBank tom = new MyBank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $1500.00 checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $2500.00 checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); } } abstract interface Bank { public abstract double getCheckingBalance(); public abstract void makeCheckingDeposit(double amount); public abstract void makeCheckingWithdrawal(double amount); } class MyBank implements Bank { private double checking; public MyBank(double c) { checking = c; } public void makeCheckingDeposit(double amount) { checking += amount; } public void makeCheckingWithdrawal(double amount) { checking -= amount; } } Errors on next slide

  20. // Java2107.java This program demonstrates that it is possible to implement an interface // and define additional methods that are not declared in the interface. public class Java2107 { public static void main (String args[]) { System.out.println("\nJAVA2107.JAVA\n"); MyBank tom = new MyBank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $1500.00 checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $2500.00 checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); tom.closeAccount(); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); } } abstract interface Bank { public abstract double getCheckingBalance(); public abstract void makeCheckingDeposit(double amount); public abstract void makeCheckingWithdrawal(double amount); } class MyBank implements Bank { private double checking; public MyBank(double c) { checking = c; } public double getCheckingBalance() { return checking; } public void makeCheckingDeposit(double amount) { checking += amount; } public void makeCheckingWithdrawal(double amount) { checking -= amount; } public void closeAccount() { checking = 0; } }

  21. // Java2108.java // This program shows how one class, <BankAccounts> can implement two // interfaces <Checking> and <Savings>. public class Java2108 { public static void main (String args[]) { System.out.println("\nJAVA2108.JAVA\n"); BankAccounts tom = new BankAccounts(5000.0,7500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $1500.00 checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $2500.00 checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); System.out.println("Tom's savings balance: " + tom.getSavingsBalance()); System.out.println("Tom makes a $1500.00 savings deposit"); tom.makeSavingsDeposit(1500.0); System.out.println("Tom's savings balance: " + tom.getSavingsBalance()); System.out.println("Tom makes a $2500.00 savings withdrawal"); tom.makeSavingsWithdrawal(2500.0); System.out.println("Tom's savings balance: " + tom.getSavingsBalance()); System.out.println(); } } Output and other classes on next slide

  22. abstract interface Checking { public abstract double getCheckingBalance(); public abstract void makeCheckingDeposit(double amount); public abstract void makeCheckingWithdrawal(double amount); } abstract interface Savings { public abstract double getSavingsBalance(); public abstract void makeSavingsDeposit(double amount); public abstract void makeSavingsWithdrawal(double amount); } class BankAccounts implements Checking,Savings { private double checking; private double savings; public BankAccounts(double c, double s) { checking = c; savings = s; } public double getCheckingBalance() { return checking; } public double getSavingsBalance() { return savings; } public void makeCheckingDeposit(double amount) { checking += amount; } public void makeSavingsDeposit(double amount) { savings += amount; } public void makeCheckingWithdrawal(double amount) { checking -= amount; } public void makeSavingsWithdrawal(double amount) { savings -= amount; } }

  23. // Java2109.java // This program shows that it is possible to have a field in an interface, but it // must be final and initialized. public class Java2109 { public static void main (String args[]) { System.out.println("\nJAVA2109.JAVA\n"); MyBank tom = new MyBank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $1500.00 checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $2500.00 checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Computing interest"); tom.computeInterest(); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); } }

  24. abstract interface Bank { public final double rate = 0.05; public abstract double getCheckingBalance(); public abstract void makeCheckingDeposit(double amount); public abstract void makeCheckingWithdrawal(double amount); public abstract void computeInterest(); } class MyBank implements Bank { private double checking; private double interest; public MyBank(double c) { checking = c; interest = 0.0; } public double getCheckingBalance() { return checking; } public void makeCheckingDeposit(double amount) { checking += amount; } public void makeCheckingWithdrawal(double amount) { checking -= amount; } public void computeInterest() { interest = checking * rate; checking += interest; } }

  25. Using Fields in an Interface Fields may be used in an interface declaration. All fields must have an initialized value. Field values are constant and cannot be changed. The final keyword is optional. Final is implied.

  26. Abstract Classes

  27. // Java2110.java // This program uses an abstract <Bank> class, rather than a <Bank> interface. // There appears no difference between an abstract class and an interface. public class Java2110 { public static void main (String args[]) { System.out.println("\nJAVA2110.JAVA\n"); MyBank tom = new MyBank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $1500.00 checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $2500.00 checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); } } abstract class Bank { public abstract double getCheckingBalance(); public abstract void makeCheckingDeposit(double amount); public abstract void makeCheckingWithdrawal(double amount); } class MyBank extends Bank { private double checking; public MyBank(double c) { checking = c; } public double getCheckingBalance() { return checking; } public void makeCheckingDeposit(double amount) { checking += amount; } public void makeCheckingWithdrawal(double amount) { checking -= amount; } }

  28. // Java2111.java // An abstract class can have both abstract members and concrete members. // An interface can only have abstract members. public class Java2111 { public static void main (String args[]) { System.out.println("\nJAVA2111.JAVA\n"); MyBank tom = new MyBank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $1500.00 checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $2500.00 checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); } } abstract class Bank { protected double checking; protected Bank(double c) { checking = c; } public abstract double getCheckingBalance(); public abstract void makeCheckingDeposit(double amount); public abstract void makeCheckingWithdrawal(double amount); } class MyBank extends Bank { protected MyBank(double c) { super(c); } public double getCheckingBalance() { return checking; } public void makeCheckingDeposit(double amount) { checking += amount; } public void makeCheckingWithdrawal(double amount) { checking -= amount; } }

  29. Abstract Interfaces &Abstract Classes All methods of an interface must be abstract. Methods in an abstract class may be abstract or concrete.

  30. Interfaces vs. Abstract Classes

  31. Inheritance vs. ImplementationInheritance of Classes A class is a blueprint for creating an object. An inherited class (subclass) is a modified blueprint for creating an specialized object. Car class Truck class

  32. Inheritance vs. ImplementationImplementation of Interfaces A interface is something abstract. It is like a general idea you have before anyone can create the blueprints. Plane class Car class Vehicle Interface

  33. Polymorphism

  34. Polymorphism Polymorphism allows a single accessing feature, such as an operator, method or class identifier, to have many forms.

  35. // Java2112.java // This program displays the information of three different classes with 3 different <getData> methods. public class Java2112 { public static void main (String args[]) { Mammal animal1 = new Mammal(); Bird animal2 = new Bird(); Fish animal3 = new Fish(); getData1(animal1); getData2(animal2); getData3(animal3); System.out.println(); } public static void getData1(Mammal obj) { System.out.println("A " + obj.getType() + obj.getMove()); } public static void getData2(Bird obj) { System.out.println("A " + obj.getType() + obj.getMove()); } public static void getData3(Fish obj) { System.out.println("A " + obj.getType() + obj.getMove()); } } Other classes on next slide

  36. abstract interface Animal { abstract String getType(); abstract String getMove(); } class Mammal implements Animal { String animalType; public Mammal() { animalType = "Mammal"; } public String getType() { return animalType; } public String getMove() { return " walks."; } } class Bird implements Animal { String animalType; public Bird() { animalType = "Bird"; } public String getType() { return animalType; } public String getMove() { return " flies."; } } class Fish implements Animal { String animalType; public Fish() { animalType = "Fish"; } public String getType() { return animalType; } public String getMove() { return " swims."; } }

  37. // Java2113.java // This program is almost identical to Java2112.java. // All three of the <getData> use (Animal obj) in the method heading. public class Java2113 { public static void main (String args[]) { System.out.println("\nJAVA2113.JAVA\n"); Mammal animal1 = new Mammal(); Bird animal2 = new Bird(); Fish animal3 = new Fish(); getData1(animal1); getData2(animal2); getData3(animal3); System.out.println(); } public static void getData1(Animal obj) { System.out.println("A " + obj.getType() + obj.getMove()); } public static void getData2(Animal obj) { System.out.println("A " + obj.getType() + obj.getMove()); } public static void getData3(Animal obj) { System.out.println("A " + obj.getType() + obj.getMove()); } } Other classes on previous slide

  38. // Java2114.java // This program demonstrates "polymorphism". // The <getData> method displays the correct data polymorphically. public class Java2114 { public static void main (String args[]) { System.out.println("\nJAVA2114.JAVA\n"); Mammal animal1 = new Mammal(); Bird animal2 = new Bird(); Fish animal3 = new Fish(); getData(animal1); getData(animal2); getData(animal3); System.out.println(); } public static void getData(Animal obj) { System.out.println("A " + obj.getType() + obj.getMove()); } }

  39. // Java2115.java // This program demonstrates that polymorphism can be done // with abstract classes in the same manner as abstract interfaces. public class Java2115 { public static void main (String args[]) { System.out.println("\nJAVA2115.JAVA\n"); Mammal animal1 = new Mammal(); Bird animal2 = new Bird(); Fish animal3 = new Fish(); getData(animal1); getData(animal2); getData(animal3); System.out.println(); } public static void getData(Animal obj) { System.out.println("A " + obj.getType() + obj.getMove()); } } Other classes on next slide

  40. abstract class Animal { abstract String getType(); abstract String getMove(); } class Mammal extends Animal { String animalType; public Mammal() { animalType = "Mammal"; } public String getType() { return animalType; } public String getMove() { return " walks."; } } class Bird extends Animal { String animalType; public Bird() { animalType = "Bird"; } public String getType() { return animalType; } public String getMove() { return " flies."; } } class Fish extends Animal { String animalType; public Fish() { animalType = "Fish"; } public String getType() { return animalType; } public String getMove() { return " swims."; } }

  41. Polymorphism Steps

  42. // Java2116.java // This program is almost identical to Java2115.java. // This time the <getData> method uses the <Object> class. // The program does not compile, even though every class extends // the <Object> class. public class Java2116 { public static void main (String args[]) { System.out.println("\nJAVA2116.JAVA\n"); Mammal animal1 = new Mammal(); Bird animal2 = new Bird(); Fish animal3 = new Fish(); getData(animal1); getData(animal2); getData(animal3); System.out.println(); } public static void getData(Object obj) { System.out.println("A " + obj.getType() + obj.getMove()); } }

  43. // Java2117.java // This program demonstrates that it is possible to use the // <Object> class for polymorphism, but types casting must be used // to the <Animal> class before the program functions properly. public class Java2117 { public static void main (String args[]) { System.out.println("\nJAVA2117.JAVA\n"); Mammal animal1 = new Mammal(); Bird animal2 = new Bird(); Fish animal3 = new Fish(); getData(animal1); getData(animal2); getData(animal3); System.out.println(); } public static void getData(Object obj) { System.out.println("A " + ((Animal)obj).getType() + ((Animal)obj).getMove()); } }

  44. Using the Object Class with Polymorphism Java allows the use of a higher class or interface in the parameter signature of a method heading. This is precisely what makes polymorphism possible. The higher level class or interface must have abstract methods declared for polymorphism to work properly. It is possible to use a higher class or interface, such as Object for a polymorphic method. However, if the required methods are not declared in the higher class or interface, then it becomes necessary to type castto a class or interface, which does declare the required methods.

More Related