Adv C#
Programming

1. Introduction to .NET and C# 2. Points to keep in mind for pyramid 3. C# Pyramid Program Example for type 1 4. C# Pyramid Program Example for type 2 5. C# Pyramid Programs on Type 3 6. C# Pyramid Programs of type 4 7. C# Pyramid Programs of type 5 8. Classes and Objects 9. Recursive functions 10. Constructors in C# 11. Destructor 12. Method Overloading 13. Constructor Overloading 14. Inheritance in C# 15. Interface in C# 16. Method Overriding 17. readonly and sealed keyword in C# 18. Exception Handling 19. Delegates 20. Multicast Delegates 21. Events 22. Structure 23. Threads 24. Working with Threads 25. Thread Life Cycle 26. Introducing Multithreading 27. Advantage and limitation of multithreading 28. Thread priority 29. Properties 30. Collections 31. ArrayList Class 32. Hastable Class 33. SortedList 34. Stack 35. Queue Class 36. Generics 37. Unsafe code 38. C# Networking 39. Prgs for Science and Engineer 40. Prg to use alias 41. Prg to count minors and majors using array 42. Prg for sum up of two array in third array 43. Prg to show boxing and unboxing 44. Prg for type casting from byte to int 45. Prg to find area of circle 46. Prg to find cube root of entered number 47. Prg for digit manipulation 48. Prg for printing a reverse triangle 49. Prg to show case and goto fall through 50. Prg to show use of foreach loop 51. Prg for adding 2 numbers 52. Prg to show jagged array 53. Prg to show different methods of Array 54. Prg to print number Pyramid 55. Prg to show entered string is palindrome or not 56. Prg to show entered number is prime or not 57. Prg to show different methods of StringBuilder 58. Prg to show different methods of String 59. Prg for appending a string using StringBuilder 60. Prg to show appending a string with numeric type data using StringBuilder 61. Prg to show use of ArrayList 62. Prg to pass an array as a parameter to a function for finding out a prime number 63. Prg to show the constant member 64. Prg for showing destructor 65. Prg for sum up and reversing the entered number 66. Prg for enum type conversation 67. Prg to show sum of first fifty even and odd number 68. Prg to find the factorial of entered number 69. Prg for iterating each element of array using foreach loop 70. Prg to show array position and value 71. Prg to show how to insert and remove string using StringBuilder 72. Prg to show how to join the string 73. Prg to show the value of sin 90 74. Prg to enter two number and find max 75. Prg to show padding 76. Prg to show entered number is prime or not 77. Prg to show private constructor 78. Prg to show how to use getter and setter property 79. Prg using out parameter 80. Prg to count the number of spaces and words in a String 81. Prg to show how to use Split method 82. Prg to create binary triangle 83. Prg to create dule star triangle 84. Prg to create triangle 85. Prg to show use of EndsWith and StartsWith method of string 86. Prg for static constructor 87. Prg to show how to use insert method 88. Prg to sort a string 89. Prg to store student name and roll no using structure 90. Prg to show how to use delegate 91. Prg to calculate depreciation 92. Prg for calculating Economic order cost and time between order 93. Prg to find radius of circle 94. Prg to sum up odd numbers from 0 to 20 95. Prg to calculate simple interest 96. Prg to categorise student on base of there marks range where marks are stored in array 97. Prg to create triangle on console screen 98. Prg to print sin theta, cos theta and tan theta from 0.1 to 1.0 angle in radians 99. Prg to demonstrate variables 100. Prg to illustrate the differences between int and double 101. Prg to show exception handling using try.. catch block 102. Prg to demonstrate how to use interface 103. Prg to print number triangle 104. Prg to demonstrate structure 105. Prg to test exception 106. Prg to count number of spaces and words in a entered string 107. Prg to demonstrate ref parameter 108. Prg to print a matrix table from 1 to 10 109. Prg to display 2 D array elements position in matrix form 110. Prg to count number of adult and children 111. Prg to print pascal triangle 112. Prg to demonstrate stringbuilder and its method 113. Prg to reverse a string 114. Prg for string sorting 115. Interview Question & Answer part 1 116. Interview Question & Answer part 2 117. Interview Question & Answer part 3 118. Interview Question & Answer part 4 119. Interview Question & Answer part 5 120. Interview Question & Answer part 6 121. Interview Question & Answer part 7 122. Interview Question & Answer part 8 123. Interview Question & Answer part 9 124. Interview Question & Answer part 10 125. Interview Question & Answer part 11 126. Interview Question & Answer part 12 127. Interview Question & Answer part 13 128. Interview Question & Answer part 14 129. Interview Question & Answer part 15
Pr.Pg Next Pg

Inheritance in C# tutorials

  • In the Csharp language, classes can be derived from other classes, thereby inheriting fields and methods from those classes.

  • Inheritance is a compile-time mechanism in Csharp that allows you to extend a class called the base class or superclass with another class called the derived class or subclass.

  • In Csharp, inheritance is used for two purposes:

    1. Class Inheritance

    2. Interface Inheritance

  1. Class Inheritance: Create a new class as an extension of another class, primarily for the purpose of code reuse. That is, the derived class inherits the public methods and public data of the base class. Csharp only allows a class to have one immediate base class, i.e., single class inheritance.

  2. Interface Inheritance: Create a new class to implement the methods defined as part of an interface for the purpose of subtyping. That is a class that implements an interface “conforms to” or is constrained by the type of the interface. Csharp supports multiple interface inheritance.

 

Note: For extending a class and for implementing an interface C# uses ( : ) colon operator.

 

Single inheritance

  • When one class is derived from one base class it is called as single inheritance.

  • Csharp supports single inheritance.

CbtSAM Single Ingeritance

Syntax for class inheritance:

<modifier> class <class_name> : <base_class_name>

{

//Derived class methods

}

 

Syntax of interface inheritance:

<modifier> class <class_name> : <interface_name>

{

//Class provides the implementation for the methods which are declared in interface

}


 

Example: Csharp program for demonstrating inheritance

Sam Madam 

// Csharp program for demonstrating inheritance

using System;

public class A //superclass

{

public int num1; //member of superclass

public int num2; //member of superclass

public void setVal(int no1, int no2) //method of superclass

{

num1 = no1;

num2 = no2;

}

}

public class B : A //subclass B

{

public int multi; //member of subclass

public void mul() //method of subclass

{

multi = num1*num2; //accessing member of superclass from subclass

}

}

public class Inheritance

{

public static void Main(String[] args)

{

B objb = new B();

objb.setVal(10, 6); //calling superclass method throgh subclass object

objb.mul();

Console.WriteLine("Multiplication is " + objb.multi);

}

}

 

Output

Multiplication is 60

 


 

Example: // Csharp program for demonstrating inheritance

Sam Madam 

// Csharp program for demonstrating inheritance

using System;

public class AA //superclass

{

public int num1; //member of superclass

public int num2; //member of superclass

public void setVal(int no1, int no2) //method of superclass

{

num1 = no1;

num2 = no2;

}

}

public class BB :AA //subclass B

{

public int multi,div,add,sub; //member of subclass

public void mul() //method of subclass

{

multi = num1*num2; //accessing member of superclass from subclass

}

public void division()

{

div=num1/num2; //accessing member of superclass from subclass

}

public void addition()

{

add=num1+num2; //accessing member of superclass from subclass

}

public void subtraction()

{

sub=num1-num2; //accessing member of superclass from subclass

}

}

public class InheritanceDemo

{

public static void Main(String[] args)

{

BB objb = new BB();

int i, j;

Console.Write("Enter number1 : ");

i = Int32.Parse(Console.ReadLine()); //asking user defined number1

Console.Write("Enter number2 : ");

j = Int32.Parse(Console.ReadLine()); //asking user defined number2

objb.setVal(i, j); //calling superclass method throgh subclass object

objb.mul();

objb.addition();

objb.subtraction();

objb.division();

Console.WriteLine("Addition is :" + objb.add);

Console.WriteLine("Subtraction is :" + objb.sub);

Console.WriteLine("Multiplication is :" + objb.multi);

Console.WriteLine("Division is :" + objb.div);

}

}

 

Output

Enter number1 : 4

Enter number2 : 4

Addition is :8

Subtraction is :0

Multiplication is :16

Division is :1

 

Multilevel Inheritance

  • Class can derive from other class.

  • Class can be derived from other classes as well.

  • When we derive classes from derived classes we get levels of inheritances which is called as multilevel inheritance.

CbtSAM Multilevel Inheritance

 

Example: Csharp program to create three classes and perform multilevel inheritance among them

Sam Madam 

// Csharp program to create three classes and perform multilevel inheritance among them

using System;

public class student

{

public string name;

public int rno;

public void input()

{

Console.Write("Enter the name of the student : ");

name = Console.ReadLine();

Console.Write("Enter the roll no of the student : ");

rno = Int32.Parse(Console.ReadLine());

}

public void displays()

{

Console.WriteLine("Name of the student : " + name);

Console.WriteLine("Roll no : " + rno);

}

}

 

 

public class degree : student

{

public string deg;

public void getdegree()

{

Console.Write("Enter the degree in which the student studied : ");

 

deg = Console.ReadLine();

}

public void displayd()

{

Console.WriteLine("Degree in which the student studied : " + deg);

}

}

 

public class post_deg : degree

{

string degr;

public void getpdegree()

{

Console.Write("Enter the post graduation degree in which the student studies : ");

degr = Console.ReadLine();

}

public void displayp()

{

Console.WriteLine("Post graduation Degree in which the student studies : " + degr);

}

}

 

 

public class MultiLeve

{

public static void Main(String[] args)

{

post_deg p1 = new post_deg();

p1.input();

p1.getdegree();

p1.getpdegree();

p1.displays();

p1.displayd();

p1.displayp();

 

}

}

 

Output

Enter the name of the student : Jack

Enter the roll no of the student : 123

Enter the degree in which the student studied : BS

Enter the post graduation degree in which the student studies : MS

Name of the student : Jack

Roll no : 123

Degree in which the student studied : BS

Post graduation Degree in which the student studies : MS

Pr.Pg border                                              Next Pg