Override Constructor of anonymous classes

Discussion in 'Plugin Development' started by narrowtux, Jun 1, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    narrowtux

    Hi,
    a theoretical question:
    Can I Override the constructor of a class when I virtually reimplement this class?
    I mean this:
    Code:
    Class c = new Class(){
       public void foo(){
          bar();
       }
    };
    
    Is there a way to reimplement the constructor there?
     
  2. Offline

    Jayjay110

    lol, disregard this post, its just me talking crap:

    the only thing I understand in that question is class, the rest is all jibberish, my java fails :(
     
  3. Offline

    masteroftime

    I'm not completely sure what you want but i guess that you want to implement an abstract class or interface right when you instantiate it (anonymous class).

    What I don't understand is:
    1. Why do you create a Class object? Does this just mean any class or really the Java Class?
    2. Why do you want to implement a constructor? An anonymous class can only be created in one place so there's basically no need to have a constructor. So its not possible to have one.
    As I said I don't really understand what you need that for or how you want to use it. So if my guesses were wrong please post more details here.
     
  4. Offline

    narrowtux

    Was just an example, I have a custom class in my source ;)
    I didn't knew the exact name for this kind of objects :D
    So now I know that this is called an anonymous class.
     
  5. Offline

    The_Tree_Maker

  6. Offline

    narrowtux

    So after going through this very unstructured code, I now know how:
    Code:
    XYClass class = new XYClass(){
    {
    //this is the constructor
    }
    public void foo(){
    bar();
    }
    };
    Thank you :)
     
  7. Offline

    The_Tree_Maker

    That's cool dude. You could almost write a program to rewrite itself. Kind of like a Polymorphic Virus.
     
Thread Status:
Not open for further replies.

Share This Page