Presentation is loading. Please wait.

Presentation is loading. Please wait.

// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");

Similar presentations


Presentation on theme: "// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");"— Presentation transcript:

1

2 // Java2101.java This program tests the features of the 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(); }

3

4 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; } }

5 // Java2102.java // This program uses a simplified 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; } }

6 // Java2103.java // The former class is now a 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 Typical ProgramJava2103.java Uses class Uses interface Has methods with program statements No statements, only method headings Methods headings have no semi-colons Method headings have semi- colons Class has a constructorThere is no constructor There are fields to store dataThere are no fields

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 class implements the 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 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 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; } }

20

21 // 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; } }

22 // Java2108.java // This program shows how one class, can implement two // interfaces and. 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(); }

23 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; } }

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

25 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; }

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

27 // Java2110.java // This program uses an abstract class, rather than a 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 InterfaceAbstract Class Abstract methods onlyAbstract and concrete methods No constructor allowedCan have a constructor Needs a class to implement the interface Needs a subclass to implement the abstract methods Only final data fields are allowed Any data field is allowed Cannot instantiate an object

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

32 Inheritance vs. Implementation Implementation of Interfaces A interface is something abstract. It is like a general idea you have before anyone can create the blueprints.

33

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

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

38 // Java2114.java // This program demonstrates "polymorphism". // The 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()); }

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 Use the following steps to use a method polymorphically 1 Declare an interface or abstract superclass with the necessary abstract methods. 2 Implement the abstract methods in multiple classes using implementations appropriate to each class. 3 Declare some method that performs a common purpose for each of the methods, but the actual execution depends on the implementation of the specified class object. The parameter heading of this method needs to use the interface identifier or the super class identifier.

42 // Java2116.java // This program is almost identical to Java2115.java. // This time the method uses the class. // The program does not compile, even though every class extends // the 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 // class for polymorphism, but types casting must be used // to the 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 cast to a class or interface, which does declare the required methods.


Download ppt "// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");"

Similar presentations


Ads by Google