Class Inheritance

Inheritance allows for the creation of new classes(derived classes) which reuse, extend, and alter the behavior of another class( the base class).

Derived classes can only have a single base class, but inheritance allows a chain of inheritance in which one class draws from two through its base class being a derived class.

A class inherits from another through the following syntax for its declaration:

public class NewClass : BaseClass
{
}

Review an example of its use below:

class Burger
{
	string ON, CH, TO, LT, brgr;
	public void BuildBurger()
	{
		Console.WriteLine(“What do you want on your burger?”);
		brgr = (Console.ReadLine());
		Console.WriteLine(“You want a burger with{0}”+brgr);
	}
}
class Salmon: Burger
{
	public void makeBurger()
	{
		BuildBurger();
	}
	static void Main(string[] args)
	{
		Burger salmon = new Burger();
		salmon.BuildBurger();
	}
}

Relationships can be categorized as inheritance, composition, utilization, and instantiation. Composition describes a relationship of ownership meaning a motorcycle has an engine. Utilization describes the motorcycle class using the rider class.

ABSTRACT CLASSES AND METHODS

When a base class declares an abstract member, any non-abstract class inheriting from the class directly must override the method. An abstract derived class inherits abstract members, but does not implement them.

The abstract keyword applied to a class prevents direct instantiation through the new keyword.

Inheritance is then required to use it. Abstract classes can hold one or more abstract method signatures with parameters and return value, but no actual method body; however, abstract classes can hold nonabstract classes. Any class with an abstract member must be declared abstract.

INTERFACES

The interface reference type resembles an abstract base class composed exclusively of abstract members. Classes implementing interfaces must provide implementations for all interface members. Classes can implement multiple interfaces, but only derive from a single base class. Interfaces define the relationship between classes lacking an “IS-A” relationship.

ACCESS

Derived classes can access public, protected, internal, and protected members of the base class. This access allows the derived class to use virtually any members, but it cannot access the members directly. It only uses the work they perform.

Applying the sealed keyword to a class or specific members of a class prevents inheritance.