Download Lecture 9

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
1
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
2
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
3
Abstract Classes
• See Dinner.java
Food
Beans
Franks
Pepperoni
4
public class Dinner {
public static void main (String[] args) {
Pepperoni slice = new Pepperoni();
System.out.println (slice.slogan());
} // method main
} // class Dinner
abstract class Food {
abstract public String slogan();
} // class Food
class Pepperoni extends Food {
public String slogan() {
return "Great for pizza!";
} // method slogan
} // class Pepperoni
Abstract Classes
• See Printer.java
File
Binary_File
Text_File
Image_File
5
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
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
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
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
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
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
6
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
7
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
8
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 Philosopher
class Kant implements Philosopher {
public String pontificate() {
return "Follow the Categorical Imperitive!";
} // method pontificate
} // class Kant
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
implement one or more interfaces
9
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
10
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
11
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
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 File
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;
} // constructor Text_File
public String print() {
return text;
} // method print
} // class Text_File
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
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
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
13
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
14
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
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
15
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
16
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
17
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
package Simple_IO;
import java.io.*;
// Reader.java in Simple_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
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
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
name and are used in the same program, they must
18
be referenced by their fully qualified name
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
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 *
wildcard character in the import statement to
provide access to all classes in the package
19
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
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