noob oop question

S

steve

i'd like to inherit from a class while changing the access-level of an
interface within the derived class. the below won't work...how do i properly
do this...i don't want to expose the "add" method of the "InheritedItems"
class to a caller even though i inherit from the "Items" class which has a
different access-level. i understand why this is a problem but don't know
how to accomplish what i've explained above as what i want to do. i assume
the answer is probably through the interface definition of "IItems" rather
than the class-level being inherited.

help please.

tia,

steve


----------

interface IItems

sub Add(byval Index as integer)

end interface

----------

class Items

implements IItems

public overridable sub Add(byval Index as integer) implements IItems.Add
' some code here
end sub

end class

----------

class InheritedItems

inherits Items

protected overrides sub Add(byval Index as integer)
' some code here
end sub

end class
 
A

Armin Zingler

steve said:
i'd like to inherit from a class while changing the access-level of
an interface within the derived class. the below won't work...how do
i properly do this...i don't want to expose the "add" method of the
"InheritedItems" class to a caller even though i inherit from the
"Items" class which has a different access-level. i understand why
this is a problem but don't know how to accomplish what i've
explained above as what i want to do. i assume the answer is
probably through the interface definition of "IItems" rather than
the class-level being inherited.


You can't. The class implements the Interface, consequently the derived
class also does. The access-Level is unimportant because every method
implementing the interface method is public. That's the contract of the
interface.

Even if you declare the Method private (but not overridable which would
cause a conflict (and wouldn't make sense))


Private Sub AddAnything(ByVal Index As Integer) Implements IItems.Add
' some code here
End Sub

The sub will also be there if the object is accessed via it's IITems
interface. As you see I also changed the procedure name which shows that
only "Implements IItems.Add" binds the method.

Armin
 
A

Armin Zingler

Armin Zingler said:
You can't. The class implements the Interface, consequently the
derived class also does. The access-Level is unimportant because
every method implementing the interface method is public. That's the
contract of the interface.

I have to add: You can choose the access level as you want - withing the
specifications, i.e. you can not change it in the inherited class - but this
has nothing to do with the interface.

Armin
 
S

steve

thanks armin...i thought so. i just didn't know if there was another way to
accomplish what i was trying to do. that makes sense...i've gone a different
route to get it done.

thanks again.


| > > i'd like to inherit from a class while changing the access-level
| > > of an interface within the derived class. the below won't
| > > work...how do i properly do this...i don't want to expose the
| > > "add" method of the "InheritedItems" class to a caller even though
| > > i inherit from the "Items" class which has a different
| > > access-level. i understand why this is a problem but don't know
| > > how to accomplish what i've
| > > explained above as what i want to do. i assume the answer is
| > > probably through the interface definition of "IItems" rather than
| > > the class-level being inherited.
| >
| >
| > You can't. The class implements the Interface, consequently the
| > derived class also does. The access-Level is unimportant because
| > every method implementing the interface method is public. That's the
| > contract of the interface.
|
| I have to add: You can choose the access level as you want - withing the
| specifications, i.e. you can not change it in the inherited class - but
this
| has nothing to do with the interface.
|
| Armin
|
 

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