I wrote a UserControl which is derived from a standard Control.
This UserControl has a Property which is a list of a abstract class.
But i get an Error of the PropertyGrid which say that it could not create a new instance of the abstract class because it's abstract.
What can i do to handle this?
Abstract classes cannot be created, they must be implemented.
An abstract class is a class that cannot be instantiated, but must be inherited from
This is why you see an error from the PropertyGrid.
You will need to create a base class that implements all or some of your abstract class or
you might be able to use an interface.
Abstact VS Interface.
Related
I am working on c# windows form. I want to do class generalization from given input code by generating an abstract class for common properties of all classes given in input and then other classes are inherited with that abstract class. I am successfully getting abstract class but I don't know how to inherit other classes with that base class so that I can also get those classes in output.
I have the following situation:
I have a base read-only class with simple information
I have a second class that inherits from the base class, this class' members can be updated
public class ExtendedClass : BaseClass
{}
To check if a class can be updated I have an abstract class UpdatableRecord
This abstract class has a property RecordState ("Deleted", "Edited", "New", "None")
this property has the default value "deleted".
What is the best way to build the classes (use different interfaces?, how is the hierarchy?)
I have the following code:
public class HitController : MonoBehaviour, ICentroidHitListener
The way I understand it. HitController inherits from MonoBehaviour and implements the methods in the ICentroidHitListener.
But how can I code this if I just want it only to implement the methods in the ICentroidListener? Do I code it like this:
public class HitController : ICentroidHitListener
In which case that looks like HitController inherits from ICentroidHitListener
If it's an interface, : implements the interface. If it's a class, : inherits from the class.
Therefore HitController : ICentroidHitListener is correct.
Generally, there is no way to differentiate an interface from a class using the : syntax.
That's why it's important to add an I prefix to all your interfaces.
The code you provided, public class HitController : ICentroidHitListener, is correct, and the readers of your code should be able to easily and quickly deduce that it is an interface based on the I prefix.
Taken from here:
When a class or struct implements an interface, the class or struct
provides an implementation for all of the members defined by the
interface. The interface itself provides no functionality that a class
or struct can inherit in the way that base class functionality can be
inherited. However, if a base class implements an interface, the
derived class inherits that implementation. The derived class is said
to implement the interface implicitly.
Taken from here:
Inheritance enables you to create new classes that reuse, extend, and
modify the behavior that is defined in other classes. The class whose
members are inherited is called the base class, and the class that
inherits those members is called the derived class. A derived class
can have only one direct base class. However, inheritance is
transitive. If ClassC is derived from ClassB, and ClassB is derived
from ClassA, ClassC inherits the members declared in ClassB and
ClassA.
In C#, as opposed to languages like C++, multiple inheritance is not allowed, meaning that a class cannot inherit from more than one other class. Conversely, a class can implement more than one interface.
"inheritance" implies extending existing behaviour. An interface has no behaviour, you have to implement all the behaviour of the methods/properties defined in the interface. Hence you "implement" an interface and cannot really inherit anything from it.
If you inherit from something else, you inherit all its concrete behaviour. If some other class implements an interface you don't have to also implement that interface. Now, the class that "implements" that interface could make one or more of those interface implementations "abstract"; in which case you'd be forced to implement the abstract members of the class you're deriving from. But, you don't have to implement that interface--you inherit the interface from the base. You can include the interface in the "base class list" for the derived type, but it's not necessary.
By that token, you'd also "implement" an entirely abstract class. But, it's rare that anyone would use that terminology. For the most part "interface" is syntax sugar in .NET; but it does have IL-level support. e.g. an entirely abstract base class is semantically identical to an interface.
Interfaces let you ensure the only thing you can do is define an interface and not behaviour (an abstract class can have behaviour).
The answer to what you're asking is "yes", if I understand it correctly.
In the first example, you are inheriting from a base class (MonoBehavior) and implementing an interface (ICentroidHitLIstener').
You have access to all the members of MonoBehavior from your HitController class, and your HitController class must provide implementation for all members defined in the ICentroidHitListener interface.
In the second example, you are only implementing the ICentroidHitListener interface.
That's easy.
Inheriting is gaining base class features to yours.
Implementing is creating features according to specified protocol.
class:class and interface:interface is inheritance,
class:interface is implementing,
class:abstract_class are both implementing and inheritance.
Folks
I am surprised to see my code working, that I dont have any abstract method in abstract class. But as per rules we should have atleast one abstract method right ? :S
No, you don't need an abstract method in an abstract class.
The restrictions are:
If a class has an abstract method, property, index, or event then the class must also be abstract.
You can't instantiate an abstract class.
There is no requirement that abstract classes must have abstract methods.
No, this is entirely valid. Indeed, every static class in C# is actually an abstract and sealed class in .NET. (You can't declare an abstract class to be sealed with C# source code though.)
The C# 4 spec explicitly calls this out, in section 10.1.1.1 (abstract classes):
An abstract class is permitted (but not required) to contain abstract members.
It's certainly rare to have an abstract class with no abstract members, but it's not unheard of. (Don't forget that it's not just methods that can be abstract - you can have abstract events and properties, too.)
From MSDN - abstract (Emphasis added):
An abstract class may contain abstract methods and accessors.
Your abstract class can contain nothing but non-abstract methods, or it could be am empty class if you want. It gives you a base class that cannot be instantiated, which could be useful for plymorphism.
You don't need abstract methods to have an abstract class. Sometimes it's useful to just restrict a class from being instantiated, and designate it as a base to some inheritance hierarchy. All the methods can be defined in an abstract class.
I'm not familiar on using abstract class.
I'm trying to call a abstract class and get this error Cannot create an instance of the abstract class or interface and I already research this error but I'm really confused on this.
Here's my code:
string B1String;
while ((B1String = OasisFile.ReadLine()) != null)
{
Questions_Base oQuestions_Base = new Questions_Base(); // error here
oQuestions_Base.Import(B1String);
}
Please advice me.. thanks!
The purpose of an abstract class it to serve as part of a class hierarchy where more-derived classes share some common implementation.
If you have a flight simulator, you might define an abstract class ThingsThatFly that implements some properties (air speed, altitude, heading) and methods (TakeOff(), Land()) that all flying things have in common, but would be declared abstract because ThingsThatFly is an abstraction of all concrete things that fly. You could certainly have classes inbetween as well, for example Cessna172 could inherit from Airplane that inherits from ThingsThatFly. You would do that if all airplanes have some common implementation that e.g. birds don't have (for example, a Fuel Remaining property).
You would then have a number of concrete (= real life) things that fly like a Cessna 172, a Space Shuttle and a Duck. Each of those would be a concrete class that derives from ThingsThatFly
This is different than having the concrete classes implement an interface such as IThingsThatFly in that the abstract class provides not only a definition of the properties and methods that are expected, but also provides a (hopefully useful) implementation of those properties and methods.
An Abstract class can only be inherited.
public class CustomClass : Questions_Base {
}
Here's a link all about abstract classes and how to use them.
You cant create an instance of an abstract class.
You need to define a concrete class that inherits the abstract class, and create an instance of that.
Abstract class is made to be overriden by Derived class. If you have to have Abstract class, first create s Derived class from it and use Derived class contructor.
If it's not important, just remove abstract word from Questions_Base class declaration, so making that non abstract one. Also because in code provided I don't see any abstract member, so may this one is correct choice.
Regards.
An abstract class cannot be instantiated. You must provide an implementation for the class.
abstract class Animal
{
public abstract void Speak() { }
}
public class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Woof");
}
}
See MSDN on abstract for more information
From the documentation: "The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class."
The purpose of using abstract is exactly to prevent instantiation, because you only created the class to use as a base class and never want an instance created.
More here.
An abstract class is one which MUST be inherited.
It falls somewhere between an Interface, which defines only the interface that a class must implement and no implementation code and a class that you can create an instance of which defines both the interface and the implementation code. There are several abstract classes in the .NET framework such as CollectionBase. You cannot create an instance of CollectionBase, it is intended for you to create a class that inherits from it and extends it's capabilities.
You should simpley be able to remove the kwy work "abstract" from your class definition of Questions_Base or create a new class definition that inherits from it.
Abstract classes, marked by the keyword abstract in the class definition, are typically used to define a base class in the hierarchy. What's special about them, is that you can't create an instance of them - if you try, you will get a compile error. Instead, you have to subclass them, as taught in the chapter on inheritance, and create an instance of your subclass. So when do you need an abstract class? It really depends on what you do. To be honest, you can go a long way without needing an abstract class, but they are great for specific things, like frameworks, which is why you will find quite a bit of abstract classes within the .NET framework it self. A good rule of thumb is that the name actually makes really good sense - abstract classes are very often, if not always, used to describe something abstract, something that is more of a concept than a real thing.