Enhanced Class Design Joe Komar 12312021 Komar Associates

  • Slides: 31
Download presentation
Enhanced Class Design Joe Komar 12/31/2021 Komar Associates 1

Enhanced Class Design Joe Komar 12/31/2021 Komar Associates 1

Enhanced Class Design Tools n Abstract classes n Interfaces n Packages 12/31/2021 Komar Associates

Enhanced Class Design Tools n Abstract classes n Interfaces n Packages 12/31/2021 Komar Associates 2

Abstract Classes n n n Typically represents a “concept” in hierarchy Cannot be instantiated

Abstract Classes n n n Typically represents a “concept” in hierarchy Cannot be instantiated Contain abstract methods u Some methods typically not abstract u Expect the children to finish the definition of the class n Format: abstract … methodname(); u defines what parameters are accepted and what is returned 12/31/2021 Komar Associates 3

Abstract Classes n n The modifiers final and static cannot be used with abstract

Abstract Classes n n The modifiers final and static cannot be used with abstract methods If there is one or more abstract methods in a class is must be declared abstract u e. g. , n abstract class Food { Children must override the abstract methods of the abstract parent u If don’t override all abstract methods, the child must be declared abstract 12/31/2021 Komar Associates 4

Printer Example File Binary_File Text_File Image_File 12/31/2021 Komar Associates 5

Printer Example File Binary_File Text_File Image_File 12/31/2021 Komar Associates 5

Printer Example public class Printer { public static void main (String[] args) { byte[]

Printer Example public class Printer { public static void main (String[] args) { byte[] logo_data = {41, 42, 49, 44}; Text_File report = new Text_File ("Sand Reconner", 66, "One two three"); Image_File logo = new Image_File ("Number 1", 45, logo_data); Print_Logger daily = new Print_Logger(); daily. log(report); daily. log(logo); }// method main }// class Printer 12/31/2021 Komar Associates 6

Printer Example (cont’d) abstract class File { protected String id; protected int size; public

Printer Example (cont’d) abstract class File { protected String id; protected int size; public File (String file_id, int file_size){ id = file_id; size = file_size; }// constructor File public String name(){ return id; }// method name abstract public String print(); }// class File 12/31/2021 Komar Associates 7

Printer Example (cont’d) class Text_File extends File { protected String text; public Text_File (String

Printer Example (cont’d) class Text_File extends File { protected String text; public Text_File (String id, int size, String file_contents){ super(id, size); text = file_contents; }//constructor Text_File public String print() { return text; }// print method }// class Text_File 12/31/2021 Komar Associates 8

Printer Example (cont’d) class Binary_File extends File { protected byte[] data; public Binary_File (String

Printer Example (cont’d) class Binary_File extends File { protected byte[] data; public Binary_File (String id, int size, byte[] file_data){ super(id, size); data = file_data; }//constructor Binary_File public String print() { return ""; }//method print }// class Binary_File 12/31/2021 Komar Associates 9

Printer Example (cont’d) class Image_File extends Binary_File { public Image_File (String id, int size,

Printer Example (cont’d) class Image_File extends Binary_File { public Image_File (String id, int size, byte[] file_data){ super(id, size, file_data); }// constructor Image_File public String print() { return new String (data); }//method print }//class Image_File 12/31/2021 Komar Associates 10

Printer Example (cont’d) class Print_Logger { public void log (File file) { System. out.

Printer Example (cont’d) class Print_Logger { public void log (File file) { System. out. println (file. name() + " : " + file. print()); }// method log }//class Print_Logger 12/31/2021 Komar Associates 11

Printer Example Output 12/31/2021 Komar Associates 12

Printer Example Output 12/31/2021 Komar Associates 12

Interfaces Collection of constants and abstract methods n Not classes n Can be used

Interfaces Collection of constants and abstract methods n Not classes n Can be used in the definition of a class interface-name { constants and methods} class-name implements interface-name { implementation of abstract methods} n 12/31/2021 Komar Associates 14

Interfaces n n All methods in an interface are, by default, public and abstract

Interfaces n n All methods in an interface are, by default, public and abstract All constants are public and final Constants can be used in the class as if they were declared locally Interfaces can be linked in a hierarchy (extends) like classes 12/31/2021 Komar Associates 15

Interfaces n Interface names can be used as a “class type” in a formal

Interfaces n Interface names can be used as a “class type” in a formal parameter u Any class that implements that interface will be accepted as the actual parameter n n A class can implement more than one interface Interfaces allow for “multiple inheritance” 12/31/2021 Komar Associates 16

Printer 2 Example public class Printer 2 { public static void main (String[] args)

Printer 2 Example public class Printer 2 { public static void main (String[] args) { byte[] logo_data = {41, 42, 49, 44}; Text_File report = new Text_File ("Sand Reconner", 66, "One two three"); Image_File logo = new Image_File ("Number 1", 45, logo_data); Print_Logger daily = new Print_Logger(); daily. log(report); daily. log(logo); }// method main }// class Printer 2 12/31/2021 Komar Associates 17

Printer 2 Example class File { protected String id; protected int size; No Print()

Printer 2 Example class File { protected String id; protected int size; No Print() method public File (String file_id, int file_size){ id = file_id; size = file_size; }// constructor File Not abstract any longer public String name(){ return id; }// method name }// class File 12/31/2021 Komar Associates 18

Printer 2 Example class Text_File extends File implements Printable { protected String text; public

Printer 2 Example class Text_File extends File implements Printable { protected String text; public Text_File (String id, int size, String file_contents){ super(id, size); text = file_contents; Implements the print() }//constructor Text_File method public String print() { return text; name() method in File }// print method }// class Text_File 12/31/2021 Komar Associates 19

Printer 2 Example class Binary_File extends File { protected byte[] data; public Binary_File (String

Printer 2 Example class Binary_File extends File { protected byte[] data; public Binary_File (String id, int size, byte[] file_data){ super(id, size); Doesn’t implement Printable data = file_data; }//constructor Binary_File }// class Binary_File 12/31/2021 Parent to Image_File Komar Associates 20

Printer 2 Example class Image_File extends Binary_File implements Printable { public Image_File (String id,

Printer 2 Example class Image_File extends Binary_File implements Printable { public Image_File (String id, int size, byte[] file_data){ super(id, size, file_data); }// constructor Image_File Implements print() public String print() { return new String (data); }//method print }//class Image_File 12/31/2021 name() in File class Komar Associates 21

Printer 2 Example interface Printable { String name(); Names name() and print() methods to

Printer 2 Example interface Printable { String name(); Names name() and print() methods to be implemented String print(); interface Name }// interface Printable class Print_Logger { public void log (Printable file) { System. out. println (file. name() + " : " + file. print()); }// method log }//class Print_Logger 12/31/2021 Komar Associates 22

Printer 2 Example Output 12/31/2021 Komar Associates 23

Printer 2 Example Output 12/31/2021 Komar Associates 23

Abstract Classes and Interfaces n Used together become a powerful tool u Multiple Inheritance

Abstract Classes and Interfaces n Used together become a powerful tool u Multiple Inheritance u Encapsulate and hide information u Specify what must be done not how u Allow systems to be built by many programmers n Basically, represent the “heart” of object orientation 12/31/2021 Komar Associates 24

Packages n n n Used to group similar classes together under one name Identify

Packages n n n Used to group similar classes together under one name Identify files and classes for the package Put compiled versions into a subdirectory associated with that package name u Include in the classpath environment variable u Include in the “standard” path u Name at compile and run time 12/31/2021 Komar Associates 25

Simple_IO Example package Simple_IO; import java. io. *; public class Reader{ public static int

Simple_IO Example package Simple_IO; import java. io. *; public class Reader{ public static int read() throws IOException { Buffered. Reader stdin =new Buffered. Reader (new Input. Stream. Reader (System. in)); String value = stdin. read. Line(); return Integer. parse. Int (value); }// method read public static String read_line() throws IOException { Buffered. Reader stdin = new Buffered. Reader (new Input. Stream. Reader (System. in)); return stdin. read. Line(); }// method read_line }//class Reader 12/31/2021 Komar Associates 26

Simple_IO Example package Simple_IO; public class Writer { public static void write (int value){

Simple_IO Example package Simple_IO; public class Writer { public static void write (int value){ System. out. println (value); }// method write public static void write_line (String line) { System. out. println (line); }// method write_line }// class Writer 12/31/2021 Komar Associates 27

Compiling and Running n n Assume that Simple_IO directory on A: Place Reader. java

Compiling and Running n n Assume that Simple_IO directory on A: Place Reader. java and Writer. java in that directory and compile as normal creating the. class files You can continue to add classes to this directory To use them use import Simple_IO. *; 12/31/2021 Komar Associates 28

Example use of a package import java. io. IOException; import Simple_IO. *; class Simple_IO_Test

Example use of a package import java. io. IOException; import Simple_IO. *; class Simple_IO_Test { public static void main (String[] args) throws IOException{ int value = Reader. read(); String line = Reader. read_line(); Writer. write (value); Writer. write_line (line); }// method main }//class Simple_IO_Test 12/31/2021 Komar Associates 29

Finding the Directory n n Got your own system -- add directories in the

Finding the Directory n n Got your own system -- add directories in the “standard” classpath At UST, store directories on diskette, then to compile and run: u javac -classpath a: ; %classpath% prog. java u java -classpath a: ; %classpath% prog 12/31/2021 Komar Associates 30

Package Considerations n If you have a conflict with method names, use the fully

Package Considerations n If you have a conflict with method names, use the fully qualified name: u Simple_IO. Reader. read() u Simple_IO. Writer. write() u Don’t n then need to use an import statement Note that these packages you create need to exist on the system on which the program runs 12/31/2021 Komar Associates 31

Summary n n Abstract classes -- if any method is abstract, the entire class

Summary n n Abstract classes -- if any method is abstract, the entire class is abstract Abstract methods must all be implemented somewhere in the hierarchy Interfaces are useful in implementing more than one hierarchy Packages are ways of grouping similar and/or interdependent classes under one name 12/31/2021 Komar Associates 32