Access specifier and inheritance

M

mshetty

Hi,

I have a class x with two public methods xmethod1 and xmethod2. I want
to derive a class y such that it can access only xmethod2. I have done
the following and it compiles please let me know if it is valid to do
so.

class y : public x
{
private:
void xmethod1()
{
// some dummy implementation
// trying to override xmethod1 with a private access
// specifier so that whenever xmethod1 is invoked using an
// object of y an error is thrown by the compiler
}
}

Is this valid? I want the compiler to throw an error.
Is there a better way of doing this?

Thanks and Regards,
M Shetty

PS: This still allows:
y *y1 = new y;
x *x2 = y1;
x2->xmethod1();
// calls derived private method y::xmethod1
 
C

Claudio Puviani

mshetty said:
Hi,

I have a class x with two public methods xmethod1 and xmethod2. I want
to derive a class y such that it can access only xmethod2. I have done
the following and it compiles please let me know if it is valid to do
so.

class y : public x
{
private:
void xmethod1()
{
// some dummy implementation
// trying to override xmethod1 with a private access
// specifier so that whenever xmethod1 is invoked using an
// object of y an error is thrown by the compiler
}
}

Is this valid? I want the compiler to throw an error.
Is there a better way of doing this?

Thanks and Regards,
M Shetty

PS: This still allows:
y *y1 = new y;
x *x2 = y1;
x2->xmethod1();
// calls derived private method y::xmethod1

What you're trying to do violates one of the most basic rules of object-oriented
programming -- the Liskov Substitutability Principle. This says that instances
(or references or pointers) of derived classes MUST be substitutable for
instances (or references or pointers) of the base class. A corrolary to this is
that you can make a derived class do more than its base, but you can't make it
do less. This is true for OOP in any language, not only C++.

In other words, if baseInst.xyz() is legal but derivedInst.xyz() is illegal,
there's a fundamental flaw in the class hierarchy that should be fixed, not
circumvented.

Claudio Puviani
 
V

Victor Bazarov

mshetty said:
I have a class x with two public methods xmethod1 and xmethod2.

Are those member functions declared 'virtual' or not?
I want
to derive a class y such that it can access only xmethod2.

In order to grant access only to member function 'xmethod2' in 'x', you
should declare it either public or protected. If it's public, everybody
will have access to it. If it's protected, only derived classes and
friends will have access. If it's private, non-friends cannot access it.
I have done
the following and it compiles please let me know if it is valid to do
so.

The posted code does not contain enough information to judge that or you
failed to correctly state the requirements you impose on the classes.
class y : public x
{
private:
void xmethod1()
{
// some dummy implementation

No need for any implementation, even for a body. If nobody is allowed
to access it, any attempt to use 'xmethod1' should be an error, even
a "legal" one, from the same class. Just don't provide the body at all.
// trying to override xmethod1 with a private access
// specifier so that whenever xmethod1 is invoked using an
// object of y an error is thrown by the compiler
}
}

Is this valid? I want the compiler to throw an error.
Is there a better way of doing this?

Thanks and Regards,
M Shetty

PS: This still allows:
y *y1 = new y;
x *x2 = y1;
x2->xmethod1();
// calls derived private method y::xmethod1

No, it most likely calls the base class member function. Without
seeing how you defined 'x', it is impossible to tell for sure.

Victor
 
J

Jeff Schwab

mshetty said:
Hi,

I have a class x with two public methods xmethod1 and xmethod2. I want
to derive a class y such that it can access only xmethod2. I have done
the following and it compiles please let me know if it is valid to do
so.

class y : public x
{
private:
void xmethod1()
{
// some dummy implementation
// trying to override xmethod1 with a private access
// specifier so that whenever xmethod1 is invoked using an
// object of y an error is thrown by the compiler
}
}

Is this valid? I want the compiler to throw an error.
Is there a better way of doing this?

Nope. You can't make publically inherited methods "more private." Use
protected inheritance instead, and put a "using x::xmethod2" declaration
in the "public" section of y's definition. This technique is useful
when x is being made a base of y only to ease y's implementation.
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top