blogs

miércoles, 10 de junio de 2015

Inheritance

Inheritance: Define one new type of data in terms of an existing.

Use the protected access modifier to make available certain members only derived classes.
For example:
protected string Color;

To invoke methods of the base class since a derived class we can use the base keyword how a variable of instance.

For example:
base.Color();

Abstract
Class: An abstract class doesn't be instanced
Method: When we define a method as abstract, we have to put the signature method and the children have to override it. The abstract class forces to overwrite the method. otherwise the class will be abstract.

Virtual keyword:
When a base class has virtual keyword in its signature method, the base class can implement its own behavior and so are children can implement their own behaviors, this is call polymorphism.

Base class gives permission so that the derived classes can overwrite the method only if it is necessary.
When a class has virtual keyword, this is the first implementation.

Sealed keyword in the following: 
Class: In a class specified that it can not do inheritance so that this class will not have children.
Method: In the methods is similar, when a method has a sealed keyword, this can't be overwrite by children.


Create class library, class, unit test and add reference:








Abstract class


Create a test method to test the constructor method of the base class




 Create the derived class


Call to the  constructor method of the base class



 This is the Car class


Create a test method to test the constructor methods of the derived class









Abstract Method

Create a abstract method in the base class with implementation 




The derived class must implement the abstract method of the base class, we must overwrite the method



We can not implement a abstract method in the base class




We resolve the problem put only the method signature in the base class.



We create a test method to test the Name Method



When we don't want overwrite the abstract method in the derived class , we must put to the class how abstract






Virtual Method

Create the base class with virtual methods



In the derived class we will overwrite one of the methods.





Create a test method to test the two virtual methods.




Execution the test with debug. On  line 33 execute the method of the derived class.





On line 34 of test method, execute the method of the base class




Sealed Class: When applied to a class, the sealed modifier prevents other classes from inheriting from it.

Sealed Method: We can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. This enable us to allow classes to derive from our class and prevent them from overwrite specific virtual methods or properties.

Sealed Class

Add sealed keyword to a class



Create a new class that derived of the base class, in this case the class car. The error is can not be inherit  of sealed class.



Sealed method

Add to method sealed keyword 



 If we overwrite the method, we will see as error because the method was sealed and a derived class can not overwrite it. 




No hay comentarios:

Publicar un comentario