Java Programming - Declarations and Access Control - Discussion


What will be the output of the program?

import java.util.*;
public class NewTreeSet2 extends NewTreeSet 
    public static void main(String [] args) 
        NewTreeSet2 t = new NewTreeSet2();
protected class NewTreeSet
    void count() 
        for (int x = 0; x < 7; x++,x++ ) 
            System.out.print(" " + x);

[A]. 0 2 4
[B]. 0 2 4 6
[C]. Compilation fails at line 2
[D]. Compilation fails at line 10

Answer: Option D


Nonnested classes cannot be marked protected (or final for that matter), so the compiler will fail at protected class NewTreeSet.

Abhijit Saha said: (Sep 2, 2010)  
When the non nested class declared as public or default (public) the JVM can access the class and execute.

So non nested class must always public. other wise JVM can't access the class.

Asd said: (Sep 6, 2011)  
By default access specifier is friendly, not public in java.

Priya said: (Oct 16, 2011)  
Is it ok if we write child class first and then the parent class?

Yuvaraj said: (Feb 19, 2013)  
Correct Cause of Compilation Failure is Class Can Not Be Declare As protected Only methode Can Be Declare As protected.

Paraam said: (Jul 14, 2013)  
Hi All,

In Declarations and Access Control > General Questions.

If you go through Question 14.

Marking a non-nested class with Final Keyword.

Has been explained as correct answer, But here in this question it says that non-nested classes cannot be either protected or Final.

Also, If you look at another Question in the same section.

In Declarations and Access Control > General Questions.

Question 17:

If you read the statement of the Question it goes like this.

Q) Given a method in a protected class, what access modifier do you use to restrict access to that method.

And there is a correct answer to that question when according this question at hand Question 17 should itself be wrong.

There are these certain contradicting concepts in this article.

Which can off course be compiled and verified, but it certainly affects the credibility of this article. Besides, that explanations provided would also require cross check.

Deepak said: (Aug 13, 2014)  
But why not classes be protected? Why it is designed so?

Moiz F P said: (May 14, 2015)  
The explanation given for the answer is correct.

Non nested classes cannot be marked protected (or final for that matter), so the compiler will fail at protected class NewTreeSet.

However, you can declare nested and inner classes as protected or private, though.

Shashank said: (May 10, 2016)  
1. The only permitted modifiers for NewTreeSet class are public and abstract.
2. Can not be defined final as NewTreeSet2 extends NewTreeSet.
3. Only inner classes can be defined private.

R.bhuvana said: (Jun 14, 2022)  
It's right but first, create the parent class after that use the keyword and access the child class to the parent class.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.