0

I do not think this kind of OOP pattern exist in C#.net and C++. So what is the name of this pattern or feature. I encountered this when programming Android, is this a feature of Java too or not? Note : I know @Override is specific for android compiler/ide only.

Foo bar = new Foo(){
    @Override
    public void fighters(){
          //some stuff
    }
};

Is this somekind of Lambda thing?

Thanks!

wembikon
  • 91
  • 2
  • 9
  • You mean the "@Override"? I'm sure that's in C# too. You're just telling the compiler that fighters() overrides a superclass's method. – markspace Aug 28 '14 at 07:16
  • 2
    Look up "anonymous class" or "anonymous inner class" – laalto Aug 28 '14 at 07:16
  • It's just an inner class in Java. – RossC Aug 28 '14 at 07:17
  • And @Override is not just in "Android compiler/ide". – Stephane Mathis Aug 28 '14 at 07:18
  • 1
    @StephaneMathis : Actually `@Override` is primarily only used to detect errors within the code and is not actually necessary for Java syntax. Any method in an extended class which has the same signature as a method in the super-class will, in fact, override the super-class method with or without `@Override`. – Squonk Aug 28 '14 at 07:23
  • @Squonk, Oh, ok... I always saw it (not only in Android but for J2EE website and other things) so I assumed it was mandatory... At least in C# the override keyword is mandatory, I think. – Stephane Mathis Aug 28 '14 at 07:29
  • @StephaneMathis : Actually `override` in C# is an access modifier and yes, it is required in certain situations (overriding virtual or abstract methods) but it's also possible to 'hide' methods in C# which is subtly different and allows defining methods with the same signature as in the super-class but defining them with the `new` keyword. – Squonk Aug 28 '14 at 07:48
  • And it's got nothing to do with Lambda's – CocoNess Aug 28 '14 at 07:53
  • @laalto You answered my question perfectly precisely. Thank you! I am only beginning with Java/Android and I think these kind of Java features are breaking the design pattern I am used to. I am a C++/C# programmer. – wembikon Aug 29 '14 at 03:06

1 Answers1

2

This is example of inner class in Java. This is an anonymous inner class, where

An anonymous class is an inner class that does not have a name at all. And whose instance is being created at the time of its creation.


Read the Anonymous Classes documentation for more detail. And Override is Java annotation (not specific to Android). Read When do you use Java's @Override annotation and why?


Consider the example below

Let, you declare an interface HelloWorld like below.

interface HelloWorld {
    public void greet();
    public void greetSomeone(String someone);
}

And you want to write a class which extends HelloWorld

class TestClass implements HelloWorld {
    String name = "world";

    @Override
    public void greet() {
        greetSomeone("world");
    }

    @Override
    public void greetSomeone(String someone) {
        name = someone;
        System.out.println("Hello " + name);
    }
}

and instantiate it as

TestClass testClass = new TestClass();

Here instead-of above two steps (declaring a class and instantiate it), you can just declare an anonymous inner class, like

HelloWorld testClass = new HelloWorld() {

    String name = "world";
    @Override
    public void greet() {
        greetSomeone("world");
    }
    @Override
    public void greetSomeone(String someone) {
        name = someone;
        System.out.println("Hello " + name);
    }
};

here testClass is an object of an anonymous inner class which implements HelloWorld.

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186