Professional Documents
Culture Documents
Objectives
Slide 1 of 29
Ver. 1.0
Programming in Java
When to Avoid Constants
Slide 2 of 29
Ver. 1.0
Programming in Java
Typesafe Enumerations
Enums:
Created using a variation of a java classes
Provide a compile-time range check
The following code snippet demonstrates how to declare
and use enum: The PowerState enum is
public enum PowerState{ declared using the enum
OFF, keyword.
These are references to the
ON, only three PowerState objects
SUSPEND; that can exist.
} This method takes
. . . a PowerState
Computer comp = new Computer(); reference.
comp.setState(PowerState.SUSPEND);
Slide 3 of 29
Ver. 1.0
Programming in Java
Enum Usage
Enums:
References can be statically imported, as shown in the
following code snippet:
import static com.example.PowerState.*;
public class Computer extends ElectronicDevice{
private PowerState powerState = OFF;
//...} PowerState.OFF
Can be used as an expression in the switch statements, and
enum constants can be used as labels for the case statements,
as shown in the following code snippet: public void
setState(PowerState state) {
switch(state) {
case OFF:
//...}}
Slide 4 of 29
Ver. 1.0
Programming in Java
Activity: BeforeEnums
Slide 5 of 29
Ver. 1.0
Programming in Java
Activity: AfterEnums
Slide 6 of 29
Ver. 1.0
Programming in Java
Complex Enums
Enums
Slide 7 of 29
Ver. 1.0
Programming in Java
Activity: ComplexEnums
Slide 8 of 29
Ver. 1.0
Programming in Java
Design Patterns
Design patterns:
Reusable solutions to common software development
problems
Documented in pattern catalogs
Have a vocabulary to discuss design
Slide 9 of 29
Ver. 1.0
Programming in Java
Implement Singleton Pattern
Singleton design
pattern
Nested classes:
Declared within the body of another class
Streamline packages
Commonly used in applications with GUI elements
Limit the access of the helper classes to the outer classes
Help to implement encapsulation in java
Nested classes can be of the following types:
Inner classes:
Can further be classified into:
Member classes
Local classes
Anonymous classes
Static nested classes
Slide 11 of 29
Ver. 1.0
Programming in Java
Inner Class: Example
Inner class:
Part of the outer class
Inherits access to all the private members of the outer class
Can be declared inside a method
The following code snippet creates the inner class, Engine:
public class Car { Car is an outer class.
private boolean running = false;
private Engine engine = new Engine();
private class Engine { Engine is an inner class.
public void start() {
running = true;
}}
public void start() {
engine.start();
}}
Slide 12 of 29
Ver. 1.0
Programming in Java
Inner Class: Example (Contd.)
Slide 13 of 29
Ver. 1.0
Programming in Java
Activity: NestedClassExamples
Slide 14 of 29
Ver. 1.0
Programming in Java
Anonymous Inner Classes
Anonymous class:
Class with no name
The following code show an anonymous inner class:
public class AnonymousExampleClass{
public Object o = new Object() Here, the java.lang.Object class is
{ instantiated and is later subclassed.
@Override
public String toString()
{
return "In an anonymous class method";
}
};
}
Slide 15 of 29
Ver. 1.0
Programming in Java
Anonymous Inner Classes (Contd.)
Slide 16 of 29
Ver. 1.0
Programming in Java
Abstraction
Abstraction:
Enables ease of maintenance
Enables implementation substitution
Enables division of labor
Implemented using the abstract classes and interfaces
Helps to avoid dependency on specific implementing classes
Reduces refactoring
Slide 17 of 29
Ver. 1.0
Programming in Java
Java Interfaces
Interfaces:
Used to define abstract types
Outline a contract for a class
Similar to abstract classes containing only public abstract
methods
Can contain constant fields
Can be used as a reference type
Essential component of many design patterns
Slide 18 of 29
Ver. 1.0
Programming in Java
Developing Java Interfaces
implements keyword:
Used to implement an interface
The following embedded Word document shows how to
declare and implement an interface.
Interface
Slide 19 of 29
Ver. 1.0
Programming in Java
Quiz
Slide 20 of 29
Ver. 1.0
Programming in Java
Quiz (Contd.)
Solution:
An inner class can be declared inside a method block called
local classes.
Slide 21 of 29
Ver. 1.0
Programming in Java
Quiz (Contd.)
Solution:
Interfaces are used to define abstract types.
Slide 22 of 29
Ver. 1.0
Programming in Java
Activity: ElectronicDeviceInterface
Slide 23 of 29
Ver. 1.0
Programming in Java
Constant Fields
Interface fields:
public, static, and final implicitly
Must be constants
The following code snippet shows how to declare a constant
field in an interface:
public interface ElectronicDevice
{
public static final String WARNING =
"Do not open, shock hazard";
public void turnOn();
public void turnOff();
}
Slide 24 of 29
Ver. 1.0
Programming in Java
Interface References
An interface reference:
Used to refer to an object that implements that interface
Can not be used as a reference type for the object that does
not implement an interface directly
Can use only the methods outlined in the interface, as shown
in the following code snippet:
ElectronicDevice ed = new Television();
ed.turnOn();
ed.turnOff();
ed.changeChannel(2); // fails to compile
String s = ed.toString();
Slide 25 of 29
Ver. 1.0
Programming in Java
instanceof Operator
instanceof operator:
Used with interfaces
Can use any reference type as an operand, as shown in the
following code snippet: Here, the Television class implements the
ElectronicDevice interface. Therefore, Television is an
instance of Television, ElectronicDevice, and
java.lang.Object.
Television t = new Television(); if (t instanceof
ElectronicDevice){}
The following diagram explains the preceding concept.
Implements
Slide 26 of 29
Ver. 1.0
Programming in Java
Casting to Interface Types
Slide 27 of 29
Ver. 1.0
Programming in Java
Marker Interfaces
Marker interface:
Defines a type that must be implemented by a class
Determines if an object can have its state serialized or not
Allows type checking
Does not require method implementations
java.io.Serializable, is used by the Java I/O library
The following code snippet shows how to use the
Serializable interface:
public class Person implements java.io.Serializable{}
The following code snippet shows how to use the
instanceof operator:
Person p = new Person();
if (p instanceof Serializable) {}
Slide 28 of 29
Ver. 1.0
Programming in Java
Summary