1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor...

37
1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭鄭鄭 ) Associate Professor Computer Science & Engineerin g Tatung Institute of Technology

Transcript of 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor...

Page 1: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

1

Lecture 9Enhanced Class Design

Instructors:

Fu-Chiung Cheng

(鄭福炯 )

Associate Professor

Computer Science & Engineering

Tatung Institute of Technology

Page 2: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

1

Outline

• abstract classes

• formal Java interfaces

• packages

Page 3: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

2

Abstract Classes

• An abstract class cannot be instantiated• It is used in a class hierarchy to organize common

features at appropriate levels• An abstract method has no implementation, just a

name and signature• An abstract class often contains abstract methods• Any class that contains an abstract method is by

definition abstract

Page 4: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

3

Abstract Classes

• The modifier abstract is used to define abstract classes and methods

• The children of the abstract class are expected to define implementations for the abstract methods in ways appropriate for them

• If a child class does not define all abstract methods of the parent, then the child is also abstract

• An abstract class is often too generic to be of use by itself

Page 5: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

4

Abstract Classes

• See Dinner.java

Food

PepperoniFranksBeans

Page 6: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

public class Dinner { public static void main (String[] args) { Pepperoni slice = new Pepperoni(); System.out.println (slice.slogan()); } // method main} // class Dinnerabstract class Food { abstract public String slogan();} // class Foodclass Pepperoni extends Food { public String slogan() { return "Great for pizza!"; } // method slogan} // class Pepperoni

Page 7: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

5

Abstract Classes

• See Printer.java

File

Text_FileBinary_File

Image_File

Page 8: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

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

Page 9: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

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

Page 10: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

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; } // method print

} // class Text_File

Page 11: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

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

Page 12: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

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

class Print_Logger { public void log (File file) { System.out.println (file.name() + " : " + file.print()); } // method log} // class Print_Logger

Page 13: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

6

Abstract Classes

• An abstract method cannot be declared as final, because it must be overridden in a child class

• An abstract method cannot be declared as static, because it cannot be invoked without an implementation

• Abstract classes are placeholders that help organize information and provide a base for polymorphic references

Page 14: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

7

Interfaces

• A Java interface is a collection of constants and abstract methods

• “Interface”: the set of service methods provided by an object

• That is, the set of methods that can be invoked through an object define the way the rest of the system interacts, or interfaces, with that object

• The Java language has an interface construct that formalizes this concept

Page 15: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

8

Interfaces

• A class that implements an interface must provide implementations for all of the methods defined in the interface

• This relationship is specified in the header of the class:

class class-name implements interface-name {

...

}

See Soap_Box.java

Page 16: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

public class Soap_Box { public static void main (String[] args) { Kant immanual = new Kant(); System.out.println (immanual.pontificate()); } // method main} // class Soap_Box

interface Philosopher { String pontificate();} // class Philosopherclass Kant implements Philosopher {public String pontificate() { return "Follow the Categorical Imperitive!"; } // method pontificate} // class Kant

Page 17: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

9

Interfaces

• An interface can be implemented by multiple classes

• Each implementing class can provide their own unique version of the method definitions

• An interface is not a class, and cannot be used to instantiate an object

• An interface is not part of the class hierarchy• A class can be derived from a base class and imple

ment one or more interfaces

Page 18: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

10

Interfaces

• Unlike interface methods, interface constants require nothing special of the implementing class

• Constants in an interface can be used in the implementing class as if they were declared locally

• This feature provides a convenient technique for distributing common constant values among multiple classes

Page 19: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

11

Interfaces

• An interface can be derived from another interface, using the extends reserved word

• The child interface inherits the constants and abstract methods of the parent

• Note that the interface hierarchy and the class hierarchy are distinct

• A class that implements the child interface must define all methods in both the parent and child

Page 20: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

public class Printer2 { 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 Printer2

Page 21: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

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} // class Fileclass 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; } // constructor Text_File public String print() { return text; } // method print} // class Text_File

Page 22: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

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} // class Binary_File

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 public String print() { return new String (data); } // method print} // class Image_File

Page 23: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

interface Printable { public String name(); public String print();} // interface Printable

class Print_Logger { public void log (Printable file) { System.out.println (file.name() + " : " + file.print()); } // method log} // class Print_Logger

Page 24: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

13

Interfaces vs. abstract classes

• Note the similarities between interfaces and abstract classes

• Both define abstract methods that are given definitions by a particular class

• Both can be used as generic type names for references

• However, a class can implement multiple interfaces, but can only be derived from one class

Page 25: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

14

Interfaces

• A class that implements multiple interfaces specifies all of them in its header, separated by commas

• The ability to implement multiple interfaces provides many of the features of multiple inheritance, the ability to derive one class from two or more parents

• Java does not support multiple inheritance• See Readable_Files.java

Page 26: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

class Readable_Files implements File_Protection, Printable { private File[] files; private int[] permissions; Readable_Files (File[] file_list, int[] permissions_list) { files = file_list; permissions = permissions_list; } // constructor Readable_Files public String name() { return "Readable files"; } // method name public String print() { String printable_list = ""; for (int index = 0; index < files.length; index++) { if (permissions[index] == READ) { printable_list = printable_list + " " + files[index].name(); } } return printable_list; } } // class Readable_Files

Page 27: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

15

Packages

• A Java package is a collection of classes• The classes in a package may or may not be

related by inheritance• A package is used to group similar and

interdependent classes together• The Java API is composed of multiple packages• The import statement is used to assert that a

particular program will use classes from a particular package

Page 28: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

16

Packages

• A programmer can define a package and add classes to it

• The package statement is used to specify that all classes defined in a file belong to a particular package

• The syntax of the package statement is:

package package-name;• It must be located at the top of a file, and there can

be only one package statement per file

Page 29: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

17

Packages

• The classes must be organized in the directory structure such that they can be found when referenced by an import statement

• There is a CLASSPATH environment variable on each computer system that determines where to look for classes when referenced

• See Simple_IO_Test.java

Page 30: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

package Simple_IO; // Writer.java in Simple_IO

public class Writer { public static void write (int value) { System.out.print(value); } // method public static void write_line (String line) { System.out.println(line); } // method} // class Reader

Page 31: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

package Simple_IO; // Reader.java in Simple_IOimport java.io.*;

public class Reader { public static int read () throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); String value = stdin.readLine(); return Integer.parseInt(value); } // method public static String read_line () throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); return stdin.readLine(); } // method} // class Reader

Page 32: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

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

Page 33: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

18

Packages

• The import statement specifies particular classes, or an entire package of classes, that can be used in that program

• Import statements are not necessary; a class can always be referenced by its fully qualified name in-line

• See Simple_IO_Test2.java• If two classes from two packages have the same nam

e and are used in the same program, they must be referenced by their fully qualified name

Page 34: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

class Simple_IO_Test2 {

public static void main (String[] args) throws java.io.IOException {

int value = Simple_IO.Reader.read(); String line = Simple_IO.Reader.read_line();

Simple_IO.Writer.write (value); Simple_IO.Writer.write_line (line);

} // method main

} // class Simple_IO_Test2

Page 35: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

19

Packages

• As a rule of thumb, if you will use only one class from a package, import that class specifically

• See Simple_IO_Test3.java• If two or more classes will be used, use the * wild

card character in the import statement to provide access to all classes in the package

Page 36: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

import Simple_IO.Writer;

class Simple_IO_Test3 {

public static void main (String[] args) throws java.io.IOException {

int value = Simple_IO.Reader.read(); String line = Simple_IO.Reader.read_line();

Writer.write (value); Writer.write_line (line);

} // method main} // class Simple_IO_Test3

Page 37: 1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

Conclusion

• Abstract classes represent generic concepts in a class hierarchy• Abstract classes: polymorphism.• An abstract class can intermix abstract methods and non-abstract method• Interfaces have constants and abstract methods (public).• Interfaces: multiple inheritance• Abstract classes and interfaces: encapsulation or information hiding• A package is used to group similar or interdependent classes together