You are on page 1of 29

Programming in Java

Objectives

In this session, you will learn to:


Use constants and enums
Explore design patterns
Implement the singleton design
Distinguish between top-level and nested classes
Implement abstraction using interfaces
Work with Java interfaces

Slide 1 of 29
Ver. 1.0
Programming in Java
When to Avoid Constants

Constants may perform input validation or value range


checking, as shown in the following code snippet:
Computer comp = new Computer();
comp.setState(Computer.POWER_SUSPEND);
This is an int constant
that equals 2.
Here, the if statement of the setState() method can be used to validate that only 0,
1, or 2 value is accepted, resulting in additional overhead.

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

The following embedded Word document shows that enums


can have fields, methods, and private constructors.

Enums

The new keyword is used to instantiate 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

The singleton design pattern is a type of the creational


design patterns.
Steps to implement the singleton design pattern:
1. Use a static reference to point to the single instance.
2. Add a single private constructor to the singleton class.
3. Use a public factory method that returns a copy of the
singleton reference.
The following embedded Word document explains the
preceding three points.

Singleton design
pattern

To obtain a singleton reference, call the getInstance()


method, as shown in the following code snippet:
SingletonClass ref = SingletonClass.getInstance();
Slide 10 of 29
Ver. 1.0
Programming in Java
Nested Classes

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.)

Static nested class:


Not considered as an inner class
Can be declared like the nested classes with an additional
static modifier
Can be instantiated before the enclosing outer class
Can not be accessed to all non-static members of the
enclosing class

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.)

When the preceding code is compiled, a separate class file


is generated.
Here, 1 is the
This class file has the following naming convention: index number of
Outer$1.class the anonymous
class in an
Anonymous inner classes can also be local classes. enclosed class,
and Outer is the
name of the
enclosed class.

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

Rules for access modifiers:


For methods of an interface:
abstract public by default
Cannot be declared as private or protected

Slide 19 of 29
Ver. 1.0
Programming in Java
Quiz

Slide 20 of 29
Ver. 1.0
Programming in Java
Quiz (Contd.)

Which of the following statements is correct about inner


classes?
A static nested class is considered as an inner class.
An inner class does not have access to the private members
of the outer class.
An inner class cannot be declared inside a method.
An inner class can be declared inside a method block called
local classes.

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.)

Which of the following statements is correct about


interfaces?
Interfaces cannot contain constant fields.
To implement interface, the extends keyword is used.
Interfaces are used to define abstract types.
Interfaces cannot be used as a reference type.

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

A subtype can be casted to an interface type, as shown in the


following code snippet:
public static void turnObjectOn(Object o){
if (o instanceof ElectronicDevice){
ElectronicDevice e = (ElectronicDevice)o;
e.turnOn(); Here, the turnObjectOn() method operates only on
} ElectronicDevice. This is poor designing.
}
The method for the preceding code snippet can be rewritten
as:
public static void turnObjectOn(ElectronicDevice e){
e.turnOn();
}

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

In this session, you learned that:


Enums provide a compile-time range check.
The new keyword is used to instantiate enums.
Design patterns are reusable solutions to common software
development problems.
The singleton design pattern is a type of the creational design
patterns.
Nested classes are declared within the body of another class.
An inner class is considered part of the outer class.
An anonymous class is a class with no name.
Abstraction enables ease of maintenance.
Interfaces are used to define abstract types.
Interface fields are public, static, and final implicitly.
You can use the instanceof operator with interfaces.
A marker interface defines a type that must be implemented
by a class.
Slide 29 of 29
Ver. 1.0

You might also like