473,566 Members | 3,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Protected Class

All,
For my knowledge, if I declare Class as follow, then it thows
compilation error.

Protected Class Book

End Class

Even same for...

Protected Friend Class Book

End Class

As per my search, class that has to be declared with Protected, should be
declared within other class.

Any suggestion why it is so ?

Mayur H Chauhan
Jun 27 '08 #1
8 2588
"Mayur H Chauhan" <ma***@orioninc .comschrieb
All,
For my knowledge, if I declare Class as follow, then it thows
compilation error.

Protected Class Book

End Class

Even same for...

Protected Friend Class Book

End Class

As per my search, class that has to be declared with Protected,
should be declared within other class.

Any suggestion why it is so ?

It is so because Protected means "only visible inside the containing
class and their derived classes". If you don't put it in another class,
Protected doesn't make sense. Where do you want to have the class
visible?
Armin

Jun 27 '08 #2
Armin,
Thanks for your reply. This is what my requirement is.

I have Class C1. Class define within the same assembly can only create the
instance. So I need to define as "Friend". Also in next release of this
assembly, I would like to allow the extension of Class C1. So I have to
define as "Protected" .

While doing my R & D this is what I have found out.

Scenario 1

Declare B1 class and within that I have declare Protected SC1 and SC2

For SC1

@ Define e1 variable as protected

For SC2

@ Inherit SC1

@ Within the constructor I was able to access e1 of SC1

Declare D1 Class and inherit B1 in that class

@ Within the constructor I was able to create instance of SC1 of B1

@ While accessing E1 variable of SC1 from the instance, I was not
Scenario 2

@ Same as that of snapshot1 except for SC1 define Public variable e2

@ within the D2 Class I was able to access the E2 variable of SC1

As per my understanding,

@ Access specified "Protected" , "Private" and "Protected Friend" can be used
only for the Sub Class. --- This is not clearly given in MS site. They have
just given to fix this error

@ Inner Class define as Protected will allow its Protected member type
accessing by derive class only if Derive class is declared within same Class
where Inner Class is define.

@ If we Inherit a class which has inner class define as protected, then the
derive class can access only access Public member types of the Inner class
and not protected.

Things that I have not understood here is that

1. Why Container class cannot be define as Protected / Private. But only
inner class can have that access specifier


"Armin Zingler" <az*******@free net.dewrote in message
news:Oc******** ******@TK2MSFTN GP03.phx.gbl...
"Mayur H Chauhan" <ma***@orioninc .comschrieb
>All,
For my knowledge, if I declare Class as follow, then it thows
compilation error.

Protected Class Book

End Class

Even same for...

Protected Friend Class Book

End Class

As per my search, class that has to be declared with Protected,
should be declared within other class.

Any suggestion why it is so ?


It is so because Protected means "only visible inside the containing
class and their derived classes". If you don't put it in another class,
Protected doesn't make sense. Where do you want to have the class
visible?
Armin

Jun 27 '08 #3
I have Class C1. Class define within the same assembly can only create the
instance. So I need to define as "Friend".
Yep that's right.
I would like to allow the extension of Class C1. So I have to
define as "Protected" .
This is where you go wrong.

Protected does not mean the class can be inherited, that is done
without adding any extra keywords. The purpose of "Protected" is to
have a class that exists inside another class but is not visible to
anything except the parent class and any class that inherits the
parent class.

Hope that explains things.

Thanks,

Seth Rowe [MVP]
Jun 27 '08 #4
"Mayur H Chauhan" <ma***@orioninc .comschrieb im Newsbeitrag
news:Ot******** ******@TK2MSFTN GP06.phx.gbl...
Armin,
Thanks for your reply. This is what my requirement is.

I have Class C1. Class define within the same assembly can only create
the
instance. So I need to define as "Friend". Also in next release of
this
assembly, I would like to allow the extension of Class C1. So I have
to
define as "Protected" .

While doing my R & D this is what I have found out.

Scenario 1

Declare B1 class and within that I have declare Protected SC1 and
SC2

For SC1

@ Define e1 variable as protected

For SC2

@ Inherit SC1

@ Within the constructor I was able to access e1 of SC1

Declare D1 Class and inherit B1 in that class

@ Within the constructor I was able to create instance of SC1 of B1

@ While accessing E1 variable of SC1 from the instance, I was not
Scenario 2

@ Same as that of snapshot1 except for SC1 define Public variable
e2

@ within the D2 Class I was able to access the E2 variable of SC1

As per my understanding,

@ Access specified "Protected" , "Private" and "Protected Friend" can
be used
only for the Sub Class. --- This is not clearly given in MS site. They
have
just given to fix this error

@ Inner Class define as Protected will allow its Protected member type
accessing by derive class only if Derive class is declared within same
Class
where Inner Class is define.

@ If we Inherit a class which has inner class define as protected,
then the
derive class can access only access Public member types of the Inner
class
and not protected.

I'm not sure if the following answers it all:

A Class can have different kind of members:

- Fields (=variables)
- Methods
- Properties
- Classes (= nested classes)
- ...

The modifiers "Protected" , "Private" and "Protected Friend" determine
where the member is *visible*/accessible:
- protected: visible in the container class and in their derived classes
- friend: visible everywhere in the same assembly
- protected friend: combination of both before (note: the scope is a set
union, not an intersection of both scopes)

As you see, the modifier has nothing to do with where an instance of the
class can be created. Availability of instance creation is done by using
the appropriate modifier at the class' constructor: public sub new,
protected sub new, friend sub new, ....
Of course, the widest scope that allows the object creation is still
limited by the class' visibility scope:

class A1
protected class Nested
public sub New
end sub
end class
end A1

Even though the nested class' constructor is public, the class is still
only visible in A1 and in classes derived from A1. So, it's equal to
"Protected sub new" in this case.

Things that I have not understood here is that

1. Why Container class cannot be define as Protected / Private. But
only
inner class can have that access specifier
Class C1
protected Class C2
end clas
end class

protected class C3 'ERROR
end class

"Protected Class C2" works because it says: C2 is visible in Class C1
and in all classes derived from C1.

Now use exactly the same sentence replacing C2 with C3:

"Protected Class C3" works because it says: C3 is visible in Class ???
and in all classes derived from ???.

The ??? can not be answered because there is no outer class. Therefore,
protected only makes sense with nested classes - and with all other
class members.

Armin

Jun 27 '08 #5
If that is the case, in the Sample 2 which I have decribe below, I have D2
which is derived from C1. C1 has Protected class SC1. Now when I inherit C1
in D1, then as per your answer, I should be able to access Protected members
of the SC1 from D1 class. But what I am able to see is that I can only
access Public variable and not the protected variable.

Thanks for your reply.

Mayur
"rowe_newsgroup s" <ro********@yah oo.comwrote in message
news:b3******** *************** ***********@z66 g2000hsc.google groups.com...
>I have Class C1. Class define within the same assembly can only create
the
instance. So I need to define as "Friend".

Yep that's right.
>I would like to allow the extension of Class C1. So I have to
define as "Protected" .

This is where you go wrong.

Protected does not mean the class can be inherited, that is done
without adding any extra keywords. The purpose of "Protected" is to
have a class that exists inside another class but is not visible to
anything except the parent class and any class that inherits the
parent class.

Hope that explains things.

Thanks,

Seth Rowe [MVP]

Jun 27 '08 #6
Thanks a ton Armin. and Seth. You have helped me to clear my doubts.

"Armin Zingler" <az*******@free net.dewrote in message
news:OG******** *****@TK2MSFTNG P05.phx.gbl...
"Mayur H Chauhan" <ma***@orioninc .comschrieb im Newsbeitrag
news:Ot******** ******@TK2MSFTN GP06.phx.gbl...
>Armin,
Thanks for your reply. This is what my requirement is.

I have Class C1. Class define within the same assembly can only create
the
instance. So I need to define as "Friend". Also in next release of
this
assembly, I would like to allow the extension of Class C1. So I have
to
define as "Protected" .

While doing my R & D this is what I have found out.

Scenario 1

Declare B1 class and within that I have declare Protected SC1 and
SC2

For SC1

@ Define e1 variable as protected

For SC2

@ Inherit SC1

@ Within the constructor I was able to access e1 of SC1

Declare D1 Class and inherit B1 in that class

@ Within the constructor I was able to create instance of SC1 of B1

@ While accessing E1 variable of SC1 from the instance, I was not
Scenario 2

@ Same as that of snapshot1 except for SC1 define Public variable
e2

@ within the D2 Class I was able to access the E2 variable of SC1

As per my understanding,

@ Access specified "Protected" , "Private" and "Protected Friend" can
be used
only for the Sub Class. --- This is not clearly given in MS site. They
have
just given to fix this error

@ Inner Class define as Protected will allow its Protected member type
accessing by derive class only if Derive class is declared within same
Class
where Inner Class is define.

@ If we Inherit a class which has inner class define as protected,
then the
derive class can access only access Public member types of the Inner
class
and not protected.


I'm not sure if the following answers it all:

A Class can have different kind of members:

- Fields (=variables)
- Methods
- Properties
- Classes (= nested classes)
- ...

The modifiers "Protected" , "Private" and "Protected Friend" determine
where the member is *visible*/accessible:
- protected: visible in the container class and in their derived classes
- friend: visible everywhere in the same assembly
- protected friend: combination of both before (note: the scope is a set
union, not an intersection of both scopes)

As you see, the modifier has nothing to do with where an instance of the
class can be created. Availability of instance creation is done by using
the appropriate modifier at the class' constructor: public sub new,
protected sub new, friend sub new, ....
Of course, the widest scope that allows the object creation is still
limited by the class' visibility scope:

class A1
protected class Nested
public sub New
end sub
end class
end A1

Even though the nested class' constructor is public, the class is still
only visible in A1 and in classes derived from A1. So, it's equal to
"Protected sub new" in this case.

>Things that I have not understood here is that

1. Why Container class cannot be define as Protected / Private. But
only
inner class can have that access specifier

Class C1
protected Class C2
end clas
end class

protected class C3 'ERROR
end class

"Protected Class C2" works because it says: C2 is visible in Class C1
and in all classes derived from C1.

Now use exactly the same sentence replacing C2 with C3:

"Protected Class C3" works because it says: C3 is visible in Class ???
and in all classes derived from ???.

The ??? can not be answered because there is no outer class. Therefore,
protected only makes sense with nested classes - and with all other class
members.

Armin

Jun 27 '08 #7
Mayur H Chauhan wrote:
I have Class C1. Class define within the same assembly can only create the
instance. So I need to define as "Friend".
Not necessarily.

"class within the same assembly can create the instance"

The class /can/ be visible outside the Assembly but only creatable from
/within/ it.

Public Class C1
Friend Sub New()
Also in next release of this assembly, I would like to allow the extension
of Class C1. So I have to define as "Protected" .
No you don't. The ability to inherit from any class is present /by
default/ - you explicitly have to switch it /off/ by using the
"NotInheritable " keyword (other languages use the more succinct, "sealed").

Public NotInheritable Class C1_1
Friend Sub New()

HTH,
Phill W.
Jun 27 '08 #8
Thanks Phill, that was an additional help.

Mayur Chauhan
"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
news:g1******** **@south.jnrs.j a.net...
Mayur H Chauhan wrote:
>I have Class C1. Class define within the same assembly can only create
the
instance. So I need to define as "Friend".

Not necessarily.

"class within the same assembly can create the instance"

The class /can/ be visible outside the Assembly but only creatable from
/within/ it.

Public Class C1
Friend Sub New()
>Also in next release of this assembly, I would like to allow the
extension
of Class C1. So I have to define as "Protected" .

No you don't. The ability to inherit from any class is present /by
default/ - you explicitly have to switch it /off/ by using the
"NotInheritable " keyword (other languages use the more succinct,
"sealed").

Public NotInheritable Class C1_1
Friend Sub New()

HTH,
Phill W.

Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2737
by: Grey Plastic | last post by:
I have several classes that all keep track of static data. However, the manner that they keep track of static data is identical, and so I'm using the template<class Child> class Parent { ... }; idiom (don't know the name of it, if there is one). The problem is that I don't want any of my classes to have public constructors. They should be...
2
2286
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point in Rectangle, the compiler tells me Point::x is protected. I would have expected Rectangle to see the protected members of any Point. Compiling the...
13
7692
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. They don't actually protect any encapsulation or anything, and for all the actual protection they offer, they might as well be public. For...
8
2527
by: Carlos J. Quintero | last post by:
Hi, As you know the current keywords "protected internal" (C#) or "Protected Friend" (VB.Net) means "Protected Or internal" (C#) or "Protected Or Friend" (VB.Net), that is, the member is accesible from whichever types inside the assembly or from derived classes included those outside the assembly. The IL has a provision for the "family...
11
3809
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of...
14
2453
by: mlimber | last post by:
In an article on the safe bool idiom (http://www.artima.com/cppsource/safeboolP.html), Bjorn Karlsson gives the following code (slightly modified): class safe_bool_base { protected: typedef void (safe_bool_base::*bool_type)() const; void this_type_does_not_support_comparisons() const {} safe_bool_base() {}
6
4129
by: Rick | last post by:
Hi, Can anyone explain to me why the below fails to compile - seeing otherA->f(); as a call to a inaccessible function, while otherB->f(); is ok? It seems you can happily access protected functions of another (same type) - but not via a base class pointer.... I've checked the FAQs, Meyers etc but nothing obvious I can find explains it.
16
3611
by: Fir5tSight | last post by:
Hi All, I have a small C#.NET program that is as follows: using System; class A { protected int x = 123; }
13
2814
by: Clive Dixon | last post by:
I am refactoring some code to move some base classes into a separate assembly. One of these base classes has a member property which is 'protected internal'. However when I move these base classes to another assembly, the compiler complains that the override in the derived class, also declared as 'protected internal', is trying to change the...
0
7683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8113
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7649
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7957
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5487
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3628
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1204
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.