Java Programming Labs

Master annotations for metadata, reflection for runtime inspection, and inner classes for encapsulation.

Annotations, Reflection & Inner Classes - Module 8

Learn advanced Java features for metadata, introspection, and nested types.

Lab 22: Annotations
Expert
Coding Challenge
Your Task: Learn to use built-in annotations and create custom annotations for metadata.

Detailed Requirements:
1. Built-in annotations:
   • @Override - method overrides superclass method
   • @Deprecated - marks element as deprecated
   • @SuppressWarnings - suppress compiler warnings
   • @FunctionalInterface - single abstract method interface

2. Create custom annotation:
   @interface MyAnnotation { String value(); }

3. Meta-annotations:
   • @Retention(RetentionPolicy.RUNTIME)
   • @Target(ElementType.METHOD)

Expected Output:
--- Built-in Annotations --- Using @Override on toString() Dog says: Woof! --- Custom Annotation --- Method has @Info annotation Author: John Doe Version: 1.0

Requirements Checklist

Use @Override annotation
Use @Deprecated or @SuppressWarnings
Create custom annotation with @interface
Add @Retention meta-annotation
Add @Target meta-annotation
Apply custom annotation to method
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Custom: @interface MyAnnotation { String value(); }
• Retention: @Retention(RetentionPolicy.RUNTIME)
• Target: @Target(ElementType.METHOD)
• Default: String version() default "1.0";
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 23: Reflection API
Expert
Coding Challenge
Your Task: Use Java Reflection API to inspect and manipulate classes, methods, and fields at runtime.

Detailed Requirements:
1. Get Class object:
   • Class<?> clazz = MyClass.class;
   • Class<?> clazz = obj.getClass();
   • Class<?> clazz = Class.forName("MyClass");

2. Inspect class members:
   • clazz.getDeclaredFields() - all fields
   • clazz.getDeclaredMethods() - all methods
   • clazz.getConstructors() - constructors

3. Create instance dynamically:
   • clazz.getDeclaredConstructor().newInstance()

4. Invoke methods:
   • method.invoke(object, args)

Expected Output:
--- Class Information --- Class name: Person Fields: name age Methods: getName setName getAge setAge greet --- Create Instance --- Created: Person[name=John, age=25] --- Invoke Method --- Hello, my name is John!

Requirements Checklist

Get Class object using .class or getClass()
Use getDeclaredFields() to list fields
Use getDeclaredMethods() to list methods
Create instance with newInstance()
Use method.invoke() to call method
Handle ReflectiveOperationException
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Get class: Class<?> c = MyClass.class;
• Fields: c.getDeclaredFields()
• Methods: c.getDeclaredMethods()
• Create: c.getConstructor(types).newInstance(args)
• Invoke: method.invoke(object, args)
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 24: Inner Classes
Advanced
Coding Challenge
Your Task: Master different types of inner classes in Java for better encapsulation and code organization.

Detailed Requirements:
1. Member Inner Class:
   • Defined inside class, non-static
   • Access: outer.new Inner()

2. Static Nested Class:
   • Defined with static keyword
   • Access: new Outer.StaticNested()

3. Local Inner Class:
   • Defined inside a method

4. Anonymous Inner Class:
   • No name, defined and instantiated at once
   • Often used with interfaces

Expected Output:
--- Member Inner Class --- Inner class accessing outer: Hello from Outer Inner value: 10 --- Static Nested Class --- Static nested class value: 42 --- Local Inner Class --- Local class says: Hello Local! --- Anonymous Inner Class --- Anonymous greeting: Hello World!

Requirements Checklist

Create member inner class
Instantiate inner class with outer.new Inner()
Create static nested class
Create local inner class in method
Create anonymous inner class
Use interface with anonymous class
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Member inner: class Outer { class Inner { } }
• Create inner: Outer.Inner i = outer.new Inner();
• Static nested: new Outer.StaticNested();
• Anonymous: new Interface() { @Override... };
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below