Children inheriting the property of there parents is known as inheritance which gives reusability we can implement the same rule in our object oriented languages and established parent child relationship between classes so that parent class members can be consumed by child classes i.e child classes are reusing the parent class members without rewriting them again.
A parent class can have any number of child classes and members of parent class can be consumed under all the child classes.
Private members of a class cannot be inherited and acquired by the child classes.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharpConsole { class Class1 { public Class1() { Console.WriteLine("Class1 constructor called"); } public void test1() { Console.WriteLine("First Method"); } public void test2() { Console.WriteLine("Second Method"); } } }// Now add another class Class2.cs and write the following code. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharpConsole { class Class2:Class1 { public Class2() { Console.WriteLine("Class2 constructor called"); } public void Test3() { Console.WriteLine("Third method called"); } static void Main() { Class2 c = new Class2(); c.test1(); c.test2(); c.Test3(); Console.ReadLine(); } } }
No comments:
Post a Comment