الأحد، 14 ديسمبر 2008

What Is Inheritance? What Is an Interface?

What Is Inheritance?

¦     Goal: For reusability of code.

¦     Use inheritance to create new classes from existing ones.

¦     Between to 2 classes not 2 objects.

¦     Is a relationship

      Example: Manger is an employee (Manger extends from Employee)

¦     Has a relationship: it means that class takes an object from another class.

¦     For example, the Bitmap class inherits from the Image class and extends it by adding functionality.

¦     General Shape to inheritance: Class Super: Sub { //do Something    } 

¦     You can easily create a custom exception class by inheriting from System.ApplicationException, as shown here:

 

class DerivedException : System.ApplicationException

{

public override string Message

{

get { return "An error occurred in the application."; }

}

}                      

// C#  (In Main Class)

try

{

throw new DerivedException();

}

catch (DerivedException ex)

{

Console.WriteLine("Source: {0}, Error: {1}", ex.Source, ex.Message);

}

Output:

Source: UserExceptions, Error: An error occurred in the application.

We can display result in file by add this code:

catch (DerivedException ex)

            {

               // Console.WriteLine("Source: {0}, Error: {1}", ex.Source, ex.Message);

                StreamWriter sw = new StreamWriter(@"location\fileName");

                sw.WriteLine("Source: {0}, Error: {1}", ex.Source, ex.Message);

                sw.Close( );

            }

What Is an Interface?

ü      Interfaces, also known as contracts (عقود اى يجب الالتزام بما بها).

ü      Define a common set of members that all classes that implement the interface must provide.

ü      For example, the IComparable interface defines the Compare To method, which enables two instances of a class to be compared for equality.

ü      For example, the IDisposable is an interface that provides a single method, Dispose, to enable assemblies that create an instance of your class to free up any resources the instance has consumed.

ü      You can create your own interfaces, see the following example:

Þ    You should note that interface is public by default.

Þ    In the case of inheritance the interface used as the follow:

       class Myclass:YourClass,ImyInterface { //do something}

interface IMessage

{

//Method: Send the message. Returns True is success, False otherwise.

bool Send( );

// The message to send.

string Message { get; set; }

// The Address to send to.

string Address { get; set; }

}

 

class EmailMessage : IMessage

    {

        public bool Send()

        {

            throw new Exception("The method or operation is not implemented.");

        }

        public string Message

        {

            get

            {

                throw new Exception("The method or operation is not implemented.");

            }

            set

            {

                throw new Exception("The method or operation is not implemented.");

            }

        }

        public string Address

        {

            get

            {

                throw new Exception("The method or operation is not implemented.");

            }

            set

            {

                throw new Exception("The method or operation is not implemented.");

            }

        }  

  }

What Are Partial Classes?

¦     Partial classes are new in .NET 2.0.

¦     Partial classes allow you to split a class definition across multiple source files.

¦     The Windows Form class is an example of a built-in partial class.

¦     Now that code is hidden in a partial class named form.Designer.cs.

¦     Note: Partial classes aren’t part of the exam objectives

0 التعليقات: