Inheritance in C#

0
Inheritance in C#:- It means Reusability, using this we can acquire the features of the parent class to the child class. Inheritance provides a relationship between classes.

Type of Inheritance:-


1.1 using Class


1)  Single  Inheritance:- 
 We can adopt the features of base class to derived class

  class A
    {
       internal  void fun1()
        {
            Console.WriteLine("A");
        }
    }
    class B:A
    {
       internal  void fun2()
        {
            Console.WriteLine("B");
        }
     
    }

Example of Inheritance:-

class InheritanceExample
    {
        int id;
        String name;
        public void accept(int id,String name)
        {
            this.id = id;
            this.name = name;
        }
        public void display()
        {
            Console.WriteLine("ID is {0} and name is {1}", id, name);
        }
       
    }

class EmployeeExample:InheritanceExample
    {
        int salary;
        internal void accept1(int salary)
        {
            this.salary = salary;
        }
        internal void display1()
        {
            Console.WriteLine("Salary is " + salary);
        }

        public static void Main()
        {
            EmployeeExample obj = new EmployeeExample();
            obj.accept(1001, "xyz");
            obj.accept1(45000);
            obj.display();
            obj.display1();

        }
    }


2) Multilevel Inheritance:-  We can adopt the features of base class to a derived class to sub derived class

class A
    {
       internal  void fun1()
        {
            Console.WriteLine("A");
        }
    }
    class B:A
    {
       internal  void fun2()
        {
            Console.WriteLine("B");
        }
     
    }

    class C : B
    {
        internal void fun3()
        {
            Console.WriteLine("C");
        }

        static void Main()
        {
            C obj = new C();
            obj.fun1();
            obj.fun2();
            obj.fun3();
            Console.ReadKey();
        }
    }

Example of Multilevel Inheritance:-

class InheritanceExample
    {
        int id;
        String name;
        public void accept(int id,String name)
        {
            this.id = id;
            this.name = name;
        }
        public void display()
        {
            Console.WriteLine("ID is {0} and name is {1}", id, name);
        }
       
    }


class EmployeeExample:InheritanceExample
    {
        int salary;
        internal void accept1(int salary)
        {
            this.salary = salary;
        }
        internal void display1()
        {
            Console.WriteLine("Salary is " + salary);
        }

        public static void Main()
        {
            EmployeeExample obj = new EmployeeExample();
            obj.accept(1001, "xyz");
            obj.accept1(45000);
            obj.display();
            obj.display1();

        }
    }

class OtherStaffExample: EmployeeExample
    {
        int bonus;
        void accept2(int bonus)
        {
            this.bonus = bonus;
        }
        void display2()
        {
            Console.WriteLine("bonus is " + bonus);
        }

        public static void Main()
        {
            OtherStaffExample obj = new OtherStaffExample();
            obj.accept(1003, "OtherStaff");
            obj.accept1(45000);
            obj.accept2(3000);
            obj.display();
            obj.display1();
            obj.display2();
            Console.ReadKey();
        }
    }

 class OtherStaffExample:EmployeeExample
        {
            int bonus;
            void accept2(int bonus)
            {
                this.bonus = bonus;
            }
            void display2()
            {
                Console.WriteLine("bonus is " + bonus);
            }

            public static void Main()
            {
                OtherStaffExample obj = new OtherStaffExample();
                obj.accept(1003, "OtherStaff");
                obj.accept1(45000);
                obj.accept2(3000);
                obj.display();
                obj.display1();
                obj.display2();
                Console.ReadKey();
            }
        }

3) Hierarchical Inheritance:-  One base class feature will be implemented into multiple child classes similar to a tree structure.

class A
    {
       internal  void fun1()
        {
            Console.WriteLine("A");
        }
    }
    class B:A
    {
       internal  void fun2()
        {
            Console.WriteLine("B");
        }
     
    }

    class C : A
    {
        internal void fun3()
        {
            Console.WriteLine("C");
        }

        static void Main()
        {
            C obj = new C();
            obj.fun1();
            obj.fun3();
            B obj1 = new B();
            obj1.fun1();
            obj1.fun2();
            Console.ReadKey();
        }
    }

1.2 using Interface:- 

 these features will be implemented into the interface concept 

1)  Single

2) Multiple





Tags

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)