10 Object-Oriented Approach 4: Encapsulation and Nested Classes
Object-oriented idioms for encapsulation
Nested Classes
Learning Outcomes
Define and describe encapsulation
Write a functional interface and functional interface implementation
Differentiate between Inner classes, Local classes, and Anonymous classes
Write a generic method
Resources
Videos
Public, Private, Protected (19:57)
Encapsulation and the API Docs (11:17)
Anonymous Classes (8:57)
Inner (Nested) Classes (16:33)
Using Generics (12:40)
Generics and Wildcards (17:50)
How to use Nested Classes in Java (Static, Inner, Local, and Anonymous) (40:07)
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
Previous week review
Resource reinforcement and clarification
Encapsulation
Controlling access
Packages
Main.demoNestedClass in Sandbox
Main.demoLocalClass in Sandbox
Activity: Functional interface (definition below) implementation (code below)
anonymous demo in Sandbox
Activity printArray (code below)
class Generics in Sandbox
ArrayList
Mindfulness
Most Important Concepts
Encapsulation definitions
From OCP Oracle Certified Professional Java SE 11 Developer Complete Study Guide
Encapsulation means only methods in the class with the variables can refer to the instance variables.
Encapsulation refers to preventing callers from changing the instance variables directly. This is done by making instance variables private and getters/setters public.
From SEVOCAB
(1) software development technique that consists of isolating a system function or a set of data and operations on those data within a module and providing precise specifications for the module (ISO/IEC/IEEE 24765:2017 Systems and software engineering-Vocabulary)
(2) concept that access to the names, meanings, and values of the responsibilities of a class is entirely separated from access to their realization (IEEE 1320.2-1998 (R2004) IEEE Standard for Conceptual Modeling Language Syntax and Semantics for IDEF1X97 (IDEFobject), 3.1.54)
(3) idea that a module has an outside that is distinct from its inside, that it has an external interface and an internal implementation (ISO/IEC/IEEE 24765:2017 Systems and software engineering-Vocabulary)
Nested classes
A functional interface in Java is an interface that contains only a single abstract (unimplemented) method.
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");