Tuesday, February 16, 2016

Lesson 5

Outline


Chapter 11

Inheritance
Base Classes
Derived Classes

Inheritance

Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the public or internal members declared in ClassB and ClassA.

Each derived class is generally a specific version of the parent or base class. If we created a Vehicle class and then derived Car class from Vehicle class. The Car class would have access to all public or internal properties and methods of the Vehicle class.

public class Vehicle
{public int NumberOfDoors { get; set; }
public string Transmission { get; set; }
}
 
public class Car : Vehicle
{public bool Convertable { get; set; }
public bool HasTrunk { get; set; }
}
Class inheritance is denoted by a colon (:) separating the current class name from the base class name. public class Car : Vehicle tells us that Car is derived from Vehicle. Vehicle is the base class and Car is the derived class. Since the Car class is derived from Vehicle we can say that all Cars are Vehicles but not all Vehicles are Cars. Inheritance only works downstream and inheriting a base class does not change the base class. But any change to the base class will be reflected in the derived class. 

Car myCar = new Car();
myCar.Convertable = true;
myCar.NumberOfDoors = 2;Above we created an instance of the Car class and called the object myCar. We then changed the property values of myCar. If you look closely, we changed the convertible property value which is located in the Car class and then changed the property value of the NumberOfDoors property from the Vehicle class. The code looks exactly the same. The myCar class does not care which level a property or method is defined only whether it is available (public or internal) or not (private).

Multiple class can be derived
We can create multiple classes based on a parent class.
public class Car : Vehicle
{public bool Convertable { getset; }
public bool HasTrunk { getset; }
}
public class Truck : Vehicle
{public bool TowPackage { getset; }
public bool FourWheelDrive { getset; }
}
Both of the above classes are derived from the same class (Vehicle), Even though they contain different properties they have the same base class. It can be read like this "All cars are vehicles but not all vehicles are cars"

Base Classes

A base class is a class that is used to create, or derive, other classes. Classes derived from a base class are called child classes, subclasses or derived classes. A base class does not inherit from any other class and is considered parent of a derived class.

The base class forms the means by which inheritance is accomplished through derivation. A class derived from a base class inherits both data and behavior. For example, vehicle can be a base class from which the derived classes car and bus can be derived. Both car and bus are vehicles, and they each represent their own specializations of base class.

Base class helps to create a specialized class that can reuse the code that is implicitly gained from the base class (except constructors and destructors) and extend the functionality of base class by adding or overriding members relevant to derived class in derived class. In C#, events are declared in base class that can be raised from derived classes. Generic classes that are used to encapsulate operations that are not specific to a particular data type serve as base classes, providing generic behavior so as to achieve flexibility and code reusability.


Derived Classes

A derived class is a class created, or derived from another existing class. The existing class from which the derived class gets created through inheritance is known as base or super class.

While inheriting from base class, the derived class implicitly inherits all the members (except constructors and destructors) which it reuses, extends and modifies the behavior of the base class. The derived class overrides the properties and methods of the base class so that it represents the specialized version of base class. C# provides ability to override and hide methods of base class in derived class which makes both classes to evolve freely and maintain binary compatibility.

Inheritance is one of the key functionalities required for all object oriented programming languages.


No comments:

Post a Comment