Enhanced Class Design - PowerPoint PPT Presentation

About This Presentation
Title:

Enhanced Class Design

Description:

Typically represents a 'concept' in hierarchy. Cannot be ... { public static int read() throws IOException { BufferedReader stdin =new BufferedReader ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 32
Provided by: joseph98
Category:

less

Transcript and Presenter's Notes

Title: Enhanced Class Design


1
Enhanced Class Design
  • Joe Komar

2
Enhanced Class Design Tools
  • Abstract classes
  • Interfaces
  • Packages

3
Abstract Classes
  • Typically represents a concept in hierarchy
  • Cannot be instantiated
  • Contain abstract methods
  • Some methods typically not abstract
  • Expect the children to finish the definition of
    the class
  • Format abstract methodname()
  • defines what parameters are accepted and what is
    returned

4
Abstract Classes
  • 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
  • e.g., abstract class Food
  • Children must override the abstract methods of
    the abstract parent
  • If dont override all abstract methods, the child
    must be declared abstract

5
Printer Example
File
Binary_File
Text_File
Image_File
6
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
7
Printer Example (contd)
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
8
Printer Example (contd)
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
9
Printer Example (contd)
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
10
Printer Example (contd)
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
11
Printer Example (contd)
class Print_Logger public void log (File
file) System.out.println (file.name()
" " file.print()) // method
log //class Print_Logger
12
Printer Example Output
13
Abstract Classes
Crust
Toppings
Traditional
Deep Dish
Mushroom
Sausage
Cheese
14
Interfaces
  • Collection of constants and abstract methods
  • Not classes
  • Can be used in the definition of a class
  • interface interface-name constants and methods
  • class class-name implements interface-name
    implementation of abstract methods

15
Interfaces
  • 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

16
Interfaces
  • Interface names can be used as a class type in
    a formal parameter
  • Any class that implements that interface will be
    accepted as the actual parameter
  • A class can implement more than one interface
  • Interfaces allow for multiple inheritance

17
Printer2 Example
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
18
Printer2 Example
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
No Print() method Not abstract any longer
19
Printer2 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 //constructor Text_File
public String print() return text
// print method // class Text_File
Implements the print() method name() method in
File
20
Printer2 Example
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
Doesnt implement Printable Parent to Image_File
21
Printer2 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 public String print()
return new String (data) //method
print //class Image_File
Implements print() name() in File class
22
Printer2 Example
interface Printable String name()
String print() // interface
Printable class Print_Logger public void
log (Printable file) System.out.println
(file.name() " " file.print()) //
method log //class Print_Logger
Names name() and print() methods to be implemented
interface Name
23
Printer2 Example Output
24
Abstract Classes and Interfaces
  • Used together become a powerful tool
  • Multiple Inheritance
  • Encapsulate and hide information
  • Specify what must be done not how
  • Allow systems to be built by many programmers
  • Basically, represent the heart of object
    orientation

25
Packages
  • 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
  • Include in the classpath environment variable
  • Include in the standard path
  • Name at compile and run time

26
Simple_IO Example
package Simple_IO import 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 read public static
String read_line() throws IOException
BufferedReader stdin new BufferedReader
(new InputStreamReader (System.in))
return stdin.readLine() // method
read_line //class Reader
27
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
28
Compiling and Running
  • 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.

29
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
30
Finding the Directory
  • Got your own system -- add directories in the
    standard classpath
  • At UST, store directories on diskette, then to
    compile and run
  • javac -classpath a\classpath prog.java
  • java -classpath a\classpath prog

31
Package Considerations
  • If you have a conflict with method names, use the
    fully qualified name
  • Simple_IO.Reader.read()
  • Simple_IO.Writer.write()
  • Dont then need to use an import statement
  • Note that these packages you create need to exist
    on the system on which the program runs

32
Summary
  • 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
Write a Comment
User Comments (0)
About PowerShow.com