Anonymous inner class - getClass() - java

I have written the following code:
public class AnonymousClasses {
public void sayHello(){
}
public interface GreetingModule {
public void sayHello();
}
public static void main(String[] args) {
AnonymousClasses jonny = new AnonymousClasses(){
#Override
public void sayHello() {
System.out.println("Hey");
}
};
AnonymousClasses john = new AnonymousClasses(){
#Override
public void sayHello() {
System.out.println("Hi");
}
};
GreetingModule greeting = new GreetingModule() {
#Override
public void sayHello() {
System.out.println("Hello");
}
};
jonny.sayHello();
john.sayHello();
greeting.sayHello();
System.out.println(jonny.getClass());
System.out.println(john.getClass());
System.out.println(greeting.getClass());
}
The output is of course:
Hey
Hi
Hello
class AnonymousClasses$1
class AnonymousClasses$2
class AnonymousClasses$3
However, when I edit my code as follows:
public class AnonymousClasses {
private final GreetingModule greetingModule;
public AnonymousClasses(GreetingModule greetingModule) {
this.greetingModule = greetingModule;
}
public void saySomething() {
greetingModule.sayHello();
}
public interface GreetingModule {
public void sayHello();
}
public static void main(String[] args) {
AnonymousClasses jonny = new AnonymousClasses(new GreetingModule(){
#Override
public void sayHello() {
System.out.println("Hey");
}
});
AnonymousClasses john = new AnonymousClasses(new GreetingModule(){
#Override
public void sayHello() {
System.out.println("Hi");
}
});
GreetingModule greeting = new GreetingModule() {
#Override
public void sayHello() {
System.out.println("Hello");
}
};
jonny.saySomething();
john.saySomething();
greeting.sayHello();
System.out.println(jonny.getClass());
System.out.println(john.getClass());
System.out.println(greeting.getClass());
}
the output is:
Hey
Hi
Hello
class AnonymousClasses
class AnonymousClasses
class AnonymousClasses$3
Can somebody explain me, why there is no name of the inner class with a dolar in the first two getClass() methods? Is it still an inner class?

You are creating instances of the class on the second sample:
AnonymousClasses jonny = new AnonymousClasses(new GreetingModule(){
#Override
public void sayHello() {
System.out.println("Hey");
}
});
To create a anonymous subclass of it would like:
AnonymousClasses jonny = new AnonymousClasses(new GreetingModule(){
#Override
public void sayHello() {
System.out.println("Hey");
}
}) {
// Overriding anything here is optional
};

$ symbol comes with the class name when there is a new unnamed implementation of the class.
In the first example , you are creating new implementations of AnonymousClasses in each case.
AnonymousClasses jonny = new AnonymousClasses(){
#Override
public void sayHello() {
System.out.println("Hey");
}
};
It's similar to creating an object of a subclass with AnonymousClasses as the parent class. In this case, since there is no name for the subclass, JVM appends $1 to its parent's name to create a unique name.
In the second example , you are just creating new objects of same implementation of AnonymousClasses. It might seem similar, but if you notice carefully, each time you are creating a new implementation of GreetingModule interface which is passed to the constructor of AnonymousClasses.
In both the cases, there is no inner class. In first example, they are just the objects of subclasses of AnonymousClasses and in second case, they are objects of the same AnonymousClasses.

Related

Java - How to call method class with interface without know class name

I'm new in java, I want to call method class from implemented Class with interface without know class name "ClassA", which only know Object c and I have 2 file.
File (1) CobaInterface.java
package cobainterface;
public class CobaInterface {
public static void main(String[] args) {
ImplementedClass implementedClass = new ImplementedClass();
ClassA clsA = new ClassA();
implementedClass.myMethodFromClassA(clsA);
}
}
class ClassA{
public Integer getTwo(){
return 2;
}
}
interface MyInterface {
public void myMethod();
//here interface
public void myMethodFromClassA(Object c);
}
File (2) : ImpementedClass.java
package cobainterface;
public class ImplementedClass extends CobaInterface {
public void myMethodFromClassA(Object c) {
//System.out.println(c.getTwo()); <- wrong when call method c.getTwo()
}
}
How about if I want to call method getTwo() from ClassA without know Class Name, which only know Object c from file (2) as describe in code above. Thanks for advance.
You should use generic types so the implementation knows what the object will be,
interface MyInterface<T> {
public void myMethod();
//here interface
public void myMethodFromClassA(T c);
}
The impl becomes,
package cobainterface;
public class ImplementedClass Implements MyInterface<ClassA> {
public void myMethodFromClassA(ClassA c) {
//System.out.println(c.getTwo()); <- wrong when call method c.getTwo()
}
}
All together,
class Scratch {
public static void main(String[] args) {
ImplementedClass implementedClass = new ImplementedClass();
ClassA clsA = new ClassA();
implementedClass.myMethodFromClassA(clsA);
}
}
class ImplementedClass implements MyInterface<ClassA> {
#Override
public void myMethod() {
}
#Override
public void myMethodFromClassA(ClassA c) {
System.out.println(c.getTwo());
}
}
class ClassA {
public Integer getTwo() {
return 2;
}
}
interface MyInterface<T> {
void myMethod();
void myMethodFromClassA(T c);
}
You could also do a cast
System.out.println((MyClass)c.getTwo());
but you will lose all benefit of type saftey.

How to pass down the new operator in a method

For example, if I wanted to do something like this to call a method:
myLights.addLight(new Fluorescent(lumens));
in order to create a new object in the Fluorescent class and pass down the lumens data. How would I then set up the method to receive this?
Assuming method is not returning anything.
void addlight(Fluorescent a){
// your logic
}
In your Lights class create a method that accepts a Fluorescent object as an argument.
public void addLight(Fluorescent fluorescent){
// do something
}
Here is a basic example:
public class HelloWorld
{
public static void main(String[] args)
{
Light light = new Light();
light.addLight(new Fluorescent("300 lm"));
System.out.print(light.getLumen());
}
}
public class Light {
private String lumen;
public Light() {
}
public void setLumens(String lumen){
this.lumen = lumen;
}
public String getLumen(){
return this.lumen;
}
public void addLight(Fluorescent fluorescent) {
if(fluorescent.getLumen() != null) {
this.lumen = fluorescent.getLumen();
}
}
}
public class Fluorescent {
private String lumen;
public Fluorescent(String lumen){
this.lumen = lumen;
}
public void setLumen(String lumen){
this.lumen = lumen;
}
public String getLumen(){
return this.lumen;
}
}
Seeing that a Fluorescent is a Light, you might want to look in to inheritance.
Look here for some explanation
Java 101: Inheritance in Java, Part 1
public class Fluorescent() {
public Fluorescent(String lumens) {
// do something
}
}
public class Lights() {
public void addLight(Fluorescent fluorescent) {
// do something
}
}

writing good abstract classes in java

I have the following need and please help me to write good and abstract class.
Different types of operations is needed based on the type
I have a abstract class,
abstract public class FileHelper{
//Template method
//This method defines a generic structure for parsing data
public void parseDataAndGenerateFile(String fileDownloadType)
{
createHeader(fileDownloadType);
generateFile();
}
//We have to write output in a excel file so this step will be same for all subclasses
public void createHeader(String fileDownloadType)
{
System.out.println('Creating HEADER in EXCEL');
}
public void generateFile(String fileDownloadType)
{
System.out.println('Output generated,writing to XLX');
}
}
public class ExcelDataParser extends FileHelper {
String fileDownloadType="";
}
public class TemplateMethodMain {
public static void main(String[] args) {
String fileDownloadType="expired";
ExcelDataParser csvDataParser=new ExcelDataParser();
csvDataParser.parseDataAndGenerateFile(fileDownloadType);
}
}
Please help me and correct me to have a good way of doing this.
If you want to use an abstract base class, you better should declare an abstract method String getDownloadType() in your abstract base class. These method must be overridden by the derived classes and the type could be fix in the derived class.
For example:
abstract public class FileHelper {
abstract String getFileDownloadType();
public void parseDataAndGenerateFile() {
createHeader();
generateFile();
}
public void createHeader() {
if ("expired".equals(getFileDownloadType())) {
} else {
}
}
public void generateFile() {
if ("expired".equals(getFileDownloadType())) {
} else {
}
}
}
public class ExcelDataParser extends FileHelper {
#Override
String getFileDownloadType() {
return "expired";
}
}
public class TemplateMethodMain {
public static void main(String[] args) {
ExcelDataParser csvDataParser = new ExcelDataParser();
csvDataParser.parseDataAndGenerateFile();
}
}
But if you don't need a class for every type, you also could make the type a variable inside a single class and passing the type to the contructor
For example:
public class CsvFileHelper {
private final String fileDownloadType;
public CsvFileHelper(String type) {
fileDownloadType = type;
}
public void parseDataAndGenerateFile() {
createHeader();
generateFile();
}
public void createHeader() {
if ("expired".equals(fileDownloadType)) {
} else {
}
}
public void generateFile() {
if ("expired".equals(fileDownloadType)) {
} else {
}
}
}
public class TemplateMethodMain {
public static void main(String[] args) {
CsvFileHelper csvDataParser = new CsvFileHelper("expired");
csvDataParser.parseDataAndGenerateFile();
}
}

Passing parameter to anonymous class in Java

i'm trying to write anonymous inner class
interface Face{
void seeThis(String what);
}
class Eyes {
public void show(Face f){}
}
public class Seen {
public void test() {
Eyes e = new Eyes();
e.show(new Face() {
#Override
public void seeThis(String what){
System.out.print(what);
}
});
public static void main(String[] args) {
Seen s = new Seen();
s.test();
}
}
How to call seeThis() and how to pass parameter to it?
Method seeThis() belongs to Face class, which instance is anonymous and thus cannot be reached without storing reference to it. If you want to store a reference, you can do this in the following way:
public class Seen {
public Face face;
....
this.face = new Face() { ... };
e.show(this.face);
And then,
Seen s = new Seen();
s.face.seeThis();
Now, regarding passing the parameter. You have two options - declare parameter outside of anonymous class and make it final in order to be reachable by this anonymous class, or replace anonymous class with normal one and pass the parameter to its constructor:
Approach one:
final int parameter = 5;
...(new Face() {
#Override
public void seeThis() {
System.out.println(parameter);
}
});
Approach two:
public class MyFace implements Face() {
private final int parameter;
public MyFace(int parameter) {
this.parameter = parameter;
}
#Override
public void seeThis() {
System.out.println(parameter);
}
}
Then,
...
e.show(new MyFace(10));

Puzzle on Inner Classes and Class Hiding

I have a sample code to solve which is based on inner classes:
package inner;
class A {
void m() {
System.out.println("Outer");
}
}
public class TestInner {
public static void main(String[] args) {
new TestInner().go();
}
private void go() {
new A().m();
class A{
void m(){
System.out.println("Inner");
}
}
new A().m();
}
class A{
void m(){
System.out.println("Middle");
}
}
}
The output given by above sample code is:
Middle
Inner
And my question is, given that I dont want to use the package name to create an object, how can I print the output as:
Outer
Inner
Since using a package is so obviously the answer, I assume you are looking for something obtuse.
You can add an outer class and call that.
class B extends A { }
// in TestInner.go()
new B().m();
class A{
void m(){
System.out.println("Inner");
}
}
new A().m();
public class TestInner {
public static void main(String[] args) {
new TestInner().go();
}
private void go() {
new inner.A().m(); //will produce output "Outer"
class A{
void m(){
System.out.println("Inner");
}
}
new A().m(); //will produce output "Inner"
}
class A{
void m(){
System.out.println("Middle");
}
}

Categories

Resources