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
----------------------------------- 9 2062
public sealed overrides function()
{
}
"Ken Varn" <nospam> wrote in message
news:uS******** ******@tk2msftn gp13.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 -----------------------------------
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******** *****@TK2MSFTNG P12.phx.gbl... public sealed overrides function() { }
"Ken Varn" <nospam> wrote in message news:uS******** ******@tk2msftn gp13.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 -----------------------------------
"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
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.GenericTex t = "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.GenericTex t;
}
}
}
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**********@D oNotSpam.hotmai l.com> wrote in message
news:uT******** ******@TK2MSFTN GP09.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
"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.GenericTex t = "MyText";
}
public override String GenericText
{
get
{
return base.GenericTex t;
}
set
{
throw new NotImplementedE xception(
"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
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**********@D oNotSpam.hotmai l.com> wrote in message
news:er******** ******@TK2MSFTN GP10.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.GenericTex t = "MyText"; }
public override String GenericText { get { return base.GenericTex t; } set { throw new NotImplementedE xception( "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
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**********@D oNotSpam.hotmai l.com> wrote in message
news:er******** ******@TK2MSFTN GP10.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.GenericTex t = "MyText"; }
public override String GenericText { get { return base.GenericTex t; } set { throw new NotImplementedE xception( "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
Does the "new" keyword help your situation in terms of shadowing?
"Ken Varn" <nospam> wrote in message
news:eq******** ******@TK2MSFTN GP09.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**********@D oNotSpam.hotmai l.com> wrote in message news:er******** ******@TK2MSFTN GP10.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.GenericTex t = "MyText"; }
public override String GenericText { get { return base.GenericTex t; } set { throw new NotImplementedE xception( "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
Does the "new" keyword help your situation in terms of shadowing?
"Ken Varn" <nospam> wrote in message
news:eq******** ******@TK2MSFTN GP09.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**********@D oNotSpam.hotmai l.com> wrote in message news:er******** ******@TK2MSFTN GP10.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.GenericTex t = "MyText"; }
public override String GenericText { get { return base.GenericTex t; } set { throw new NotImplementedE xception( "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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming years
namespace MyNamespac
public delegate void MyDel()
public class MyBase
public virtual...
|
by: JPRoot |
last post by:
Hi M. Jeffrey Tan,
Just hopping you didn't forget me? :)
Thanks
JPRoot
----- \"Jeffrey Tan\" wrote: -----
|
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 classes yClass which inherits xClass.
xClass has a virtual method which simply writes a line of text stating its
origin, yClass implements the same method which writes a line of text
stating its origin also (i.e. "From yClass").
I ran the...
|
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 base class) then override(in the
derived class), but fails to compare and contrast the two...
I've read this (short snippet)three times and by the looks of it they both
do exactly the same thing- allow you to derive from the base and save...
|
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 'override' remains a virtual method for further
descendent classes."
Now here's my question: Let's say you have base class A, and
subclasses B and C. Class A contains a virtual method, and B contains
an override method. If C didn't have an...
| |
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' modifier. What
I'm trying to figure out now is what happens when you do use modifier
'new' on a derived class method inherited from the base class?
My understanding for the override mechanism with the following code:
class Base
|
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 I've to do by using override.
It's also written, that's possible to "hide" the base class method by
using the new key word.
Because I've already written some C# code and I didn't know anything
|
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 created methods
are not "registered" and only available through the DynamicMethod class.
So a method lookup finds the origin property.
A little test:
public class DerivedClass : BaseClass
{
|
by: Nick Keighley |
last post by:
I take it this is wrong:-
class Direct_draw
{
public:
Direct_draw ();
virtual ~Direct_draw ()
{}
|
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,...
|
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...
| |
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |