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

  • SEARCH
  • Home
  • 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 8146323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:57:50+00:00 2026-06-06T13:57:50+00:00

I am trying to make a project with packages of classes with different access

  • 0

I am trying to make a project with packages of classes with different access modifiers to see all the characteristics of classes with different access modifiers. I tried having a package with a public class, one with a default class and then tried to have 2 other for protected and private classes but it said “Illegal modifier for the class privatez; only public, abstract & final are permitted” for them. Why is this so. And secondly, is testing stuff for each class a good way to understand it. I am trying to give a better question than the previous post. I also want to learn application of each access modifier.

  • 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-06-06T13:57:52+00:00Added an answer on June 6, 2026 at 1:57 pm

    I tried having a package with a public class, one with a default class and then tried to have 2 other for protected and private classes but it said “Illegal modifier for the class privatez; only public, abstract & final are permitted” for them. Why is this so.

    The simple answer is that the JLS says you can’t declare a top-level class as private.

    The reason is that it doesn’t make sense to have a class that is only visible to itself. There’s no way that any other class would ever be able to use it (apart, hypothetically, from using dirty tricks with reflection). So the JLS is just saying: “It doesn’t make sense, and I’m not allowing it.”

    (Now a nested class can have access private … but that’s because the private means private to all classes in the outermost enclosing classes …)

    And secondly, is testing stuff for each class a good way to understand it.

    I think it is better to read and try to understand the text book / tutorial. The problem is that if you try to learn by writing your own examples, you can easily draw the wrong conclusions from them.


    For this example:

    package privatez; protected class privatez { }
    

    A hypothetical protected class would/might only be visible to its subclasses. But classes are always visible to their subclasses, so this doesn’t make any sense. (Besides, the way to prevent subclasses is to declare a class final, and you can restrict subclassing using “package private”; i.e. no access modifier.)

    The protected access modifier only makes any sense for stuff inside a class. In a top level class, the normal meaning of protected doesn’t make any sense.

    (This is one of the problem of trying to learn by examples. You end up struggling with understanding why the compiler rejects the examples. Don’t expect the compiler to “explain” why something is wrong. That’s not its role.)

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

Sidebar

Related Questions

I am trying to make a tictactoe project in jQuery and I am having
I'm trying to make my project compile under GCC (Visual Studio compiles it flawlessly).
I am trying to make ajax requests in my project, but I do not
Im trying to make a photoshop like application for my college project in c#
I'm trying to make a request/reply section in my project. I want to achieve
I'm trying to make some friendlyurls in my vb.net (.net 4) project and I'm
I need to make a game in Java for a project. What I'm trying
I'm trying to make a project using struts 2, spring 3 and hibernate, but
I am trying to make an ant script for an EJB project. I am
I know already how to get all the packages available, however I'm trying to

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.