Lab 6: Anonymous Inner Class and Reflection Solved

$24.99 $18.99

Objectives Getting familiar with anonymous inner classes syntax and definition Getting familiar with the purpose of using such classes Get to know the Java reflection mechanism Full mark: 20 points Source files Cuboid.java JavaDPExample.java 1 Introduction 1.1 Anonymous Inner Classes Anonymous classes in Java are more accurately known as anonymous inner classes. Inner class means…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Categorys:

Description

5/5 – (2 votes)

Objectives

  • Getting familiar with anonymous inner classes syntax and definition

  • Getting familiar with the purpose of using such classes

  • Get to know the Java reflection mechanism

  • Full mark: 20 points

Source files

  • Cuboid.java

  • JavaDPExample.java

1 Introduction

1.1 Anonymous Inner Classes

Anonymous classes in Java are more accurately known as anonymous inner classes. Inner class means that a class is defined inside another class. An anonymous inner class is an inner class that is declared without using a class name at all. That is, of course is why it’s called an anonymous class.

The anonymous inner classes are very useful in some situations. For example, consider a situation where you need to create the only instance of a class, without creating subclasses or performing additional tasks such as method overloading.

Regular usage of inner class in java:

public class OuterClass {

// …

public class InnerClass {

// …

}

public static void main(String[] args) {

OuterClass outerObj = new OuterClass();

OuterClass.InnerClass innerObj = outerObj.new InnerClass();

}

}

And the usage of the anonymous inner class in Java:

new SomeSuperType().method();

Here, SomeSuperType can be an interface, and then this anonymous inner class implements that interface; or it can be a class, and then the anonymous inner class extends that class.

To understand how to define and use such kind of classes, it would be better to walk through an example.

class SomeClass {

public void method() {

System.out.println(“Hello World!”);

}

}

public class OuterClass {

public static void main(String[] args) {

new SomeClass() { // This is not an instance of SomeClass,

@Override public void method() { // but an instance of the sub class of SomeClass

System.out.println(“Anonymous Hello World!”);

}

}.method();

}

}

2 Java Reflection

Unlike Python, JavaScript, etc., Java is not a dynamic programming language. It relies heavily on the type of each class/variable. However, dynamically loading classes at runtime is important for object oriented programming. So, is it possible for Java to figure out attributes and methods of some random class? And is it possible to access these attributes, or invoke these methods? The answer is yes — the reflection guarantees such requests. It can:

  • recognize any class at runtime;

  • construct an instance of any class at runtime;

  • recognize the attributes and method at runtime;

  • invoke methods of an instance at runtime.

Here is a quick example — you have used the SimpleWriter1L in Lab 1 like this:

import components.simplewriter.SimpleWriter1L;

SimpleWriter1L out

= new SimpleWriter1L();

// Initialize

out.println(“Hello

World!”);

// Invoke

out.close();

But to use reflection, you do it like this:

Class<?> simpleWriter1L = Class.forName(“components.simplewriter.SimpleWriter1L”);

// Load

Object out = simpleWriter1L.newInstance();

// Initialize

Method println = simpleWriter1L.getDeclaredMethod(“println”, String.class);

// Invoke

println.invoke(out, “Hello World!”);

Method close = simpleWriter1L.getDeclaredMethod(“close”);

close.invoke(out);

It seems reflection is more complicated, but why do we need it? One of the answers can be found in Deliverable 2. So let’s start do it.

3 Deliverable 1 — Cuboid

Suppose we have an array of cuboids:

Cuboid[] Cuboids = new Cuboid[5];

We want to sort a them: (1) by length; (2) by area; (3) by volume; and lastly (4) by length first and then area. We don’t want to change the Cuboid class.

4 Deliverable 2 — Last Call: Fix the Design Pattern

In lab 3, you were presented with a design pattern. While the pattern represented a great idea, our implementation of that idea stunk! Big time!!

  1. The conditional structure (Line 29 to Line 34) — this requires to be updated every time a new animal type enters the system (it also needs to be updated if an animal type leaves the code base — yes, those todo’s causing problems again). That is, the conditional structure is inflexible and needs to be removed.

  1. AnimalType needs to know about the animal types — crazy! It needs to know that mice are small — insane!! And it needs to know how to create each animal ( new Lion() for example) — madness!!!

Rewrite AnimalType :

  1. to avoid using a conditional structure;

  1. so it knows NOTHING about the animals it handles. It may still need to accommodate a variable that stores different animal types, but the code in AnimalType must remain CONSTANT whenever animal types are changed. HINT: think reflection.

DEMO this deliverable to the lab instructor (10 points).

Once you have fixed those two issues, our design pattern is pretty good. You can consider this new solution as industrial strength code! Hence, if you managed this — you are good to go outside into the real world and code!!

But, don’t I need to know about lambda functions, streams, bloom filters, concurrent hash map, marshalling, … ?

Lab 6: Anonymous Inner Class and Reflection Solved
$24.99 $18.99