Tuesday, March 29, 2016

Lesson 11

Chapter 12 
 
Concepts:  
Interface classes
Multiple interfaces 
Interface vs Abstraction 
 
 
Interfaces
Interfaces are contracts that define the minimum set of class capabilities. An interface looks like a class, but has no implementation. The only thing it contains are declarations of events, indexers, methods and/or properties. The reason interfaces only provide declarations is because they are inherited by classes, which must provide an implementation for each interface member declared.
 
 

     interface iVehicle
    {
        int Doors { get; set; }

        string Style { get; set; }

        void SetSpeed();
    }

 
The interface class above "iVehicle" outlines what a class that implements the interface MUST support. This is the contract.
 
    class Car : iVehicle
    {
        public int Doors
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public string Style
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
 
        public void SetSpeed()
        {
            throw new NotImplementedException();
        }
    }
 
The Car class must satisfy the requirements of the interface but is not limited to only those requirements. The Car class could implement other methods or properties not outlined in the interface. Note: When visual studio implements an interface for you it adds a NotimplementedException() exception to each code snippet it created. This is useful in preventing us from access a generated method or property before we are ready.
 
Multiple Interfaces
 
Unlike inheritance a class can implement more that one interface.
 
Lets' add an engine interface to the solution.
 
    interface iEngine
    {
        int Cylinders { get; set; }
        string FuelType { get; set; }
    }
 
We can then add this interface to the Car class by adding the iEngine interface to the car class the class must now support the engine requirements in addition to the vehicle interface.
 
    class Car : iVehicle, iEngine
    {
        public int Doors
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public string Style
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public void SetSpeed()
        {
            throw new NotImplementedException();
        }
        public int Cylinders
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public string FuelType
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }

Interfaces vs Abstraction
 
A class can only inherit from 1 abstract class at a time. A class can support (implement) many interfaces.
An abstract class can contain code as well as code requirements. An interface cannot contain any implementation
An abstract class can contain private variables and methods. An interface only contains public code. 
 
 
 
 
 

No comments:

Post a Comment