Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

private and protected modifier in top level class

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q) see the following code
public class A
{
public stratic void main(String str[])
{
}
}
class B extends A // (1)
{
// some code
}
if at (1) I put private the compiler says that package members should
be accessible to the package members
and if I put protected then it says that class or interface declaration
expected
but I think that they should be allowed as they are applied to class which does not contain main method what is the problem with that
anyone ps explain me that ..
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A top level class may not be protected or private. An inner class can be though.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai,
The class which you are declaring as private is package level class, that can declared as public or default
 
Athiveera Kugan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai,
The class which you are declaring as private is package level class, that can declared as only public or default .
 
Athiveera Kugan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai,
The class which you are declaring as private is package level class, that can be declared as only public or default .
<marquee>by s.a.kugan</marquee>
 
Athiveera Kugan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai,
The class which you are declaring as private is package level class, that can be declared as only public or default .
<marquee>by s.a.kugan</marquee>
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
It is not related with main method. main() is just a method compiler only complain when you want to run the class and tells there isnot any main() method described.
A top level class can not have private or protocted modifiers! This is the rule.
class A ....
{
public static void main...
}
class B extends A {
}
main method is inherited to class B you can run class B too.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello nishesh,
u r a ranch hand. this means u have cleared SCJP.but u put up very basic questions on the board.
i can't digest this.would u mind if i ask ur score on the test.
kriti
 
What's that smell? Hey, sniff this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic