Include an interface to a specific implementation object or vice versa?

In C #, when I have an interface and several specific implementations, can I apply the interface to a specific type or a specific type passed to the interface?

What are the rules in this case?

+28
c # oop
Feb 12 '09 at 0:01
source share
5 answers

Both directions are allowed in Java and C #. Downcasting requires an explicit cast and may throw an exception if the object does not match the correct type. However, to increase the rating there is no explicit actuation and is always safe.

That is, if you have a public interface Animal and two implementations of these interface , Cat and Dog ....

 Animal meowAnimal = new Cat(); // No cast required Animal barkAnimal = new Dog(); // No cast required Cat myCat = (Cat) meowAnimal; // Explicit cast needed Dog myDog = (Dog) barkAnimal; // Explicit cast needed Dog myPet = (Dog) meowAnimal; // Will compile but throws an Exception 

and you will need try / catch around explicit throws. In C #, you have a useful as keyword:

 Dog myDog = barkAnimal as Dog; Dog myPet = meowAnimal as Dog; 

No exception will be thrown, and myDog will not be null, and myPet will be null. Java does not have an equivalent keyword, although you can always use if (meowAnimal instanceof Dog) tests to maintain type safety. (I would suggest that the as keyword generates a bytecode that executes if, assigning zero to is . But maybe .NET has a bytecode instruction that executes the equivalent of as .)

+32
Feb 12 '09 at 0:33
source share

In most languages ​​you can use both directions. If you have a specific class, you can apply it to the interface. If you have an interface, you can apply it to a specific class.

As a rule, you want to go only in the first direction. The reason is that you should not know what a particular class is when you only have a pointer to an interface. If you are passing something as an interface, you should be able to do everything you need from that interface. If you need to use parts of a specific object rather than an interface, you have a design problem that needs to be fixed, not casting.

+13
Feb 12 '09 at 0:27
source share

If you are talking about Java (but the rules for other languages ​​are similar), it is something like this:

You can (down) apply the interface to a specific implementation if the link you drew is actually a link to a specific specific implementation. It means

 Vehicle v=new Car(); (Car)v // this is OK (Bus)v // this is not 

The error appears as a ClassCastException in Java.

You can fully implement a specific implementation of an interface for an interface.

+3
Feb 12 '09 at 0:09
source share

Both are valid, considering it logical. Many times, users of your interface do not have access to specific classes, which is one of the main reasons for having an interface in the first place.

0
Feb 12 '09 at 0:06
source share

An interface can be a whole class, if it shouldn’t be, it’s better when you create a constructor that takes an interface as a parameter and copies Settings, so you control what is happening and what is needed.

0
Dec 29 '16 at 19:50
source share



All Articles