title: Java overriding methods when creating new instance of a class
body: <p>This might be simple for seasoned java developers but I just cant seem to figure it out. I read a post from here. The code was</p>
  
  <pre><code>View v = new View(this) {
      @Override
      protected void onDraw(Canvas canvas) {
          System.out.println("large view on draw called");
          super.onDraw(canvas);
      }
  };
  </code></pre>
  
  <p>It was an Android question. Here the user creates an instance of a view and overrides a method in a single line. Is there a name for this kind of coding? </p>
  
  <p>My second doubt is, he overrides a protected method from another package. Isn't protected mean package private. I know this will work as I tried it out but I just couldn't figure out why it worked. So why is this code working? </p>
  
  <p>I did try to google this and search in SO before asking but couldn't figure out an answer. </p>