Protected Class

M

Mayur H Chauhan

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
 
A

Armin Zingler

Mayur H Chauhan said:
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
 
M

Mayur H Chauhan

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
 
R

rowe_newsgroups

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]
 
A

Armin Zingler

Mayur H Chauhan said:
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
 
M

Mayur H Chauhan

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
 
P

Phill W.

Mayur said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top