I was wondering: can an interface inherit from another class?
I'm trying to let an interface inherit from the MarshalByRefObject.
My intent is that all classes implementing the interface also inherit from that class.
No, it cannot.
An interface can only specify other interfaces that must be implemented. This is done by using the same syntax as inheritance, but it's something different.
You could use an abstract class instead that inherits from MarshalByRefObject and and requires your interfaces to be implemented.
Depending on how you need to enforce your requirement, generic constraints might help, too. For generic type parameters, you can set class constraints, like class Argh<T> where T : MarshalByRefObject, ISomeInterface.
An interface cannot inherit from a class. In order to do that C# would need to support multiple inheritance of implementation which is currently not supported.
Imagine if you could derive an interface, IMyInterface, from a class, MyClass. Then when you come to declare another class that implements that interface, you would have to write something like:
public class MyImplementingClass: MyBaseClass, IMyInterface
But that implies multiple inheritance of implementation since you are inheriting from both MyBaseClass and MyClass.
No, but an interface can inherit from another interface.
No, an interface cannot have any implementation, so it can't inherit from a class. However you can make an abstract class that inherits from MarshalByRefObject.
No. Classes can implement interfaces. Its not the other way round.
Class can inherit class and implement interface.
Interface can implement only interface but can neither inherit nor implement classes.
Related
I'm reading some code online where someone implemented the following classes: IMapObj which is a normal interface, AbstractMapObj that derives from that interface and a lot of map objects that derive from AbstrsctMapObj.
Throughout all his code, he refers to IMapObj and not AbstractMapObj.
What's the benefit of using an interface and an abstract class instead of just an abstract class? Needless to say no other class derives from IMapObj, only AbstractMapObj.
There is only 1 reason to use both, and that is that the abstract class can provide a default implementation of some or all of the functionality. The interface can be easily mocked for testing.
What's the benefit of using an interface and an abstract class instead of just an abstract class?
In the example posted, there appears to be no real reason to use an abstract class. In other scenarios, the abstract class could provide a common base to a subset of the interface implementations. With the interface providing a more stable/common abstraction for the rest of the application/library.
Generally I would only use an abstract class to share a common implementation, not as an interface definition - but that's just my preference. There are many different styles and patterns that people use.
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.
I have a simple question in C#
Are Interfaces inherited or are they implemented?
Thanks
Classes implement interfaces. Abstract classes are being inherited. A class can inherit from one class, but implement as many interfaces as you want it to.
However, interfaces can also "inherit" other interfaces. They don't really implement them, so it's more like inheriting their interface contracts, but they can implement multiple interfaces, so it's not really inheritance either. A class that will implement an interface which inherits from other interfaces, will have to implement all interface contracts that are included in the original interface, and it's base interfaces as well, recursively.
You should also read about abstract classes, for more useful information on the general subject.
That's just terminology. According to one MSDN page and another one a class or struct implements an interface. However, when you declare a new interface, that interface may inherit from other base interfaces.
that depends if the thing 'using' the interface is a class or another interface.
A class implements the interface (and any interfaces that it inherits).
Another interface inherits (or extends) an interface
You implement an interface by using a class. It is implemented, because it has no definition. Once you have a class. Be it abstract or a normal class, you inherit, because you are inheriting pre-built functionality.
Although there is 2 caveats.
abstract classes don't have to add any implementation to their methods (throwing exceptions is acceptable, or even leaving them empty, or marking the methods and properties as abstract to show the implementation is incomplete. Empty implementations are legitimate as long as the return type is satisfied. They just have to define the methods used by the interface.
you inherit from other classes, but adding 'sealed' to the class name will stop any one else from inheriting it e.g. System.String and System.Type
Classes can implement one / more interfaces implicitly and/or
explicitly.
An interface can inherit other interfaces.
The implicit implemented methods of an Interface can be abstract / virtual can be
overriden in derived classes.
In short Interfaces are implemented and implementation of the interfaces can be inherited
What is different between an abstract and an Interface class in C#?
An interface is not a class, it is just a contract that defines the public members that a class must implement.
An abstract class is just a class from which you cannot create an instance. Normally you would use it to define a base class that defines some virtual methods for derived classes to implement.
Rather than writing whole thing here..
try http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx
A class can implement multiple interfaces but can only inherit from one abstract class.
An abstract class can provide implementation for it's methods. An interface cannot provide implementations.
the level of interface is higher than abstract.
when u're design the strcuture, draw the uml, u should use interface.
when u're implement, then u should use abstract to extract repeat things.
anyway, the different is not only a syntax problem..
hope it helps.
Google "abstract class vs interface" and you'll get lots of explanatory articles...
A class can implement multiple
interfaces but can only inherit from
one abstract class.
Also, abstract classes may have some functions defined but interfaces will not have any function definition and the deriving class must define all of them.
I would explain this through the usage. Abstract class can be used when there is only one hierarchy, additionally without default implementation; while interface can be used across hierarchies (horizontally), often referred to as a behavior.
Interface is also an abstraction and in c# substitutes multiple class inheritance, so this may be confusing, but you have to distinguish when to use what.
Hope this helps,
Robert
The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.
When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.
I would like to pass a generic interface to a function:
private I<T> CreateStubRepository<T, I >()
where I : aGenericBaseClass
So i was wondering if generic interfaces implement a base class or specific interface?
I know by using reflection you can test if it is a generic class but I dont see that helping me
Well. What's the point of forcing the usage of any interface? I really do not get it (or your question).
You should more likely do something like this:
public interface IMyRepository<T>
{
}
public class Repository<T> : IMyRepository<T>
{
}
private IMyRepository<TEntity> CreateStubRepository<TEntity>()
{
return new Repository<TEntity>();
}
var repos = CreateStubRepository<User>();
Update
thanks for your answer but thats not what I am asking. What I want to know is does a class that implements a generic interface have a base class or does it inherit from an interface? I dont want to force any interface its more a question of is the object passed generic
Classes do not inherit interfaces. They implement them. The different is subtle but important.
A class can only inherit another class. This means that if you do not specify that a class inherits from another it will still inherit from object. And that wont change no matter how many interfaces a class implement.
class MyClass : ICoolInterface // inherits object
class MyList : ArrayList, ISomeInterface // inherits ArrayList
class MyGenericList<T> : IList<T> // inherits object.
Generic or non-generic classes can implement or inherit from generic or non-generic interfaces and classes. The only limitation is that the full type of any interface/class implemented/inherited from must be discernible given the full type of the class doing the implementing or inheriting. For example, a Foo<Bar> might inherit from FooBase and implement IDisposable; a FnordDuffleBag might inherit from DuffleBag<Fnord> and implement IReachInto<Fnord>.
Thanks for all the comments I think i was going in the wrong direction, What I was hoping for was that when I applied to a class the framework would know that it inherited from a base class or interface of say aGenericItemBaseClass before it constructed the class at runtime.
Yes I know that I can create a generic class with type parameters and use that but thats not what I was asking (although you may have got that impression from my posting).
At runtime I know that when using reflection I can determine if a class is generic by calling : IsGenericType which returns true if a type is generic.
So what I wanted to know which may have been explained poorly is, when using template types is there anyway to determine if that type is a generic type? It appears the answer is No the IL interperates the class as generic not the compiler.