By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,958 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,958 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
-----------------------------------
Nov 16 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
public sealed overrides function()
{
}

"Ken Varn" <nospam> wrote in message
news:uS**************@tk2msftngp13.phx.gbl...
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
-----------------------------------

Nov 16 '05 #2

P: n/a
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" <vo***********@myfloridahouse.com> wrote in message
news:uy*************@TK2MSFTNGP12.phx.gbl...
public sealed overrides function()
{
}

"Ken Varn" <nospam> wrote in message
news:uS**************@tk2msftngp13.phx.gbl...
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
-----------------------------------


Nov 16 '05 #3

P: n/a

"Ken Varn" wrote...
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

Nov 16 '05 #4

P: n/a
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
-----------------------------------
"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...

"Ken Varn" wrote...
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

Nov 16 '05 #5

P: n/a

"Ken Varn" wrote...
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
Nov 16 '05 #6

P: n/a
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
-----------------------------------
"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:er**************@TK2MSFTNGP10.phx.gbl...

"Ken Varn" wrote...
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

Nov 16 '05 #7

P: n/a
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
-----------------------------------
"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:er**************@TK2MSFTNGP10.phx.gbl...

"Ken Varn" wrote...
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

Nov 16 '05 #8

P: n/a
KJ
Does the "new" keyword help your situation in terms of shadowing?

"Ken Varn" <nospam> wrote in message
news:eq**************@TK2MSFTNGP09.phx.gbl...
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
-----------------------------------
"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:er**************@TK2MSFTNGP10.phx.gbl...

"Ken Varn" wrote...
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


Nov 16 '05 #9

P: n/a
KJ
Does the "new" keyword help your situation in terms of shadowing?

"Ken Varn" <nospam> wrote in message
news:eq**************@TK2MSFTNGP09.phx.gbl...
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
-----------------------------------
"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:er**************@TK2MSFTNGP10.phx.gbl...

"Ken Varn" wrote...
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


Nov 16 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.