Why properties can inherit from interface

Started by
2 comments, last by kitman20022002 8 years, 1 month ago

following from this test http://www.careerride.com/question-5-Csharp.NET they say the answer is A and B but I dont understand Why properties can inherit from interface, because you can't create properties in interface so how can you inherit it? thanks

Advertisement
You can't create fields in interfaces. You can create properties in an interface, you just can't implement them on an interface. Remember, under the hood, a property named "Foo" turns into two methods with names like "get_Foo" and "set_Foo," so declaring a property on an interface is like adding those methods to that interface.

An interface is not a class, it has no implementation and you cant instantiate them directly. They are just a "specification" of the properties and methods that MUST be implemented by a class that implements the interface.

In fact there are two different ways that a class can implement the properties and methods on an interface, implicit and explicit, and neither of them involve inheritance as there is nothing to inherit from. Classes can inherit from only one other class but a class can implement many interfaces both are very different relationships smile.png

This is the second time you have referenced careerride and I have to say I am not impressed with the site or the quality of questions/answer they have. I would say find a good book on C# and read that and work through things in Visual Studio (Which is free), the Wrox books are normmaly very solid on facts and teaching

An interface is not a class, it has no implementation and you cant instantiate them directly. They are just a "specification" of the properties and methods that MUST be implemented by a class that implements the interface.

In fact there are two different ways that a class can implement the properties and methods on an interface, implicit and explicit, and neither of them involve inheritance as there is nothing to inherit from. Classes can inherit from only one other class but a class can implement many interfaces both are very different relationships smile.png

This is the second time you have referenced careerride and I have to say I am not impressed with the site or the quality of questions/answer they have. I would say find a good book on C# and read that and work through things in Visual Studio (Which is free), the Wrox books are normmaly very solid on facts and teaching

I know careerride.com is bad, but I was even surprised because actually one of the company that I interviewed was using this as a test.

This topic is closed to new replies.

Advertisement