![]() Let us discuss those rules one by one with Examples. While working with Interface we need to remember some Rules. Here, we have created one interface with the name ITestInterface by using the interface keyword. For a better understanding, please have look at the below diagram.įor a better understanding please have a look at the below example. Moreover, in an interface, we can only declare abstract members. In a class declaration, we need to use the class keyword whereas in an interface declaration we need to use the interface keyword. The way we define a class, in the same way, we need to define an interface. Note: A class can inherit from a class and interface(s) at a time. On the other hand, if a class is inheriting from an interface, it is to implement the members of its Parent (Interface), not for consumption. Generally, a class inherits from another class to consume members of its Parent. What restrictions? The restrictions are to implement each and every method of the interface under the child class. Simply speaking, the Parent Interface imposes restrictions on the Child Classes. For a better understanding, please have a look at the below diagram. And if a class has an interface as a Parent, the class is responsible for providing the implementation for all the abstract methods of the interface. Now, just like a class is having another class as a Parent, a class can also have an Interface as a Parent. So, this is what we learned in Inheritance. And once the Parent/Child relationship is established, then the members of class A can be consumed under class B. And there is a relationship called Parent/Child relation between them. Here, we have class A with a set of members and class B is inheriting from class A. In Inheritance, we already learned that a class inherits from another class, and the child class consumes the members of the Parent class. Note: Every abstract method of an interface should be implemented by the child class of the interface without fail (Mandatory). Please read thisarticle, if you want to learn these things in detail. What are abstract methods, why do we need abstract methods, and how do implement abstract methods we have already discussed this in our Abstract Classes and Abstract Methods article.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |