R
Reporter
I read that an anonymous class is an unnamed local class.
However, in the example below, I have created what I believe to be an
anonymous class that is a member class, not a local class. Anyone
want to comment?
package anonymousexample3a;
public class Main {
ClassA anonB_Object = new ClassA("Hello" ) {
// The original Display Method
public void Display(){
System.out.println("Inherited Anonymous Overridden MEMBER
Display ClassA Output:"+Message);
}
};
public Main() {
ClassA classA_Object = new ClassA("Hello");
// Display the Anonymouse Version
classA_Object.Display();
ClassA anonA_Object = new ClassA("Hello" ) {
// The original Display Method
public void Display(){
System.out.println("Inherited Anonymous Overridden
LOCAL Display ClassA Output:"+Message);
}
};
// Display the Anonymouse Class Based on ClassA
anonA_Object.Display();
anonB_Object.Display();
}
public static void main(String[] args) {
Main example = new Main();
}
}
class ClassA {
String Message = "";
public ClassA(String aMessage){
Message = aMessage;
}
// The original Display Method
public void Display(){
System.out.println("Basic ClassA Output:"+Message);
}
}
However, in the example below, I have created what I believe to be an
anonymous class that is a member class, not a local class. Anyone
want to comment?
package anonymousexample3a;
public class Main {
ClassA anonB_Object = new ClassA("Hello" ) {
// The original Display Method
public void Display(){
System.out.println("Inherited Anonymous Overridden MEMBER
Display ClassA Output:"+Message);
}
};
public Main() {
ClassA classA_Object = new ClassA("Hello");
// Display the Anonymouse Version
classA_Object.Display();
ClassA anonA_Object = new ClassA("Hello" ) {
// The original Display Method
public void Display(){
System.out.println("Inherited Anonymous Overridden
LOCAL Display ClassA Output:"+Message);
}
};
// Display the Anonymouse Class Based on ClassA
anonA_Object.Display();
anonB_Object.Display();
}
public static void main(String[] args) {
Main example = new Main();
}
}
class ClassA {
String Message = "";
public ClassA(String aMessage){
Message = aMessage;
}
// The original Display Method
public void Display(){
System.out.println("Basic ClassA Output:"+Message);
}
}