+ 3
Why error=>In below,i am trying to use anonymous inner class to provide the implementation for m1().but it shows an erro
interface It { void m1(); } class Test implements It { It i=new It()//anonymous inner class { public void m1()//implementation for m1 { System.out.println("hello"); } }; public static void main (String args[]) { Test t=new Test(); t.i.m1();//calling m1() } } /*it shows Test is not abstract and doesn't override abstract method m1().... I am not getting this error!!! help*/
12 Réponses
+ 3
The only explanation would be that the code was not exactly the same. The m1 method would have to be implemented if the Test class extended It. Most likely what happened was that the Test class on your friends computer didn't extend the It interface, then the code would also run fine:
interface It {
void m1();
}
class Test {
It i = new It() {
public void m1() {
System.out.println("hello");
}
};
public static void main (String args[]) {
Test t=new Test();
t.i.m1();
}
}
See how similar it looks to your original code?
Note the anonymous class could have also been defined using a lambda like:
It i = () -> {
System.out.println("hello");
};
+ 7
1. You don't need an inner class here.
2. Add the @Override annotation.
3. Correct the call of the m1 method.
interface It {
void m1();
}
class Test implements It {
@Override
public void m1() {
System.out.println("hello");
};
public static void main (String args[]) {
Test t=new Test();
t.m1();//calling m1()
}
}
+ 4
Your friend might have switched off the error for the @Override annotation, but I doubt the call of the method works like you posted it.
+ 4
The @Override annotation only does an extra compile time check to make sure that the following method matches the exact signature, return type etc of an inherited class and otherwise is not required to make the code valid.
The error that you were receiving was simply due to the fact the the Test class itself did not implement the required method m1() as is necessary by the implicit contract that is created between a class and its implemented interfaces. Once the method is added to the Test class itself the code will run fine. Remember that the inner anonymous class is not part of the definition of the Test class itself. However the object created from the anonymous class (It i) is. All the class knows is that there is an object that implements the interface It called i declared within the Text class.
interface It {
void m1();
}
class Test implements It {
It i = new It() {
public void m1() { // This m1 method is not seen as part of the Test class, but rather part of the anonymous class.
System.out.println("hello");
}
};
public void m1() { // Once this method is added the code is valid.
System.out.println("m1");
}
public static void main (String args[]) {
Test t=new Test();
t.i.m1();
}
}
+ 4
This:
It i = new It() {
public void m1() {
System.out.println("hello");
}
};
is the same as this:
It i = () -> {
System.out.println("hello");
};
You'll need to learn about java lambda's in order to understand. You should first have a good grasp of basic java, inheritance, interfaces, anonymous classes etc...
The inferred part(s) can be omitted.
In this case the lambda is creating an anonymous class that implements the It interface just as the previous code before it does. It just allows us to use less code. It does not replace the use of the new keyword. Its use is just inferred as is the type and the required methods signature.
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html
https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
https://www.tutorialspoint.com/java8/java8_lambda_expressions.htm
https://javabrains.io/courses/java_lambdabasics/
+ 1
yes I know@tashi.... but I was just practicing anonymous inner class. and the same programme runs great in my friend's system... but not in mine😔
+ 1
hmm.....
+ 1
thanks@ChaoticDawg... that helps me a lot.... But I still don't understand... how the same code run fine in my friend's system??
+ 1
yes.... you are right..... thanks .... u are osum ...
0
so, is () -> = new It()??? isn't??.
0
I wasn't aware of that... thanks again
0
hey, It i= () ->, is not working!! it shows an error say illegal start of expression