Skip to main content

Posts

Showing posts with the label inheritance

Do you choose inheritance or interfaces?

What's the right answer? The answer to every programming question Let's explore both options and when you might choose to prefer one or the other. Pay extra special attention and focus because this is a common job question and an answer you should fundamentally know if you want to become a better software developer! Why choose interfaces? Choose inheritance when your objects have a has-a or can-do relationship. What does this mean? A has-a relationship means the object implementing the interface "has a" object/thing that is the interface. Here is a concrete example of a has-a relationship: public interface IWheel { public void TurnRight ( ) ; public void TurnLeft ( ) ; } public class Boat : IWheel { public void TurnRight ( ) { // implementation } public void TurnLeft ( ) { // implementation } } A boat has a wheel, and is an example of using an interface with a has-a relatio...