Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

44

Part II

HANDLING DATA

 

 

 

In this chapter, you will learn about the basic components of the C# language, such as classes, namespaces, structs, enumerations, and interfaces. You will also learn about the methods used with classes. Finally, you will learn to write, com-

pile, and execute a simple program in C# by using the Main() method.

Classes

The most important component of C# is a class. A class is defined as a data structure used to create objects. The instance of a class is called an object. An object contains data and has methods associated with it. The data and methods associated with a class are called the members of the class. A class is used to define the data contained in objects. However, classes do not contain data themselves. To use a class in a program, you first need to declare the class.

Declaring Classes

To declare a class, you use the class keyword. A class is declared using the class declaration statement as shown:

<modifiers> class <class name>

{

------------------

}

Here, modifiers specify the accessibility information about the class. A class modifier defines the scope of the class. C# supports several class modifiers, such as new,

public, private, protected, internal, sealed, and abstract. Take a look at Table

3-1 to learn more about class modifiers.

 

COMPONENTS OF C#

Chapter 3

45

Table 3-1 The Class Modifiers in C#

 

 

 

 

 

 

 

 

 

 

 

Class Modifier

Description

 

 

 

 

 

 

 

new

A new class modifier is used with nested classes. It is used to hide an

 

 

inherited class by the same name as the base class.

 

 

 

public

A public class modifier is used to define classes that can be accessed

 

 

from any program code.

 

 

 

private

A private class modifier is used to define classes that can be accessed

 

 

from a containing type. A private modifier is typically used with a

 

 

class that contains static methods.

 

 

 

protected

A protected class modifier is used to define classes that can be

 

 

accessed from a containing type or the types derived from the con-

 

 

taining types.

 

 

 

internal

An internal class modifier is used to define classes that can be

 

 

accessed from the current assembl y.

 

 

 

sealed

A sealed class modifier is used to prevent a class being derived from

 

 

a base class.

 

 

 

abstract

An abstract class modifier is used to define a base class of other

 

 

classes.However, you cannot create instances of an abstract class.

 

 

An abstract class supports inheritance.However, if you inherit

 

 

from an abstract class, you need to implement all its abstract methods.

 

 

An abstract class cannot be a sealed class, as you cannot derive a

 

 

sealed class.

 

 

 

 

 

 

 

 

After declaring a class, you can use it in a C# project. C# allows you to use the class that you define in other applications.In addition, you can derive a class from an existing class. This concept is called inheritance. Inheritance will now be discussed in detail.

Inheritance

The concept of inheritance is familiar to C and C++ programmers. Inheritance allows a class to be derived from another class known as the base class. The class

46

Part II

HANDLING DATA

 

 

 

that you derive is called the derived class. Inheritance allows you to reuse the data and methods of a class by the derived class. However, the constructors and destructors of the base class are not implicitly inherited. You will learn about the methods, constructors, and destructors used with classes later in this chapter.

In C#, you cannot derive a class from multiple classes. This is known as single inheritance. However, you can derive any number of classes from a single class. In addition, a base class of other classes can, in turn, be derived from another class. In C#, the Object class is the base class of all classes. The Object class lies in the System namespace. Here is the syntax of inheriting a derived class from a base class.

class <derived class> : <base class>

In the previous code, a colon (:) is used to indicate that a class is derived from an existing class. For example,

class Class1 : Class2

Here, Class2 is the base class of Class1, and therefore, Class1 inherits all members of Class2.

The following example will help you to understand the concept of inheritance.

class Employee

{

public void EmployeeName()

{

-------------

}

}

class Salary : Employee

{

public void CalculateSalary()

{

-----------

}

}

COMPONENTS OF C#

Chapter 3

47

 

 

 

class Bonus

{

static void Main()

{

Salary salary1 = new Salary; salary1.EmployeeName(); salary1.CalculateSalary();

}

}

This code declares two classes, Employee and Salary. The Salary class is inherited from the Employee class and, therefore, inherits the method declared by the base class. An instance of the derived class is created to access the members of the base class.

As discussed earlier, the derived class implicitly inherits all the members of the base class. However, you can hide any method of the base class so that the derived class cannot access the method. To do so, you declare another method by a signature that is same as that of the base class method. When a compiler finds such a method, it generates a warning. To suppress this warning, you use the new keyword. This makes the base class method inaccessible to the derived class.

In the above case, if you need to declare a method with the name EmployeeName() in the Salary class, you need to include the new keyword in the method declaration statement. The next example uses the new keyword.

class Salary : Employee

{

new public void EmployeeName()

{

-----------

}

}

Inheritance allows a derived class to inherit the methods of the base class. However, the constructors and destructors of the base class are not implicitly inherited. The next section looks at the constructors and destructors in detail.