How do you make a member of a class mandatory to override with a _new_
definition? For example, when inheriting from
System.Collections.CollectionBase, you are required to implement certain
methods, such as public void Add(MyClass c).
How can I enforce the same behavior (of requiring to implement a member with
a new return type in an inherited class) in the master class (similar to the
CollectionBase)?
I have a class called "LinearNavigator" which is a generic manager for
Forward and Back operations (like IE) that is inherited to provide
interaction with specific types (like inheriting CollectionBase).
LinearNavigator has methods Next & Back which I want to make mandatory to
implement and a method called Clear that should not be
overridden/re-implemented.
Thanks! 11 2020
Hi,
You need to define LinearNavigator as an abstract class.
You can contain methods and properties as normal, but the
methods for which you wish to enforce implementation,
define them as abstract. Naturally these cannot contain
any implementation in the base class:
The inheriting class must use the override keyword. If
this is omitted, or the implementation is omitted the
compiler reports an error.
For example:
/// Base Class
public abstract class CMostlyAbstract
{
public int x = 1;
public CMostlyAbstract()
{
// TODO: Add constructor logic
}
public abstract void MustOverride();
public void Clear()
{
x=0;
}
}
/// Overriding class
public class COverride : CMostlyAbstract
{
public COverride()
{
// TODO: Add constructor logic
}
public void Reset()
{
base.Clear();
}
public override void MustOverride()
{
// Specific Implementation here!
}
}
/// Usage
COverride co = new COverride();
co.MustOverride();
co.Clear();// Call base method
co.Reset();// call COverride, which calls base
HTH,
Martin -----Original Message----- How do you make a member of a class mandatory to
override with a _new_definition? For example, when inheriting from System.Collections.CollectionBase, you are required to
implement certainmethods, such as public void Add(MyClass c).
How can I enforce the same behavior (of requiring to
implement a member witha new return type in an inherited class) in the master
class (similar to theCollectionBase)?
I have a class called "LinearNavigator" which is a
generic manager forForward and Back operations (like IE) that is inherited
to provideinteraction with specific types (like inheriting
CollectionBase).LinearNavigator has methods Next & Back which I want to
make mandatory toimplement and a method called Clear that should not be overridden/re-implemented.
Thanks!
.
So,where would it best to use an Interface over an abstract class and vica
versa?
"mk" <mk@nospam.com> wrote in message
news:00****************************@phx.gbl... Hi, You need to define LinearNavigator as an abstract class. You can contain methods and properties as normal, but the methods for which you wish to enforce implementation, define them as abstract. Naturally these cannot contain any implementation in the base class:
The inheriting class must use the override keyword. If this is omitted, or the implementation is omitted the compiler reports an error.
For example:
/// Base Class public abstract class CMostlyAbstract { public int x = 1; public CMostlyAbstract() { // TODO: Add constructor logic } public abstract void MustOverride();
public void Clear() { x=0; } } /// Overriding class public class COverride : CMostlyAbstract { public COverride() { // TODO: Add constructor logic }
public void Reset() { base.Clear(); } public override void MustOverride() { // Specific Implementation here! } } /// Usage COverride co = new COverride(); co.MustOverride(); co.Clear();// Call base method co.Reset();// call COverride, which calls base
HTH,
Martin
-----Original Message----- How do you make a member of a class mandatory to override with a _new_definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certainmethods, such as public void Add(MyClass c).
How can I enforce the same behavior (of requiring to implement a member witha new return type in an inherited class) in the master class (similar to theCollectionBase)?
I have a class called "LinearNavigator" which is a generic manager forForward and Back operations (like IE) that is inherited to provideinteraction with specific types (like inheriting CollectionBase).LinearNavigator has methods Next & Back which I want to make mandatory toimplement and a method called Clear that should not be overridden/re-implemented.
Thanks!
. http://msdn.microsoft.com/library/de...us/vbcon/html/
vbconabstractclassesversusinterfaces.asp
Found a link to the abstract class vs interface question :D
"Jack Meyhoff" <po********@127.0.0.3> wrote in message
news:O3**************@TK2MSFTNGP09.phx.gbl... So,where would it best to use an Interface over an abstract class and vica versa?
"mk" <mk@nospam.com> wrote in message news:00****************************@phx.gbl... Hi, You need to define LinearNavigator as an abstract class. You can contain methods and properties as normal, but the methods for which you wish to enforce implementation, define them as abstract. Naturally these cannot contain any implementation in the base class:
The inheriting class must use the override keyword. If this is omitted, or the implementation is omitted the compiler reports an error.
For example:
/// Base Class public abstract class CMostlyAbstract { public int x = 1; public CMostlyAbstract() { // TODO: Add constructor logic } public abstract void MustOverride();
public void Clear() { x=0; } } /// Overriding class public class COverride : CMostlyAbstract { public COverride() { // TODO: Add constructor logic }
public void Reset() { base.Clear(); } public override void MustOverride() { // Specific Implementation here! } } /// Usage COverride co = new COverride(); co.MustOverride(); co.Clear();// Call base method co.Reset();// call COverride, which calls base
HTH,
Martin
-----Original Message----- How do you make a member of a class mandatory to override with a _new_definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certainmethods, such as public void Add(MyClass c).
How can I enforce the same behavior (of requiring to implement a member witha new return type in an inherited class) in the master class (similar to theCollectionBase)?
I have a class called "LinearNavigator" which is a generic manager forForward and Back operations (like IE) that is inherited to provideinteraction with specific types (like inheriting CollectionBase).LinearNavigator has methods Next & Back which I want to make mandatory toimplement and a method called Clear that should not be overridden/re-implemented.
Thanks!
.
This queston was posted just a few days ago, though I'm not sure there
is a definitive answer.
I use iterfaces when I need to perform the same method on disparate,
hierarchically unrelated classes. As an example, I may need to call a
method called Validate on different types of business objects. The
business objects are not related at all, but each need to be validated
before taking some other action. By imherinting an interface called
IValidate, I can call the Validate method on each of these objects
without knowing their real type, by casting each of them to an
IValidate type and calling the method. If the object doesn't implent
the IValidate interface, then I skip the call. These could be objects
contained in an arraylist or other collection type.
I use an abstract class if I want to force an object to implement an
overridden method, but when there are other characteristics that a
class would share with its base class.
Jonathan Schafer
On Sat, 9 Aug 2003 19:16:17 +0200, "Jack Meyhoff"
<po********@127.0.0.3> wrote: So,where would it best to use an Interface over an abstract class and vica versa?
"mk" <mk@nospam.com> wrote in message news:00****************************@phx.gbl... Hi, You need to define LinearNavigator as an abstract class. You can contain methods and properties as normal, but the methods for which you wish to enforce implementation, define them as abstract. Naturally these cannot contain any implementation in the base class:
The inheriting class must use the override keyword. If this is omitted, or the implementation is omitted the compiler reports an error.
For example:
/// Base Class public abstract class CMostlyAbstract { public int x = 1; public CMostlyAbstract() { // TODO: Add constructor logic } public abstract void MustOverride();
public void Clear() { x=0; } } /// Overriding class public class COverride : CMostlyAbstract { public COverride() { // TODO: Add constructor logic }
public void Reset() { base.Clear(); } public override void MustOverride() { // Specific Implementation here! } } /// Usage COverride co = new COverride(); co.MustOverride(); co.Clear();// Call base method co.Reset();// call COverride, which calls base
HTH,
Martin
>-----Original Message----- >How do you make a member of a class mandatory to override with a _new_ >definition? For example, when inheriting from >System.Collections.CollectionBase, you are required to implement certain >methods, such as public void Add(MyClass c). > >How can I enforce the same behavior (of requiring to implement a member with >a new return type in an inherited class) in the master class (similar to the >CollectionBase)? > >I have a class called "LinearNavigator" which is a generic manager for >Forward and Back operations (like IE) that is inherited to provide >interaction with specific types (like inheriting CollectionBase). >LinearNavigator has methods Next & Back which I want to make mandatory to >implement and a method called Clear that should not be >overridden/re-implemented. > >Thanks! > > >. >
Hi,
It really depends on what you're trying to achieve,
however an Interface contains no implementation
whatsoever and is commonly used just to define a
contract. For example, if you define an interface in a
separate application, then several applications can
contain classes that implement that foreign interface,
and can pass objects to one another that may be cast to
that interface and the correct methods called against
them.
An interface could not have solved all of Noah's
requirements, because he had a Clear() method that must
be implemented in the base class. However he could have
defined an interface, and inherited that in his base
class. This scenario would have been useful for remoting
or distributed situations, but would not have mattered
much as the key element was providing a base class with
some overridable and some sealed methods rather than
defining an abstract contract.
HTH,
Martin -----Original Message----- So,where would it best to use an Interface over an
abstract class and vicaversa?
"mk" <mk@nospam.com> wrote in message news:00****************************@phx.gbl... Hi, You need to define LinearNavigator as an abstract
class. You can contain methods and properties as normal, but
the methods for which you wish to enforce implementation, define them as abstract. Naturally these cannot
contain any implementation in the base class:
The inheriting class must use the override keyword. If this is omitted, or the implementation is omitted the compiler reports an error.
For example:
/// Base Class public abstract class CMostlyAbstract { public int x = 1; public CMostlyAbstract() { // TODO: Add constructor logic } public abstract void MustOverride();
public void Clear() { x=0; } } /// Overriding class public class COverride : CMostlyAbstract { public COverride() { // TODO: Add constructor logic }
public void Reset() { base.Clear(); } public override void MustOverride() { // Specific Implementation here! } } /// Usage COverride co = new COverride(); co.MustOverride(); co.Clear();// Call base method co.Reset();// call COverride, which calls base
HTH,
Martin
>-----Original Message----- >How do you make a member of a class mandatory to override with a _new_ >definition? For example, when inheriting from >System.Collections.CollectionBase, you are required to implement certain >methods, such as public void Add(MyClass c). > >How can I enforce the same behavior (of requiring to implement a member with >a new return type in an inherited class) in the master class (similar to the >CollectionBase)? > >I have a class called "LinearNavigator" which is a generic manager for >Forward and Back operations (like IE) that is
inherited to provide >interaction with specific types (like inheriting CollectionBase). >LinearNavigator has methods Next & Back which I want
to make mandatory to >implement and a method called Clear that should not be >overridden/re-implemented. > >Thanks! > > >. >
.
Okay, this is not what I'm looking for. Maybe an abstract class, but the
members can't be abstract since I do need an application of the method in
the base class.
Just like CollectionBase does implement IList's Add & Remove methods, which
you have to override in an inherited class. But in CollectionBase, you have
to actually cast to IList to get access to the Add & Remove methods.
-Noah
"Jack Meyhoff" <po********@127.0.0.3> wrote in message
news:O3**************@TK2MSFTNGP09.phx.gbl... So,where would it best to use an Interface over an abstract class and vica versa?
"mk" <mk@nospam.com> wrote in message news:00****************************@phx.gbl... Hi, You need to define LinearNavigator as an abstract class. You can contain methods and properties as normal, but the methods for which you wish to enforce implementation, define them as abstract. Naturally these cannot contain any implementation in the base class:
The inheriting class must use the override keyword. If this is omitted, or the implementation is omitted the compiler reports an error.
For example:
/// Base Class public abstract class CMostlyAbstract { public int x = 1; public CMostlyAbstract() { // TODO: Add constructor logic } public abstract void MustOverride();
public void Clear() { x=0; } } /// Overriding class public class COverride : CMostlyAbstract { public COverride() { // TODO: Add constructor logic }
public void Reset() { base.Clear(); } public override void MustOverride() { // Specific Implementation here! } } /// Usage COverride co = new COverride(); co.MustOverride(); co.Clear();// Call base method co.Reset();// call COverride, which calls base
HTH,
Martin
-----Original Message----- How do you make a member of a class mandatory to override with a _new_definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certainmethods, such as public void Add(MyClass c).
How can I enforce the same behavior (of requiring to implement a member witha new return type in an inherited class) in the master class (similar to theCollectionBase)?
I have a class called "LinearNavigator" which is a generic manager forForward and Back operations (like IE) that is inherited to provideinteraction with specific types (like inheriting CollectionBase).LinearNavigator has methods Next & Back which I want to make mandatory toimplement and a method called Clear that should not be overridden/re-implemented.
Thanks!
.
I don't think I'm understanding something...
Do you just mean explicit interface implementation? That's what
CollectionBase uses for IList (as you mentioned, you have to cast
CollectionBase to an IList to use it).
But in what way do you have to override ILists's Add & Remove in
CollectionBase. This compiles just fine (doesn't do much, granted).
class Class1 : System.Collections.CollectionBase
{
public Class1()
{
}
public static void Main()
{
Class1 class1 = new Class1();
}
}
--mike
"Noah Coad [MVP .NET/C#]" <no**@coad.net> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl... Okay, this is not what I'm looking for. Maybe an abstract class,
but the members can't be abstract since I do need an application of the
method in the base class.
Just like CollectionBase does implement IList's Add & Remove
methods, which you have to override in an inherited class. But in CollectionBase,
you have to actually cast to IList to get access to the Add & Remove methods.
-Noah
"Jack Meyhoff" <po********@127.0.0.3> wrote in message news:O3**************@TK2MSFTNGP09.phx.gbl... So,where would it best to use an Interface over an abstract class
and vica versa?
"mk" <mk@nospam.com> wrote in message news:00****************************@phx.gbl... Hi, You need to define LinearNavigator as an abstract class. You can contain methods and properties as normal, but the methods for which you wish to enforce implementation, define them as abstract. Naturally these cannot contain any implementation in the base class:
The inheriting class must use the override keyword. If this is omitted, or the implementation is omitted the compiler reports an error.
For example:
/// Base Class public abstract class CMostlyAbstract { public int x = 1; public CMostlyAbstract() { // TODO: Add constructor logic } public abstract void MustOverride();
public void Clear() { x=0; } } /// Overriding class public class COverride : CMostlyAbstract { public COverride() { // TODO: Add constructor logic }
public void Reset() { base.Clear(); } public override void MustOverride() { // Specific Implementation here! } } /// Usage COverride co = new COverride(); co.MustOverride(); co.Clear();// Call base method co.Reset();// call COverride, which calls base
HTH,
Martin
>-----Original Message----- >How do you make a member of a class mandatory to override with a _new_ >definition? For example, when inheriting from >System.Collections.CollectionBase, you are required to implement certain >methods, such as public void Add(MyClass c). > >How can I enforce the same behavior (of requiring to implement a member with >a new return type in an inherited class) in the master class (similar to the >CollectionBase)? > >I have a class called "LinearNavigator" which is a generic manager for >Forward and Back operations (like IE) that is inherited to provide >interaction with specific types (like inheriting CollectionBase). >LinearNavigator has methods Next & Back which I want to make mandatory to >implement and a method called Clear that should not be >overridden/re-implemented. > >Thanks! > > >. >
Okay, well when defining your own implementation of Add & Remove, the 'new'
or 'override' keyword is not nessasary. How is this accomplished?
-Noah
"Michael Mayer" <mr*****@charter.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... I don't think I'm understanding something... Do you just mean explicit interface implementation? That's what CollectionBase uses for IList (as you mentioned, you have to cast CollectionBase to an IList to use it).
But in what way do you have to override ILists's Add & Remove in CollectionBase. This compiles just fine (doesn't do much, granted).
class Class1 : System.Collections.CollectionBase { public Class1() { } public static void Main() { Class1 class1 = new Class1(); } }
--mike "Noah Coad [MVP .NET/C#]" <no**@coad.net> wrote in message news:%2******************@TK2MSFTNGP10.phx.gbl... Okay, this is not what I'm looking for. Maybe an abstract class, but the members can't be abstract since I do need an application of the method in the base class.
Just like CollectionBase does implement IList's Add & Remove methods, which you have to override in an inherited class. But in CollectionBase, you have to actually cast to IList to get access to the Add & Remove methods.
-Noah
"Jack Meyhoff" <po********@127.0.0.3> wrote in message news:O3**************@TK2MSFTNGP09.phx.gbl... So,where would it best to use an Interface over an abstract class and vica versa?
"mk" <mk@nospam.com> wrote in message news:00****************************@phx.gbl... > Hi, > You need to define LinearNavigator as an abstract class. > You can contain methods and properties as normal, but the > methods for which you wish to enforce implementation, > define them as abstract. Naturally these cannot contain > any implementation in the base class: > > The inheriting class must use the override keyword. If > this is omitted, or the implementation is omitted the > compiler reports an error. > > For example: > > /// Base Class > public abstract class CMostlyAbstract > { > public int x = 1; > public CMostlyAbstract() > { > // TODO: Add constructor logic > } > public abstract void MustOverride(); > > public void Clear() > { > x=0; > } > } > /// Overriding class > public class COverride : CMostlyAbstract > { > public COverride() > { > // TODO: Add constructor logic > } > > public void Reset() > { > base.Clear(); > } > public override void MustOverride() > { > // Specific Implementation here! > } > } > /// Usage > COverride co = new COverride(); > co.MustOverride(); > co.Clear();// Call base method > co.Reset();// call COverride, which calls base > > > HTH, > > Martin > > >-----Original Message----- > >How do you make a member of a class mandatory to > override with a _new_ > >definition? For example, when inheriting from > >System.Collections.CollectionBase, you are required to > implement certain > >methods, such as public void Add(MyClass c). > > > >How can I enforce the same behavior (of requiring to > implement a member with > >a new return type in an inherited class) in the master > class (similar to the > >CollectionBase)? > > > >I have a class called "LinearNavigator" which is a > generic manager for > >Forward and Back operations (like IE) that is inherited > to provide > >interaction with specific types (like inheriting > CollectionBase). > >LinearNavigator has methods Next & Back which I want to > make mandatory to > >implement and a method called Clear that should not be > >overridden/re-implemented. > > > >Thanks! > > > > > >. > >
Maybe the Stragegy Design Pattern is what you really need?
This allows you to provide an algorithm in an abstract base class which
calls abstract methods.
Derivatives supply the implementation of the methods that get called in the
base class' algorithm.
For example (bastardized):
using System;
namespace TestApp
{
class MainClass
{
[STAThread]
static void Main(string[] args)
{
ParentClass p = new ADerivative();
p.DoSomething();
Console.Read();
}
}
public abstract class ParentClass
{
public void DoSomething()
{
for (int i=0;i<100;i++)
{
SuppliedInDerived(i);
}
}
public abstract void SuppliedInDerived(int i);
}
public class ADerivative : ParentClass
{
public override void SuppliedInDerived(int i)
{
Console.WriteLine("this example sucks: " + i);
}
}
}
"Noah Coad [MVP .NET/C#]" <no**@coad.net> wrote in message
news:OA*************@tk2msftngp13.phx.gbl... Okay, well when defining your own implementation of Add & Remove, the
'new' or 'override' keyword is not nessasary. How is this accomplished?
-Noah
"Michael Mayer" <mr*****@charter.net> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... I don't think I'm understanding something... Do you just mean explicit interface implementation? That's what CollectionBase uses for IList (as you mentioned, you have to cast CollectionBase to an IList to use it).
But in what way do you have to override ILists's Add & Remove in CollectionBase. This compiles just fine (doesn't do much, granted).
class Class1 : System.Collections.CollectionBase { public Class1() { } public static void Main() { Class1 class1 = new Class1(); } }
--mike "Noah Coad [MVP .NET/C#]" <no**@coad.net> wrote in message news:%2******************@TK2MSFTNGP10.phx.gbl... Okay, this is not what I'm looking for. Maybe an abstract class, but the members can't be abstract since I do need an application of the method in the base class.
Just like CollectionBase does implement IList's Add & Remove methods, which you have to override in an inherited class. But in CollectionBase, you have to actually cast to IList to get access to the Add & Remove methods.
-Noah
"Jack Meyhoff" <po********@127.0.0.3> wrote in message news:O3**************@TK2MSFTNGP09.phx.gbl... > So,where would it best to use an Interface over an abstract class and vica > versa? > > > "mk" <mk@nospam.com> wrote in message > news:00****************************@phx.gbl... > > Hi, > > You need to define LinearNavigator as an abstract class. > > You can contain methods and properties as normal, but the > > methods for which you wish to enforce implementation, > > define them as abstract. Naturally these cannot contain > > any implementation in the base class: > > > > The inheriting class must use the override keyword. If > > this is omitted, or the implementation is omitted the > > compiler reports an error. > > > > For example: > > > > /// Base Class > > public abstract class CMostlyAbstract > > { > > public int x = 1; > > public CMostlyAbstract() > > { > > // TODO: Add constructor logic > > } > > public abstract void MustOverride(); > > > > public void Clear() > > { > > x=0; > > } > > } > > /// Overriding class > > public class COverride : CMostlyAbstract > > { > > public COverride() > > { > > // TODO: Add constructor logic > > } > > > > public void Reset() > > { > > base.Clear(); > > } > > public override void MustOverride() > > { > > // Specific Implementation here! > > } > > } > > /// Usage > > COverride co = new COverride(); > > co.MustOverride(); > > co.Clear();// Call base method > > co.Reset();// call COverride, which calls base > > > > > > HTH, > > > > Martin > > > > >-----Original Message----- > > >How do you make a member of a class mandatory to > > override with a _new_ > > >definition? For example, when inheriting from > > >System.Collections.CollectionBase, you are required to > > implement certain > > >methods, such as public void Add(MyClass c). > > > > > >How can I enforce the same behavior (of requiring to > > implement a member with > > >a new return type in an inherited class) in the master > > class (similar to the > > >CollectionBase)? > > > > > >I have a class called "LinearNavigator" which is a > > generic manager for > > >Forward and Back operations (like IE) that is inherited > > to provide > > >interaction with specific types (like inheriting > > CollectionBase). > > >LinearNavigator has methods Next & Back which I want to > > make mandatory to > > >implement and a method called Clear that should not be > > >overridden/re-implemented. > > > > > >Thanks! > > > > > > > > >. > > > > >
CollectionBase is an abstract class. It cannot even be instantiated.
CollectionBase also happens to implements IList.
You can recreate this behavior in your object model too.
You can also have a derived class call its base class's methods, as well as
override them (or neither). You can even shadow the base classes in derived
classes, also this is not recommended.
I think all the flexibility you need is there. But perhaps I am missing
something in the original question. Maybe you wouldn't mind restating it
with a code sketch example to make it crystal clear what you want?
Thanks for all your help and information. I haven't found quite what I'm
looking for yet and this thread is getting old, so I'll reconsider my
question, try to study and learn some more, and repost it more propperly
stated at a later time. Thanks once again!
-Noah
"mia lanui" <mi**@email.com> wrote in message
news:2C*****************@nwrdny02.gnilink.net... I am getting tired, I meant to write "shadow the base class's methods in
the derived classes, although this is not recommended"
"mia lanui" <mi**@email.com> wrote in message news:sA*****************@nwrdny02.gnilink.net... CollectionBase is an abstract class. It cannot even be instantiated. CollectionBase also happens to implements IList.
You can recreate this behavior in your object model too.
You can also have a derived class call its base class's methods, as well as override them (or neither). You can even shadow the base classes in derived classes, also this is not recommended.
I think all the flexibility you need is there. But perhaps I am missing something in the original question. Maybe you wouldn't mind restating it with a code sketch example to make it crystal clear what you want?
This discussion thread is closed Replies have been disabled for this discussion. Similar topics
8 posts
views
Thread by Scott J. McCaughrin |
last post: by
|
4 posts
views
Thread by Ian Malone |
last post: by
|
6 posts
views
Thread by Querejeto |
last post: by
|
7 posts
views
Thread by alex.mizrahi |
last post: by
|
6 posts
views
Thread by Bill Rubin |
last post: by
|
3 posts
views
Thread by Wayne Brantley |
last post: by
|
3 posts
views
Thread by Jeff |
last post: by
|
52 posts
views
Thread by Neil |
last post: by
|
10 posts
views
Thread by shanknbake |
last post: by
| | | | | | | | | | |