Exemplo n.º 1
0
        private static void Chapter81()
        {
            // 8.1.1. Polymorphism
            // As we saw on Tuesday, Polymorphism also includes casting.
            HouseCat suki = new HouseCat("Suki", 12);

            // Note: This takes in a CAT, it could be ANY class that extends cat (HouseCat, Tiger, etc)
            CatSitter annie = new CatSitter(suki);
        }
Exemplo n.º 2
0
        private static void Chapter82()
        {
            // 8.2. Interfaces
            // An interface is similar to an abstract class, with some important differences.
            // Interfaces allow us to create code organized by behavior, rather than static data.

            // Interfaces can contain
            // 1) Constants
            // 2) Method signatures
            // 3) Static methods
            // 4) Default methods

            // Refer to the IFeedable interface

            HouseCat  suki  = new HouseCat("Suki", 12);
            CatSitter annie = new CatSitter(suki);

            annie.FeedThePet();

            // Benefits of interfaces
            // You can only extend one class, but you may implement many interfaces.
            // You can extend a class and implement an interface at the same time.
            // When you declare properties and return types to be interface types, you decouple code using your classes from the actual class types you use.
        }