How to use "new" on an abstract class [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I've tried to search for other questions like mine but I can't find any good result
I'm trying to use a DLL that has a method that need to be called with a new class but that class is abstract.
thanks.
i tried to use new IClientHandler but it just errors

You're trying to instantiate an abstract class in C# code. This is not possible, as an abstract class is a class that cannot be instantiated on its own. Instead, you would need to create a non-abstract class that extends the abstract class and then use that to create an object.
Here's an example:
abstract class AbstractClass
{
public abstract void AbstractMethod();
}
class ConcreteClass : AbstractClass
{
public override void AbstractMethod()
{
Console.WriteLine("I'm a non-abstract method!");
}
}
// Now we can create an instance of ConcreteClass
var instance = new ConcreteClass();
You can then call the AbstractMethod on the instance object, since it has been implemented in the ConcreteClass.
Also my small mistake, as #ewerspej noticed:
IClientHandler is an interface, not an abstract class. Neither can be instantiated. Interfaces require implementations that can be instantiated and abstract classes must be extended by non-abstract classes that can be instantiated.

Related

An empty super class for the sake of a common ancestor [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I got a C# class which is making some actions based on data it receives which is held in other classes:
public Class ActionClass
{
public void DoStuff(List<MyItemsClass> data) { ... }
}
It started as a single class with a single items class, but then I needed to add another action class which does its stuff in a different way and receives different items.
Now I got an abstract class for the action part which enforces its derived class to implement how they do their stuff. It left me with a dilemma about the items class.. I want to have a single signature in the abstract class which receives the needed items class:
public abstract DoStuff(List<MySuperItemsClass> data);
So I created a common ancestor also to the 2 different items classes. However, since they have nothing in common, I got an empty class which now serves only for the purpose of being able to pass that super class, while dynamically instantiating the correct derived classes.
Is it a good practice to have an empty class for the purpose of creating a type? Is there another more common way of achieving what I need without the use of an empty class?
Use generic interface instead of abstract class. Class can implement multiple interfaces.
interface IActionClass<T>
{
void DoStuff(T data);
}
class MyClass : IActionClass<List<MySuperItemsClass>>, IActionClass<int>
{
public void DoStuff(List<MySuperItemsClass> data) { }
public void DoStuff(int data) { }
}

Inheritance with static classes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I don't know whether this question will make sense or if it's a naive question, but I haven't been able to find a satisfactory answer.
I would like to be able to share code across multiple static classes. Is there an "equivalent" of an abstract base class for static classes (i.e. can I have one static class inherit from another one)? If not, why not? Is there another way of sharing code across multiple static classes in a manner analogous to what you do with an abstract base class?
Also, how does a static class compare to a "sealed" class?
You're quite right. Reflection will show that a static class is both abstract and sealed:
public static class MyStaticTest {
}
...
// I'm abstract
Console.WriteLine(typeof(MyStaticTest).IsAbstract ? "I'm abstract" : "");
and that's why you can't create an instance (compile time error): new MyStaticTest();. However, reflection will show that any static class is sealed as well:
// I'm sealed
Console.WriteLine(typeof(MyStaticTest).IsSealed ? "I'm sealed" : "");
and so you can't inherit from it (compile time error): public class MyClass: MyStaticTest {..}. Thus, all you can do with static class is to declare static members (static fields, properties, methods etc.)
The abstract keyword enables to create classes and class members that are incomplete and must be implemented in a derived class whereas The sealed keyword enables you to prevent the inheritance of a class or certain class members that were previously marked virtual.
I think its complete for Above

In c#, can a class inherit an interface? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I know that an interface inherits another interface, and a class can inherit from another interface, but can a class inherit from another interface?
Interfaces are meant to be implemented by classes. when you declare an interface you expect to work with an instance that implements it - that instance must be a class, since you cannot instantiate interfaces.
The case when you declare an interface which inherits from a different interface is possible, and allows you to separate the responsibility of each interface, but force a class to implement all of those responsibilities
As mentioned in my comment on the question, MSDN example for implementing interfaces HERE
It's called "implementing" the interface.
If interface Inter1{} and class A : Inter1, then if class B : A, class B will also "inherit" the interface Inter1.
From Microsoft's Inheritance (C# Programming Guide)
A class or struct can implement multiple interfaces. A class can inherit a base class and also implement one or more interfaces.
So, a class:
can implement multiple interfaces (no limits)
inherits from a base class (just the one. If none is declared, the base class is object by default)

Why doesn't C# allow using parent's constructor by default? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Please, note that I don't ask how to do something. This question is about design decision by language authors.
Let's say you have a base class with constructor and a method:
public class BaseClass
{
public BaseClass(string argument)
{
}
public void SomeMethod()
{
}
}
And you also have derived class that is empty (or, in reality, has other unrelated stuff):
public class DerivedClass : BaseClass
{
public DerivedClass() // Some other constructor not used in the example, just so it will compile
}
So, when you will try to call the method on an instance of the derived class, it will work, but if you try to call the constructor, it will result in compile error:
var d = new DerivedClass("argument"); // error CS1729: The type `DerivedClass' does not contain a constructor that takes `1' arguments
d.SomeMethod(); // Works OK
What is the reason behind this language design decision?
This is because constructors are not inherited.
For more info, see this post for an explanation as to why this is: Why are constructors not inherited?

Different kinds of instance of Child- Parent class hierarchy in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to know the relation between instances of parent child in c#.
Suppose you have two classes Base and Derived:
Base b = new Base()
Derived d = new Derived()
Derived db = new Base()
Base bd = new Derived()
then what is the meaning of above types of objects
Please suggest I am confused.
Assuming that Base is actually the base class of Derived....
Line 1 contains an instance of the base class.
Line 2 contains a instance of the Derived class.
These are both "standard" variables, where the variable type exactly matches the instance it contains.
Line 4 is an instance of derived class, but restricted to only accessing base class methods. Think of it as a slightly restricted view of the derived class.
Line 3 should produce a compilation error. You can't place an instance of a base class in to a variable of a more restrictive type. Think of it like this "All Dogs(derived class) are Animals(base class), but not all Animals are Dogs.

Categories

Resources