Lab 10
Lab 10
OBJECT-ORIENTED PROGRAMMING
LAB 10: NESTED CLASS, DESIGN PATTERN
I. Objective
After completing this lab tutorial, you can:
• Understand how to program with Nested Classes,
• Understand how to program with Design Patterns.
• The class Outer is the external enclosing class and the class Nested is the class defined within
the class Outer.
• Nested classes are classified as static and non-static.
• Nested classes that are declared static are simply termed static nested classes whereas non-static
nested classes are termed inner classes. To access the non-static nested class, create an object of
the outer class, and then create an object of the inner class. Otherwise the static nested class
without creating an object of the outer class.
class Outer {
...
static class StaticNested {
...
}
class Inner {
...
}
}
• The class StaticNested is a nested class declared static whereas the non-static nested class, Inner,
is declared without the keyword static.
• A nested class is a member of its enclosing class.
• Non-static nested classes or inner classes can access the enclosing class members even when it
was declared private.
• Static nested classes cannot access any other member of the enclosing class.
1. Inner Class
1.1. Member Classes
// outer class
class OuterClass {
// static member
static int outer_x = 10;
// instance(non-static) member
int outer_y = 20;
// private member
private int outer_private = 30;
// inner class
class InnerClass {
void display() {
// can access the static member of the outer class
System.out.println("outer_x = " + outer_x);
}
}
}
// Driver class
public class InnerClassDemo {
public static void main(String[] args) {
// accessing an inner class
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
innerObject.display();
}
}
• Unlike an inner class, a local inner class is not a member of the outer class and therefore, it
cannot have any access specifier.
• That is, it cannot use modifiers such as public, protected, private, or static.
• However, it can access all members of the outer class, as well as final variables, declared within
the scope in which it is defined.
interface Age {
void getAge();
}
class AnonymousDemo {
public static void main(String[] args) {
@Override
public void getAge() {
// printing age
System.out.print("Age is " + x);
}
};
oj1.getAge();
}
}
// Output: Age is 21
// outer class
class OuterClass {
// static member
static int outer_x = 10;
// instance(non-static) member
int outer_y = 20;
// private member
private static int outer_private = 30;
}
}
}
// Driver class
public class StaticNestedClassDemo {
public static void main(String[] args) {
// accessing a static nested class
OuterClass.StaticNestedClass nestedObject = new
OuterClass.StaticNestedClass();
nestedObject.display();
}
}
o Behavioral Patterns
1. Singleton
• A singleton pattern is a type of creational pattern.
• The singleton design pattern provides complete information on such class implementations.
• Consider the following when implementing the singleton design pattern:
o The reference is finalized so that it does not reference a different instance.
o The private modifier allows only same class access and restricts attempts to instantiate
the singleton class.
o The factory method provides greater flexibility. It is commonly used in singleton
implementations.
o The singleton class usually includes a private constructor that prevents a constructor to
instantiate the singleton class.
o To avoid using the factory method, a public variable can be used at the time of using a
static reference.
class SingletonExample {
private static SingletonExample singletonExample = null;
private SingletonExample() {
}
2. Factory Pattern
• It is one of the commonly used design patterns in Java.
• It belongs to the creational pattern category.
• This pattern does not perform direct constructor calls when invoking a method.
IV. Exercises
1. Student class contains the information name, address, sex, and score. The Student class has a
nested class StudentOperator, that has two methods print() and type():
• The print() method will print the following information:
Student [" name "," address "," sex "," score "]
• The type() method will return a student's rank with a data type of String.
o If the score > 8 then return A,
o If 5 <= score <= 8 then return B,
o If the score < 5 then return C.
A loadImage() method just needs to return the String “Loaded successfully.” And write a main
method to test your program.
3. Implement the program as the diagram below.
- getInfo(): this method returns the value of all properties of the class.
-- END--