an abstract class inherits another abstract class issue - c#

I have an inheritance schema like below:
public abstract class BaseAttachment
{
public abstract string GetName();
}
public abstract class BaseFileAttachment:BaseAttachment
{
public abstract string GetName();
}
public class ExchangeFileAttachment:BaseFileAttachment
{
string name;
public override string GetName()
{
return name;
}
}
I basically want to call GetName() method of the ExchangeFileAttachment class; However, the above declaration is wrong. Any help with this is appreciated. Thanks

The two immediate problems I see is that your final ExchangeFileAttachment class is declared abstract, so you'll never be able to instantiate it. Unless you have another level of inheritance you are not showing us, calling it will not be possible - there's no way to access it. The other problem is that BaseFileAttachment has a property that is hiding the GetName() in BaseAttachment. In the structure you are showing us, it is redundant and can be omitted. So, the 'corrected' code would look more like:
public abstract class BaseAttachment
{
public abstract string GetName();
}
public abstract class BaseFileAttachment : BaseAttachment
{
}
public class ExchangeFileAttachment : BaseFileAttachment
{
string name;
public override string GetName()
{
return name;
}
}
I put corrected in quotes because this use-case still does not make a ton of sense so I'm hoping you can give more information, or this makes a lot more sense on your end.

Just remove the redeclaration from BaseFileAttachment:
public abstract class BaseFileAttachment : BaseAttachment
{
}
BaseFileAttachment already inherits the abstract GetName declaration from BaseAttachment. If you really want to mention it again in BaseFileAttachment, use the override keyword:
public abstract class BaseFileAttachment : BaseAttachment
{
public override abstract string GetName(); // that's fine as well
}

Related

A proper way of defining methods strongly connected to the interface

I have a following interface:
interface IName
{
string Name { get; }
}
And some base class BaseClass. Childs of this class may implement IName interface, but not all of them do.
If a Child implements IName, I would also like to override ToString() method, exactly the same way for all cases, as follows:
public override string ToString()
{
return Name;
}
It seems that a good place for overriding ToString() would be in IName interface, but i believe that it is not possible in C#.
Implementing ToString() in every class seems a bad idea too, because it's a lot of code redundancy (and a waste of time).
What is a proper solution for a case like this?
I'd suggest to make a second base class as such:
public abstract class BaseClass
{
// your base class implementation
}
public abstract class NamedBaseClass : BaseClass, IName
{
public string Name { get; set;}
public override string ToString()
{
return Name;
}
}
this way, if you want a child to implement both BaseClass and IName, then you should inherit from NamedBaseClass.
According to your words that "some might implement IName and some not", then BaseClass should not implement IName, but you should still have some sort of a base implementation. this is my solution.
EDIT:
to make a single class which returns the name and has nothing to do with BaseClass, then you can make an unrelated abstract implementation just for that:
public abstract class NameStringClass : IName
{
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
Consider cFoo as the child class which has IName. And cBar parent class, and cBaz as a class which doesnt implement IName.
public interface IName
{
string Name { get; }
}
public class CBaz : CBar
{
}
public class CFoo : CBar, IName
{
public CFoo(string name)
{
Name = name;
}
public string Name { get; }
}
abstract public class CBar
{
public override string ToString()
{
if (this is IName)
{
var temp = (IName) (this);
return temp.Name;
}
else
{
return base.ToString();
}
}
}
[Old Answer]
Note that an interface is essentially empty. You need to think of an interface as bearing more of the meaning of contract, implying that the person whom implements this interface as requiring to implement a property/method/field/etc of such signature.
For implementation specific tasks which may be shared commonly by many classes, an abstract class is more suitable.
The moment you need to resolve some kind of logic, you must go through a gateway of implementation, some implementation must occur, interfaces are essentially empty templates. The most loose way of implementing this is via an abstract class which contains both the name property and the ToString override, from which you then inherit from for all your subsequent classes.
At the same time you can consider an abstract class for your CBar class. As well as calling the base method base.ToString().
[End of Old Answer]

What would be the right modifier?

I have the following class with some methods and I would like to use this as a base class of another class.
public class BaseClass
{
public string DoWork(string str)
{
// some codes...
}
// other methods...
}
I don't want this class to be instantiated, but the derived class should still use the original implementation of the methods of its base class.
Is it possible? What should be my modifier?
Since you don't want this class to be instantiated, make it an abstract class. You can still have implementation on the class.
abstract
snippet,
public abstract class BaseClass
{
public virtual string DoWork(string str)
{
// can have implementation here
// and classes that inherits can overide this method because of virtual.
}
// other methods...
}
Make BaseClass abstract:
public abstract class BaseClass
{
// Only available to BaseClass
private string _myString;
public string DoWork(string str)
{
// Available to everyone
return _myString;
}
protected void DoWorkInternal() {
// Only available to classes who inherit base class
}
}
This way, you can define your own code within BaseClass - but it cannot be initialized directly, it must be inherited from.

C# interface and abstract class

I have defined an interface, an abstract class that implements that interface and a class that derives from the abstract class. I need the interface because I am using a dynamic loader to implement the plugins and I created an abstract class to implement a few things that all plugins will have.
Now I want to implement a class-wide string as a name. What I created is this:
public interface IDevicePlugin {
string name { get; }
}
abstract public class DevicePlugin : IDevicePlugin {
abstract public string name { get; }
}
public class somePlugin : DevicePlugin, IDevicePlugin {
public override string name {
get {
return "my name";
}
}
}
But this gives me the error "cannot override because 'name' is not a property'. If I remove the override, it says it is hiding the inherited member 'name'.
How do I correctly implement this?
It doesn't error for me, but... I suspect that this is because you are re-implementing the interface. Drop the , IDevicePlugin in somePlugin:
public class somePlugin : DevicePlugin {
public override string name {
get { return "my name"; }
}
}
It inherits the interface from the parent class.

4 Classes with the same properties and methods - is it possible to create only one?

i have another question open here on SO and after thinking about it, i may be approaching this in the wrong way.
i have 4 classes, that have the same properties and methods.
some of the classes, have their own properties and methods ( not overrides of the existing ones ).
currently i create each class as:
public class ClassOne
{
public ClassOne()
{
}
public int ID {get;set;}
// More properties here
public void Set(){
// Do Stuff to save this
}
// More Methods here
}
cant i create one class that will generate all of the 4 classes?
and in the classes themselfs i only create specific properties/methods for that class?
repeating the code seems very odd to me, im sure there must be a way to do this, just dont know how.
Your situation is one of the main reasons why inheritance was invented. So with that, you can write
public class Base
{
// Properties and methods common to all
}
public class ClassOne : Base
{
// Properties and methods specific to ClassOne
}
public class ClassTwo : Base
{
// Properties and methods specific to ClassTwo
}
public class ClassThree : Base
{
// Properties and methods specific to ClassThree
}
public class ClassFour : Base
{
// Properties and methods specific to ClassFour
}
As requested, more code, using interfaces and abstract classes:
An interface is just a blueprint, defining what properties and methods are required to be compatible with other "BaseClasses"
public interface IBaseClass
{
public int ID {get;set;}
public void Set();
}
Abstract classes can contain code, but can not be instantiated, they are form of starting point for a class, but not a complete class themselves.
public abstract class ABaseClass : IBaseClass
{
public int ID {get;set;}
public void Set(){
// Do Stuff to save
}
}
Each class inherits from the abstract class and can then override and implement whatever it wants, customizing it however is necessary.
public class ClassOne : ABaseClass
{
}
public class ClassTwo : ABaseClass
{
}
public class ClassThree : ABaseClass
{
}
public class ClassFour : ABaseClass
{
}
ps. not entirely sure if my syntax is 100% correct
Could you simply make a base class with your properties and inherit from that class?
Why not use inheritance??
public class ClassOne
{
public ClassOne()
{
}
public virtual int ID {get;set;}
// More properties here
public virtual void Set(){
// Do Stuff to save this
}
// More Methods here }
public class ClassTwo : ClassOne
{
public string ClassTwoString { get; set; }
}
public class ClassThree : ClassOne
{
public string ClassThreeString { get; set; }
}
Can you make them all inherit off of the same class? If so, that sounds ideal.
Barring the possibility of making them inherit, you could write an interface that describes the methods and properties which each of them use. Then you can call each instance of the class through the same interface.
Barring again that possibility, you could write a reflective assignor/accessor. But you shouldn't do that.

Setting value to a field in the derived class

using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(new B("MyName").Name);
}
}
abstract class A
{
public A(string name)
{
this.GetType().GetField("Name").SetValue(this, name);
}
}
class B : A
{
public B(string name)
: base(name)
{
}
public string Name
{
set;
get;
}
}
}
Is it possible to do something like that?
I can't stress how very, very, very bad this is. You are creating an inverse coupling that is convoluted, confusing and contrived, severely lacking in clarity, failing best practices and object oriented principals, which is going to create a maintenance and management nightmare for people implementing derivatives of your abstract class. Do the right thing!!
abstract class A
{
protected A(string name)
{
Name = name;
}
public abstract string Name
{
get;
protected set;
}
}
class B: A
{
public B(string name) : base(name)
{
}
private string m_name;
public override string Name
{
get { return "B Name: " + m_name; }
protected set
{
m_name = value;
}
}
}
It is possible, but i wouldn´t recommend to do that. The problem is that your base class knows to much about the class that are derived from it.
When you derive a class from your abstract base class that does not define the property Name you get an Exception on runtime.
If you expect that each class, that is derived from your base class, has a property Name, then it would be easier to define the Property Name in your abstract base class and set the property with you constructor.
It's really bad form to do that. Generally you should just call a method like 'SetPossibleData()', and force all children to implement it in a fashion they decide.
Why do you need to do this?
Use GetProperty() Method,
public A(string name)
{
this.GetType().GetProperty("Name").SetValue(this,name,null);
}
It would be really straight forward if every class initializes the fields and properties it defines. Why does B expect the base class initialize its Name?
abstract class A
{
public A()
{
}
}
class B : A
{
// I know, its trivial, but it does the same ...
public B(string name) : base()
{
Name = name;
}
public string Name { set; get; }
}
The only thing I could think of why you wrote this code is that the base class has some logic to initialize the field. Straight forward would be to let the derived class call the logic, but initialize the field itself:
abstract class A
{
public A()
{
}
protected string GenerateName(string someArg)
{
// complicated logic to generate the name
}
}
class B : A
{
public B(string someArg) : base()
{
Name = base.GenerateName(someArg);
}
public string Name { set; get; }
}

Categories

Resources