Inheritance (Derived and Base Class)
In C#, it is possible to inherit fields and methods
from one class to another.
"inheritance concept" into two categories:
Derived Class (child) - the class that inherits
from another class
Base Class (parent) - the class being inherited
from
To inherit from a class, use the : symbol.
• Public Class base
• {
• }
• Class Child:Base
• {
• }
Inheritance
the ability to create classes which inherits certain
aspects from parent classes.
public class Animal
{
public void Greet()
{
Console.WriteLine("Hello, I'm some sort of animal!");
}}
public class Dog : Animal { }
• Inheritance is the process by which one object
can acquire the properties of another object.
• Inheritance is not only from one class to
another - you can have a whole hierarchy of
classes, which inherits from eachother.
Program
class TwoDShape {
public double Width;
public double Height;
public void ShowDim()
{ Console.WriteLine("Width and height are " +
Width + " and " + Height);
}}
• C# does not support the inheritance of
multiple base classes into a single derived
class.
• created a base class that defines the attributes
common to a set of objects, it can be used to
create any number of more specific derived
classes.
• a private member of a base class is not
accessible to a derived class.
• a derived class have access public member in
the base class.
• protected, we can create class members that
are private to their class but that can still be
inherited and accessed by a derived class.
using System;
class B {
protected int i, j; // private to B, but accessible by D
public void Set(int a, int b)
{ i = a; j = b; }
public void Show() {
Console.WriteLine(i + " " + j); }
}
class D : B {
int k; // private // D can access B's i and j
public void Setk()
{ k = i * j; }
public void Showk()
{ Console.WriteLine(k); }
}
The sealed Keyword
If you don't want other classes to inherit from a
class, use the sealed keyword:
If you try to access a sealed class, C# will
generate an error:
sealed class Vehicle { ... }
class Car : Vehicle { ... }
The error message will be something like this:
'Car': cannot derive from sealed type 'Vehicle'
Program
Constructors and Inheritance
• it is possible for both base classes and derived
classes to have their own constructors.
• C# uses base, which has two uses.
• The first use is to call a base class constructor.
The second is to access a member of the base
class that has been hidden by a member of a
derived class.
• derived-constructor(parameter-list) : base(arg-
list) { // body of constructor }
• base.member Here, member can be either a
method or an instance variable. T
Base Class References and Derived Objects
using System;
class X {
public int a;
public X(int i)
{ a = i; }
}
class Y : X {
public int b;
public Y(int i, int j) : base(j)
{ b = i; }
}
• class BaseRef {
• static void Main() {
• X x = new X(10);
• X x2;
• Y y = new Y(5, 6);
• x2 = x; // OK, both of same type
Console.WriteLine("x2.a: " + x2.a);
• x2 = y; // OK because Y is derived from X
Console.WriteLine("x2.a: " + x2.a);
• }}