What do you use if you want all methods and properties to be implemented

I took the test and asked one question. QUESTION: What do you use if you want all methods and properties to be implemented? a) inheritance. b) Polymorphism. c) Encapsulation d) Interface.

I think its an interface. I'm right, or. ans diff?

+2


source to share


4 answers


Yes, use the interface. An interface is basically a contract that says, "Hey, you need to implement these members, or I'm not going to compile."



+8


source


An interface or abstract class will do what you want. In an abstract class, if a is method

marked as abstract

, then it must be implemented in the derived class. The question really comes down to what you should be using. Interface or abstract class.



The quick answer (and I mean the quick and the dirty) is that you should use an interface when trying to set up contractual behavior between classes. You should use an abstract class when the collection of the derived class has some common behavior.

+4


source


The interface ensures that the class has method stubs for all methods, but they may not be implemented and may throw NotImplementedExceptions.

The best way to ensure that all methods are implemented is to use unit tests, where you check if the methods actually do what they want them to do.

+3


source


Actually, these concepts are not even compared with each other.

-1


source







All Articles