473,762 Members | 8,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Close access to the base class public methods

Hi!
Could you imagine any way to block access to the base class public
methods?
Here is an example:
class B:
def public_method() :
pass

class A(B):
def public_a_method ():
pass

def a = A()

Is there any way to block the call a.public_method () without any
changes to B class?
Thank you!
Nov 11 '08 #1
4 1965
RinKaMeAri wrote:
Hi!
Could you imagine any way to block access to the base class public
methods?
Here is an example:
class B:
def public_method() :
pass

class A(B):
def public_a_method ():
pass

def a = A()

Is there any way to block the call a.public_method () without any
changes to B class?
Thank you!
The simplest way would be to override B.public_method within A by
defining A.public_method to raise a NotImplementedE rror or similar
exception. Though of course this then begs the question of why A would
need to subclass B in the first place, but I assume there would be
methods you *did* want to implement.

Perhaps more background would yield a better solution.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Nov 11 '08 #2
On Nov 11, 9:12*pm, Steve Holden <st...@holdenwe b.comwrote:
RinKaMeAri wrote:
Hi!
Could you imagine any way to block access to the base class public
methods?
Here is an example:
class B:
* * def public_method() :
* * * * pass
class A(B):
* * def public_a_method ():
* * * * *pass
def a = A()
Is there any way to block the call a.public_method () without any
changes to B class?
Thank you!

The simplest way would be to override B.public_method within A by
defining A.public_method to raise a NotImplementedE rror or similar
exception. Though of course this then begs the question of why A would
need to subclass B in the first place, but I assume there would be
methods you *did* want to implement.

Perhaps more background would yield a better solution.

regards
*Steve
--
Steve Holden * * * *+1 571 484 6266 * +1 800 494 3119

Thanks, Steve! will do what you have proposed.

It is just the stub example. Have been writing some tests for class
hierarchy and found the case when user can call the base method with
the derived instance and have problems :-) IMO, it is the class
design bug. Tried to play with __all__, setattr, getattr, but didn't
find any solution.

BTW, what do you mean "to subclass B in the *first place*"?
Nov 11 '08 #3
RinKaMeAri wrote:
On Nov 11, 9:12 pm, Steve Holden <st...@holdenwe b.comwrote:
>RinKaMeAri wrote:
>>Hi!
Could you imagine any way to block access to the base class public
methods?
Here is an example:
class B:
def public_method() :
pass
class A(B):
def public_a_method ():
pass
def a = A()
Is there any way to block the call a.public_method () without any
changes to B class?
Thank you!
The simplest way would be to override B.public_method within A by
defining A.public_method to raise a NotImplementedE rror or similar
exception. Though of course this then begs the question of why A would
need to subclass B in the first place, but I assume there would be
methods you *did* want to implement.

Perhaps more background would yield a better solution.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119


Thanks, Steve! will do what you have proposed.

It is just the stub example. Have been writing some tests for class
hierarchy and found the case when user can call the base method with
the derived instance and have problems :-) IMO, it is the class
design bug. Tried to play with __all__, setattr, getattr, but didn't
find any solution.

BTW, what do you mean "to subclass B in the *first place*"?
It's just an expression. As in "I built A as a subclass of B, but since
I overrode all B's methods in A there was no point making it a subclass
in the first place".

regards
Steve

--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Nov 11 '08 #4
On Tue, Nov 11, 2008 at 11:16 AM, RinKaMeAri <ma****@gmail.c omwrote:
On Nov 11, 9:12 pm, Steve Holden <st...@holdenwe b.comwrote:
>RinKaMeAri wrote:
Hi!
Could you imagine any way to block access to the base class public
methods?
Here is an example:
class B:
def public_method() :
pass
class A(B):
def public_a_method ():
pass
def a = A()
Is there any way to block the call a.public_method () without any
changes to B class?
Thank you!

The simplest way would be to override B.public_method within A by
defining A.public_method to raise a NotImplementedE rror or similar
exception. Though of course this then begs the question of why A would
need to subclass B in the first place, but I assume there would be
methods you *did* want to implement.
<snip>
>
BTW, what do you mean "to subclass B in the *first place*"?
Because you're inheriting from A and yet you don't want to inherit a
certain part of A, in this case public_method() , it's usually a sign
something is wrong with your class hierarchy; otherwise, you could
just inherit from something else which would have just the part of A
you want to inherit; it's a so-called "code smell", specifically
Refused Bequest I believe.

See this link into Fowler's Refactoring for more info on Refused
Bequest and the other code smells:
http://books.google.com/books?id=1Ms...um=3&ct=result

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
Nov 11 '08 #5

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

Similar topics

11
4614
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
9
4996
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class pointer which points to an instance of a derived class, but when I pass that base class pointer into a function, it can't access the derived object's public functions. Although, the base class pointer does call the appropriate virtual function...
11
2168
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain methods, such as public void Add(MyClass c). How can I enforce the same behavior (of requiring to implement a member with a new return type in an inherited class) in the master class (similar to the CollectionBase)? I have a class called...
7
3015
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the "Close()" methods reared their ugly (at least it would seem...)heads. I was happily delving away in the .NET framework, investigating the stream classes with the msdn and Lutz Roeder's .NET reflector, when I stumbled upon the following:
8
4678
by: Stanislav Simicek | last post by:
Hello, I've several C++ classes that must be ported to C#. Is there any construction in C# similar to C++: class A : protected B { ... }
10
25743
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base classes.( I already have the ReflectionPermission ) Is there a way to get this information ? Thnaks in advance
6
1503
by: wingphil | last post by:
Hi there, I have a shared method in a base class, and I need to know which subclass it has been called from. So for example Public Mustinherit Class BaseClass Public Shared Sub SharedMethod ... End Sub
6
9159
by: =?Utf-8?B?SmF5IFBvbmR5?= | last post by:
I am trying to access a Public property on a Master Page from a Base Page. On the content pages I have the MasterType Directive set up as follows: <%@ MasterType virtualpath="~/Master.master" %> On the Master Page I have a public property exposed: Public Property ErrorMessage() As String Get Return txtError.InnerText End Get
2
1781
by: fgh.vbn.rty | last post by:
Hi, I'm not sure if i'm asking the question correctly but anyway here it is. Say I have 3 classes - class A, class B, class R. 1) A and B are the building blocks and R is like a repository that stores objects of A and B. 2) A is at the lowest level and should "know about" only other As. B should know only about As and other Bs.
0
9378
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9812
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3914
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
3
3510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.