473,385 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Private Class Declaration

Can any one here who tell me that why we can not Declare a class or Interface as Private or Protected.

I have just compiled a class as

private class abc{
}

then the compiler throw an error that private is not allowed here

So can any one plz explain me the reason behind that?
Jun 28 '07 #1
19 3743
DeMan
1,806 1GB
Java is not my strongest language, however I would suggest that it doesn't make sense for a class to be private or protected, because it means that (almost) noone can see it.......
Jun 28 '07 #2
r035198x
13,262 8TB
Can any one here who tell me that why we can not Declare a class or Interface as Private or Protected.

I have just compiled a class as

private class abc{
}

then the compiler throw an error that private is not allowed here

So can any one plz explain me the reason behind that?
A class can be decared as protected, in which case it will only be available in the package in which it is declared.
A private class does not make sense and is not allowed.
You can read this article for a descrption of some of this stuff.
Jun 28 '07 #3
Java is not my strongest language, however I would suggest that it doesn't make sense for a class to be private or protected, because it means that (almost) noone can see it.......

Thanks for your Reply,

Actually I am also thinking the same from my own, but i have Posted the Question just bcz to know that my thinking is right or wrong.

But if any one wants to explain something more then, Most Welcome,

Thanks Again
Jun 28 '07 #4
DeMan
1,806 1GB
I think r0 has probably provided a better explanation and a very useful link......
Jun 28 '07 #5
r035198x
13,262 8TB
I think r0 has probably provided a better explanation and a very useful link......
You call me r0 one more time and you can consider yourself banned.
Jun 28 '07 #6
Hi,

first thing It is not right that you can not create a private or protected class.....you can create but this class should not be Top level class in the file means you can create private/protected innerclasses.

second thing what is the sense to create a Top level class as private/protected because anyway it is not going to be accessed by outside programs.

regards,

Kishore M
Jun 28 '07 #7
r035198x
13,262 8TB
Hi,

first thing It is not right that you can not create a private or protected class.....you can create but this class should not be Top level class in the file means you can create private/protected innerclasses.

second thing what is the sense to create a Top level class as private/protected because anyway it is not going to be accessed by outside programs.

regards,

Kishore M
OK.
A protected class can be accessed by other programs within the same package that that class has been defined (so it can be defined top level).
A private top level class is not allowed because that's not going to be of any use but you can declare a private class as a member of another class.
Jun 28 '07 #8
DeMan
1,806 1GB
You call me r0 one more time and you can consider yourself banned.
sorry, like a phone number I forgot the digits.......
Jun 28 '07 #9
sorry, like a phone number I forgot the digits.......


Sorry sorry but i found a Quote in the link given here
http://www.thescripts.com/forum/thread641524.html

and it will explaining that

Class structure

A class declaration starts with a modifier which can be any of
public, protected, private, abstract, static, strictfp, final.
To get us started we'll look at the public and protected modifiers first.
Java classes are encapsulated into packages. Classes in one package can only access public classes in another package. Protected classes are accessible only within the package that they have been defined. If you do not include a modifier, protected access is assumed. The class is put inside the default package which is automatically created for you.
You should always put your classes in a package. Here is the rest of the structure of a Java class:

Modifier(s) class ClassName {
members(methods, fields, nested classes and interfaces)
constructors
static initializers

}



Now what you all says about this article?
Jun 28 '07 #10
OK.
A protected class can be accessed by other programs within the same package that that class has been defined (so it can be defined top level).
A private top level class is not allowed because that's not going to be of any use but you can declare a private class as a member of another class.

Sorry r0 i want to disturb you here but we can not use protected as modifier for top level class and it will give compile time error.
Jun 28 '07 #11
r035198x
13,262 8TB
Sorry sorry but i found a Quote in the link given here
http://www.thescripts.com/forum/thread641524.html

and it will explaining that

Class structure

A class declaration starts with a modifier which can be any of
public, protected, private, abstract, static, strictfp, final.
To get us started we'll look at the public and protected modifiers first.
Java classes are encapsulated into packages. Classes in one package can only access public classes in another package. Protected classes are accessible only within the package that they have been defined. If you do not include a modifier, protected access is assumed. The class is put inside the default package which is automatically created for you.
You should always put your classes in a package. Here is the rest of the structure of a Java class:

Modifier(s) class ClassName {
members(methods, fields, nested classes and interfaces)
constructors
static initializers

}



Now what you all says about this article?
I hope you're not starting to get confused.
A private class cannot be top level. It can only be declared inside another class. That's why private was listed there as one of the possible class modifiers.
Jun 28 '07 #12
I hope you're not starting to get confused.
A private class cannot be top level. It can only be declared inside another class. That's why private was listed there as one of the possible class modifiers.

thanks to all of you for your kind discussion here.
I really got very useful knowledge from here.
Jun 28 '07 #13
r035198x
13,262 8TB
Sorry r0 i want to disturb you here but we can not use protected as modifier for top level class and it will give compile time error.
Yack, I've really messed things up now.

protected won't work for top level class alright.
If you leave a class without a modifier (and define no package), it is placed in the default package and is accessible only inside that default package (sort of protected access for that class).
But you're all right, protected specifier won't work for top level and I've been telling it wrong there.

Edit: You can call me r0 now after that mistake.
Jun 28 '07 #14
DeMan
1,806 1GB
.....(does that include me)
Jun 28 '07 #15
praveen2gupta
201 100+
class is having only Two access Modifiers

1. default (neet not to write)
2. Public

any other access modifiers are not allowed with classes.
Jun 28 '07 #16
r035198x
13,262 8TB
.....(does that include me)
You do that, you'd be banned before you even hit the Submit Reply button.
Jun 28 '07 #17
r035198x
13,262 8TB
class is having only Two access Modifiers

1. default (neet not to write)
2. Public

any other access modifiers are not allowed with classes.
Here we go again, unless it's inside another class

Expand|Select|Wrap|Line Numbers
  1. package x;
  2. class a {
  3.     private class b {
  4.     }
  5.     protected class c {
  6.     }
  7. }
Jun 28 '07 #18
Here we go again, unless it's inside another class

Expand|Select|Wrap|Line Numbers
  1. package x;
  2. class a {
  3.     private class b {
  4.     }
  5.     protected class c {
  6.     }
  7. }

This time You are all Right Dear r0
Jun 28 '07 #19
JosAH
11,448 Expert 8TB
Just to sum it all up; there are top level classes and nested classes. Top level
classes have either package scope or they are public.

Nested classes can be static or not which is not relevant for this discussion.
Nested classes can either be private, protected, package scope or public.
Anonymous classes (e.g. those small Listener or Observer classes) have to be
made visible by the surrounding class otherwise they don't exist according to the
outside world, whatever that may be.

It doesn't make sense for top level classes to be private because nothing else
can reach them. The same goes for protected top level classes, i.e. only the
class itself can reach it or a subclass thereof. But something else has to start
the whole thing going but it isn't allowed to even point its finger at it, nor at a
subclass thereof.

kind regards,

Jos
Jun 28 '07 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Corno | last post by:
Hi all, I was just wondering why a class declaration also includes the private members of a class. Is this information needed by other classes for compiling and/or linking or is this just an...
7
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a...
6
by: Chris Mantoulidis | last post by:
Forgive me if I'm wrong but I think there is something like an extra member scope in classes. for example: class abc { ostream & operator << (ostream &, const abc &); istream & operator >>...
19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
5
by: Marcin Kalicinski | last post by:
Hi, Because public inheritance should be used to model is-a relationship, is it evil from a design point of view to disallow some operations from base class? class Derived: public Base { /*...
4
by: Tom | last post by:
Let's say I've got a class defined in a header file. class foo { private: ... int bar; .... }; Now lets say I have a function that needs to access the private variable
6
by: Jonathan Potter | last post by:
Hi, I was wondering if someone could explain something to me. We recently upgraded to a new C++ compiler and found some old code wouldn't compile. Boiled down, the code that causes the error is:...
4
by: Jason Shohet | last post by:
A label on an ascx control has a corresponding declaration in the c# code-behind page. I was curious what would happen if I made that declaration 'PRIVATE' instead of 'PROTECTED'. The only things...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.