10 Object-Oriented Approach 4: Encapsulation and Nested Classes

Object-oriented idioms for encapsulation

Nested Classes

Learning Outcomes

  1. Define and describe encapsulation

  2. Write a functional interface and functional interface implementation

  3. Differentiate between Inner classes, Local classes, and Anonymous classes

  4. Write a generic method


Exam Topics

  • Declare and instantiate Java objects including nested class objects, and explain objects' lifecycles (including creation, dereferencing by reassignment, and garbage collection)

Oracle Academy

  • No Oracle Academy this week

Textbook

  • No textbook reading this week

Tutorial / Practice Activity

  • Encapsulation Practice

Lesson Plan

Mindfulness

Most Important Concepts

Activities

Write code to utilize the following functional interface by creating a class that implements it:

interface MyInterface {


public void printIt(String text);

}

Write code to complete this program:

public static void genericDemo() {

// Create arrays of Integer, Double and Character

Integer[] intArray = { 1, 2, 3, 4, 5 };

Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };

Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };


System.out.println("Array integerArray contains:");

printArray(intArray); // pass an Integer array


System.out.println("\nArray doubleArray contains:");

printArray(doubleArray); // pass a Double array


System.out.println("\nArray characterArray contains:");

printArray(charArray); // pass a Character array

}

Functional Interface Implementation

The interface above can be implemented with a concrete class that implements the interface.

class MyInterfaceImplementation implements MyInterface {

@Override

public void printIt(String text) {

System.out.println(text);

}

}

This would then require the creation of a concrete class object in order to call the method.

MyInterfaceImplementation mii = new MyInterfaceImplementation();

mii.printIt("Lame way");