473,378 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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
9 2038
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
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

"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
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

"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
8
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
5
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two...
11
by: z_learning_tester | last post by:
Hello, yes another beginner question that I'm sure is obvious to many here :-) My book is so bad. Really. It uses the exact same example of code for using the new kw and for using virtual(in the...
15
by: John Salerno | last post by:
Hi all. I have a question about virtual and override methods. Please forgive the elementary nature! First off, let me quote Programming in the Key of C#: "Any virtual method overridden with...
1
by: relient | last post by:
I'm learning about the virtual table in association with virtual methods. I got most of the logic and understanding down (I believe) for when you use 'override' and no 'override' or no 'new'...
5
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This...
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
7
by: Nick Keighley | last post by:
I take it this is wrong:- class Direct_draw { public: Direct_draw (); virtual ~Direct_draw () {}
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.