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.
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.
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 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.
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
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.