Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views13 pages

Inheritance Notes

The document explains the concept of inheritance in programming, detailing its types such as single, multilevel, multiple (using interfaces), and hierarchical inheritance. It includes code examples in C# demonstrating how these inheritance types work, along with their outputs. The document emphasizes reusability of code through inheritance by allowing derived classes to utilize properties and methods from base classes.

Uploaded by

badesrushti04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

Inheritance Notes

The document explains the concept of inheritance in programming, detailing its types such as single, multilevel, multiple (using interfaces), and hierarchical inheritance. It includes code examples in C# demonstrating how these inheritance types work, along with their outputs. The document emphasizes reusability of code through inheritance by allowing derived classes to utilize properties and methods from base classes.

Uploaded by

badesrushti04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Inheritance:

1. Inheritance Acquiring(taking) the properties of one class into another class is called
inheritance.
2. Inheritance provides reusability by allowing us to extend an exisiting class.

Syntax:

[Access modifier] class ClassName: baseclassname

Base Class

Derived Class

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApp1

class A

int a = 100, b = 200, c;


public void Add()

c = a + b;

Console.WriteLine("Addition of two number is " +c);

public void Sub()

c = a - b;

Console.WriteLine("Subtraction of two number is " + c);

class B

static void Main(string[] args)

A r = new A();

r.Add();

r.Sub();

Console.ReadLine();

OUTPUT:

Addition of two number is 300


Subtraction of two number is -100

A] Single Inheritance: when a single derived class is created from a single base class then the
inheritance is called as single inheritance.

Class A

Class B

Example:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApp1

class A

protected int a = 100, b = 200, c;


public void Add()

c = a + b;

Console.WriteLine("Addition of two number is " +c);

public void Sub()

c = a - b;

Console.WriteLine("Subtraction of two number is " + c);

class B:A

public void Mul()

c = a*b;

Console.WriteLine("Multiplication of two number is " +c);

public void Div()

c = a/b;

Console.WriteLine("Division of two number is " + c);

}
static void Main(string[] args)

B r = new B();

r.Add();

r.Sub();

r.Mul();

r.Div();

Console.ReadLine();

OUTPUT:

Addition of two number is 300

Subtraction of two number is -100

Multiplication of two number is 20000

Division of two number is 0

B] Multilevel Inheritance:
C] Multiple Inheritance(Using Interface):

Syntax:

Interface A

{
}

Interface B

{
}

Class C: A,B

Example:

using System;

namespace ConsoleApp1

interface Addition

int add(int a, int b);

interface Subtraction

{
int sub(int x, int y);

class calculation: Addition, Subtraction

public int result1;

public int add(int a, int b)

return result1 = a + b;

public int result2;

public int sub(int x, int y)

return result2 = x - y;

class Program

static void Main(string[] args)

calculation c = new calculation();

c.add(2, 4);

c.sub(3,5);

Console.WriteLine( " Multiple Inheritance using Interface ");


Console.WriteLine("Addition of two number" +c.result1);

Console.WriteLine("Subtraction of two number" +c.result2);

Console.ReadLine();

Output:

Multiple Inheritance using Interface

Addition of two number 6

Subtraction of two number -2


C] Hierarchical Inheritance :

Syntax:

Class A(Base class)

Class B : A

Class C : A

Class D : A

Example:

using System;

namespace ConsoleApp1

class calculation

public int a, b;
public void getdata()

Console.WriteLine("Enter the value of a and b");

a = Convert.ToInt32(Console.ReadLine());

b = Convert.ToInt32(Console.ReadLine());

class addition: calculation

int c;

public void add()

c = a + b;

Console.WriteLine("Addition of two number is: "+c);

class Subtraction : calculation

int d;

public void sub()

d = a - b;

Console.WriteLine("Subtraction of two number is: " + d);

}
}

class Multiplication : calculation

int e;

public void mul()

e = a * b;

Console.WriteLine("Multiplication of two number is: " + e);

class Division : calculation

int f;

public void div()

f= a /b;

Console.WriteLine("Division of two number is: " + f);

class Program

static void Main(string[] args)


{

addition a = new addition();

a.getdata();

a.add();

Subtraction s= new Subtraction();

s.getdata();

s.sub();

Console.ReadLine();

OUTPUT:

Enter the value of a and b

10

20

Addition of two number is: 30

Enter the value of a and b

15

23

Subtraction of two number is: -8

You might also like