Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
57
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

Chapter 58: Inheritance

Section 58.1: Inheritance. Constructors' calls sequence

Consider we have a class Animal which has a child class Dog

class Animal

{

public Animal()

{

Console.WriteLine("In Animal's constructor");

}

}

class Dog : Animal

{

public Dog()

{

Console.WriteLine("In Dog's constructor");

}

}

By default every class implicitly inherits the Object class.

This is same as the above code.

class Animal : Object

{

public Animal()

{

Console.WriteLine("In Animal's constructor");

}

}

When creating an instance of Dog class, the base classes's default constructor (without parameters) will be called if there is no explicit call to another constructor in the parent class. In our case, first will be called

Object's constructor, then Animal's and at the end Dog's constructor.

public class Program

{

public static void Main()

{

Dog dog = new Dog();

}

}

Output will be

In Animal's constructor

In Dog's constructor

View Demo

Call parent's constructor explicitly.

GoalKicker.com – C# Notes for Professionals

295

In the above examples, our Dog class constructor calls the default constructor of the Animal class. If you want, you can specify which constructor should be called: it is possible to call any constructor which is defined in the parent class.

Consider we have these two classes.

class Animal

{

protected string name;

public Animal()

{

Console.WriteLine("Animal's default constructor");

}

public Animal(string name)

{

this.name = name;

Console.WriteLine("Animal's constructor with 1 parameter"); Console.WriteLine(this.name);

}

}

class Dog : Animal

{

public Dog() : base()

{

Console.WriteLine("Dog's default constructor");

}

public Dog(string name) : base(name)

{

Console.WriteLine("Dog's constructor with 1 parameter"); Console.WriteLine(this.name);

}

}

What is going here?

We have 2 constructors in each class.

What does base mean?

base is a reference to the parent class. In our case, when we create an instance of Dog class like this

Dog dog = new Dog();

The runtime first calls the Dog(), which is the parameterless constructor. But its body doesn't work immediately. After the parentheses of the constructor we have a such call: base(), which means that when we call the default Dog constructor, it will in turn call the parent's default constructor. After the parent's constructor runs, it will return and then, finally, run the Dog() constructor body.

So output will be like this:

Animal's default constructor

Dog's default constructor

GoalKicker.com – C# Notes for Professionals

296

View Demo

Now what if we call the Dog's constructor with a parameter?

Dog dog = new Dog("Rex");

You know that members in the parent class which are not private are inherited by the child class, meaning that Dog will also have the name field.

In this case we passed an argument to our constructor. It in his turn passes the argument to the parent class' constructor with a parameter, which initializes the name field.

Output will be

Animal's constructor with 1 parameter

Rex

Dog's constructor with 1 parameter

Rex

Summary:

Every object creation starts from the base class. In the inheritance, the classes which are in the hierarchy are chained. As all classes derive from Object, the first constructor to be called when any object is created is the Object class constructor; Then the next constructor in the chain is called and only after all of them are called the object is created

base keyword

1.The base keyword is used to access members of the base class from within a derived class:

2.Call a method on the base class that has been overridden by another method. Specify which base-class constructor should be called when creating instances of the derived class.

Section 58.2: Inheriting from a base class

To avoid duplicating code, define common methods and attributes in a general class as a base:

public class Animal

{

public string Name { get; set; }

// Methods and attributes common to all animals public void Eat(Object dinner)

{

// ...

}

public void Stare()

{

// ...

}

public void Roll()

{

// ...

}

}

Now that you have a class that represents Animal in general, you can define a class that describes the peculiarities of specific animals:

GoalKicker.com – C# Notes for Professionals

297

public class Cat : Animal

{

public Cat()

{

Name = "Cat";

}

// Methods for scratching furniture and ignoring owner public void Scratch(Object furniture)

{

// ...

}

}

The Cat class gets access to not only the methods described in its definition explicitly, but also all the methods defined in the general Animal base class. Any Animal (whether or not it was a Cat) could Eat, Stare, or Roll. An Animal would not be able to Scratch, however, unless it was also a Cat. You could then define other classes describing other animals. (Such as Gopher with a method for destroying flower gardens and Sloth with no extra methods at all.)

Section 58.3: Inheriting from a class and implementing an interface

public class Animal

{

public string Name { get; set; }

}

public interface INoiseMaker

{

string MakeNoise();

}

//Note that in C#, the base class name must come before the interface names public class Cat : Animal, INoiseMaker

{

public Cat()

{

Name = "Cat";

}

public string MakeNoise()

{

return "Nyan";

}

}

Section 58.4: Inheriting from a class and implementing multiple interfaces

public class LivingBeing

{

string Name { get; set; }

}

public interface IAnimal

{

bool HasHair { get; set; }

}

GoalKicker.com – C# Notes for Professionals

298