How can I override a public virtual method to be private in my derived class?

K

Ken Varn

Is there anyway to override a public virtual method or property so that it
is private in my derived class?

I tried using new on the property and making it private, but no luck.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
 
J

Jeffrey A. Voigt

OOps, that just seals it so that you cannot inherit from it again.... I
don't think that is what your looking for... I'm not sure if you CAN change
the access modifier on it... hmm..

Sorry..
- j

Jeffrey A. Voigt said:
public sealed overrides function()
{
}
 
B

Bjorn Abelli

...
Is there anyway to override a public virtual
method or property so that it
is private in my derived class?

What would be the the point in overriding it
at all if you'll just be using it inside that class?

Why not simply use another name for that method?
I tried using new on the property and
making it private, but no luck.

If you have derived from a class, you'll have to see the operations in the
superclass as a "contract" not to be violated. Any operations you can do on
an instance of the superclass, you must be able to do on instances of the
subclasses (Liskov's principle).

Hence, you cannot make an inherited operation more restricted than in the
superclass.

One point of inheritance is the possibility of polymorphism, where you in
some cases don't know if the instance is of the superclass or a derived
class. It must still be able to respond to the operations of the superclass,
regardless if the methods are overridden or not. It's up to the subclasses
if the methods are overridden (with e.g. "empty" methods), but they still
must be able to respond to the operation.

// Bjorn A
 
K

Ken Varn

The only real reason why I want to override it in the derived class is so
that the method is encapsulated from the implementer with another method
that has a more specific meaning.

Example:

public class ClassA
{
private String _GenericText = "";

public virtual String GenericText
{
get
{
return _GenericText;
}
set
{
_GenericText = value;
}
}
}

public class ClassB : ClassA
{
public ClassB()
{
base.GenericText = "MyText";
}

//
// I want to make GenericText private somehow in here because it has a
wrapper
// with a different name that only returns a read-only constant.
//
// private new GenericText (or something similar)
// get... set...
//
//

public String MyText
{
get
{
return base.GenericText;
}
}
}


I realize this is a crude example, but what I am trying to convey is that I
require the derived class to "hide" the implementation of GenericText
because it contains a wrapper property that is constant and read-only. How
can I "hide" the GenericText property in my derived class?
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
 
B

Bjorn Abelli

...
The only real reason why I want to override
it in the derived class is so that the method
is encapsulated from the implementer with
another method that has a more specific meaning.

If you with a "specific" meaning means to "narrow" the possible operations
from the superclass, you'll still break the Liskov substitutable
principle...
I realize this is a crude example, but what I am
trying to convey is that I require the derived class
to "hide" the implementation of GenericText because it
contains a wrapper property that is constant and read-only.

It doesn't look "constant and read-only" to me. If it isn't in the
superclass, why should it in the subclass?
How can I "hide" the GenericText
property in my derived class?
public class ClassA
{
private String _GenericText = "";

public virtual String GenericText
{
get
{
return _GenericText;
}
set
{
_GenericText = value;
}
}
}


public class ClassB : ClassA
{
public ClassB()
{
base.GenericText = "MyText";
}

public override String GenericText
{
get
{
return base.GenericText;
}
set
{
throw new NotImplementedException(
"This subclass doesn't allow" +
" you to set the generic text...");

// ..or just leave it empty...
}
}
}


....although IMHO I think it would be a better idea to put in *another* field
to use for whatever you're trying to accomplish, as it seems you're trying
to use something defined in the superclass for something it wasn't intended
for...


// Bjorn A
 
K

Ken Varn

I did not know that I could override GenericText with a get method only. I
will try that out. The exception throwing idea is not preferred since this
shifts the assignment checking to run-time rather than compile time.

I would prefer to hide GenericText as private in the derived class, but
maybe this is the wrong approach. I primarily want to avoid including the
base class as a member and wrapping everything just so I can hide one field.
Seems sort of counter productive.


--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
 

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