What is this technology called Java?

I am a C ++ programmer, and I read this site when I came across the example below. What is this technology called Java? How is this useful?

class Application { ... public void run() { View v = createView(); v.display(); ... protected View createView() { return new View(); } ... } class ApplicationTest extends TestCase { MockView mockView = new MockView(); public void testApplication { Application a = new Application() { <--- protected View createView() { <--- return mockView; <--- whao, what is this? } <--- }; <--- a.run(); mockView.validate(); } private class MockView extends View { boolean isDisplayed = false; public void display() { isDisplayed = true; } public void validate() { assertTrue(isDisplayed); } } } 
+4
source share
2 answers

The general concept used there, Anonymous Classes

In fact, you did to create a new subclass of the Application class by overriding (or implementing) the method in the subclass. Since the subclass is not anonymous (anonymous), you cannot create additional instances of this class.

You can use the same technique to implement the interface or create an instance of an abstract class if you implement all the necessary methods in your definition.

+19
source

As others have noted, code creates layouts for testing purposes. But he also does something called the Anonymous Inner Class.

+4
source

Source: https://habr.com/ru/post/1304216/


All Articles