Lab 10
Lab 10
OBJECT-ORIENTED PROGRAMMING
LAB 10: NESTED CLASS, DESIGN PATTERN
I. Objective
After completing this first lab tutorial, you can:
class Outer{
...
class Nested{
...
}
}
• The class Outer is the external enclosing class and the class Nested is the class defined within
the class Outer.
• Nested classes that are declared static are simply termed as static nested classes whereas non-
static nested classes are termed as 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 that has been declared as static whereas the non-static
nested class, Inner, is declared without the keyword static.
• Non-static nested classes or inner classes can access the members of the enclosing class even
when they are declared as private.
• Static nested classes cannot access any other member of the enclosing class.
1. Inner Class
1.1. Member Classes
• The member class cannot have static modifier since it is associated with instances of the outer
class.
• An inner class can directly access all members that is, fields and methods of the outer class
including the private ones.
• However, the outer class cannot access the members of the inner class directly even if they are
declared as public.
• This is because members of an inner class are declared within the scope of inner class.
• To instantiate an inner class, one must create an instance of the outer class.
// 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 static member of 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();
}
}
• The scope of a local inner class is only within that particular block.
• 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.
o It can access any local variables, method parameters, or exception parameters that are
in the scope of the local method definition, provided that these are declared as final.
class OuterClass {
int x = 10;
// Output: 15
• An anonymous class does not have a name associated, so it can be accessed only at the point
where it is defined.
• It cannot use the extends and implements keywords nor can specify any access modifiers, such
as public, private, protected, and static.
• Since, an anonymous class does not have a name, it cannot have a named constructor but it can
have an instance initializer.
• Rules for accessing an anonymous class are the same as that of local inner class.
• Usually, anonymous class is an implementation of its super class or interface and contains the
implementation of the methods.
• They can access the internal or private members and methods of the outer class.
• Anonymous class is useful for controlled access to the internal details of another class.
• Also, it is useful when a user wants only one instance of a special class.
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
• A static nested class cannot directly refer to instance variables or methods of the outer class just
like static methods but can access only through an object reference.
• A static nested class, by behavior, is a top-level class that has been nested in another top-level
class for packaging convenience.
• Static nested classes are accessed using the fully qualified class name, that is,
OuterClass.StaticNestedClass.
• A static nested class can have public, protected, private, default or package private, final, and
abstract access specifiers.
// Java program to demonstrate accessing
// a static nested class
// 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 Structural Patterns
o Behavioral Patterns
1. Singleton
• Singleton pattern is a type of creational pattern.
• The singleton design pattern provides complete information on such class implementations.
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.
• This pattern does not perform direct constructor calls when invoking a method.
• The following figure shows the factory pattern diagram:
IV. Exercises
1. Student class contains the information name, address, sex, score. Student has a nested class
StudentOperator, that has two methods print() and type():
• The type() method will return a student's rank with a data type of String.
o If score> 8 then return A,
o If 5 <= score <= 5 then return B,
- move(): If vehicle is car then prints to screen “Car is moving”. If vehicle is truck then prints to
screen “Truck is moving”.