Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6545317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:34:18+00:00 2026-05-25T11:34:18+00:00

This might be simple for seasoned java developers but I just cant seem to

  • 0

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

View v = new View(this) {
    @Override
    protected void onDraw(Canvas canvas) {
        System.out.println("large view on draw called");
        super.onDraw(canvas);
    }
};

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?

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?

I did try to google this and search in SO before asking but couldn’t figure out an answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:34:19+00:00Added an answer on May 25, 2026 at 11:34 am

    protected means (roughly) “available to sub-classes”. (See this table.) Since the new View(this) { ... } creates a subclass, it is possible to override the method within it.

    In this case it doesn’t matter that you’re in a different package. (See the protected line and second column in this table.) The fact that the method is in a subclass is sufficient to “get access” to a protected method.


    Potential follow-up question: What sense does it make, if I can’t call the method anyway?

    All methods in Java are virtual. This means that whenever the View class performs a seemingly internal call to the onDraw method, this call will be dispatched to the overridden method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry this might be a simple question, but I could not figure it out.
This might seem simple but it's just stumbled me and my friends... lets take
This might be a simple question, but i can't seem to grasp it. I
This might be a simple question but I'm just picking this up so please
This might seem ridiculously simple, but I've been getting all kinds of error depending
I feel like this might be really simple but I'm just not getting it
This might be a simple one, but I can't seem to find an easy
This might be something simple i just haven't picked up but what is the
This might seem like a simple question but Id like to know the fastest
All this might look too trivial but read it through - I have simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.