Java Programming - Declarations and Access Control - Discussion
Discussion Forum : Declarations and Access Control - Finding the output (Q.No. 8)
8.
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();
t.count();
}
}
protected class NewTreeSet
{
void count()
{
for (int x = 0; x < 7; x++,x++ )
{
System.out.print(" " + x);
}
}
}
Answer: Option
Explanation:
Nonnested classes cannot be marked protected (or final for that matter), so the compiler will fail at protected class NewTreeSet.
Discussion:
9 comments Page 1 of 1.
R.bhuvana said:
3 years ago
It's right but first, create the parent class after that use the keyword and access the child class to the parent class.
Shashank said:
9 years ago
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.
2. Can not be defined final as NewTreeSet2 extends NewTreeSet.
3. Only inner classes can be defined private.
Moiz F P said:
1 decade ago
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.
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.
Deepak said:
1 decade ago
But why not classes be protected? Why it is designed so?
Paraam said:
1 decade ago
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.
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.
Yuvaraj said:
1 decade ago
Correct Cause of Compilation Failure is Class Can Not Be Declare As protected Only methode Can Be Declare As protected.
Priya said:
1 decade ago
Is it ok if we write child class first and then the parent class?
Asd said:
1 decade ago
By default access specifier is friendly, not public in java.
Abhijit Saha said:
1 decade ago
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.
So non nested class must always public. other wise JVM can't access the class.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers