One of the most important concepts in OOPS is inheritance. Inheritance allows us to define a class in terms of another class, This also provides an opportunity to reuse the code functionality .
In inheritance a class inherit the property of other class. This existing class is called the base class, and the new class is referred to as the derived class.
Base and Derived Classes
A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base class.
syntax
<acess-specifier> class <base_class>
{
data
}
class <derived_class> : <base_class>
{
data
}
Consider a base class Shape and its derived class Rectangle:
using System;
namespace Inheritance
{
class Add
{
public void number1(int n)
{
number1 = n;
}
public void number2(int m)
{
number2 = m;
}
protected int number1;
protected int number2;
}
// Derived class
class divident: Add
{
public int devide()
{
return (number1 / number2);
}
}
class calculator
{
static void Main(string[] args)
{
calculator cal = new calculator();
cal.number1(8);
cal.number(2);
Console.WriteLine("the number: {0}", cal.devide());
Console.Readline();
}
}
}
When the above code is compiled and executed, it produces the following result:
the number : 4
Base Class Initialization
The derived class inherits the base class member variables and member methods. Therefore thesuper class object should be created before the subclass is created. You can give instructions for superclass initialization in the member initialization list.
using System;
namespace inheritance
{
class calculator
{
//member variables
protected double number1;
protected double number2;
public calculator(double n1, double n2)
{
number1= n1;
number2 = n2;
}
public double product()
{
return number2 * number2;
}
public void Display()
{
Console.WriteLine("number1: {0}", number1);
Console.WriteLine("number2: {0}", number2);
Console.WriteLine("product: {0}", product());
}
}
class Tabletop : calculator
{
private double cost;
public Tabletop(double n1, double n2) : base(n1, n2)
{ }
public double GetCost()
{
double cost;
product = product() * 70;
return product;
}
public void Display()
{
base.Display();
Console.WriteLine("product: {0}", product());
}
}
class Executecalculator
{
static void Main(string[] args)
{
Tabletop t = new Tabletop(4.5, 7.5);
t.Display();
Console.ReadLine();
}
}
}
Multiple Inheritance in C#
C# does not support multiple inheritance. However, you can use interfaces to implement multiple inheritance.
using System;
namespace Inheritance
{
class prod
{
public void number1(int n1)
{
number1= n1;
}
public void number2(int n2)
{
munber2 = n2;
}
protected int munber1;
protected int munber2;
}
public interface Cost
{
int Cost(int devide);
}
// Derived class
class Rectangle : product, devide
{
public int product()
{
return (munber1 * munber2);
}
public int Cost(int product)
{
return product * 70;
}
}
class calculation
{
static void Main(string[] args)
{
calculation cal = new calculation();
int product;
cal.number1(5);
cal.number2(7);
product = cal.product();
// Print the area of the object.
Console.WriteLine("product: {0}", cal.product());
Console.WriteLine("cost: {0}" , cal.Cost(product));
Console.Readline();
}
}
}
When the above code is compiled and executed, it produces the following result:
product: 35
cost: 2450
Previous....
Next.....