Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Inheritance In c#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 286
    Comment on it

    Inheritance is the one of the important feature of OOPS(object oriented programming language). This feature allows to create a class which is derived from the another class. Inheritance saves user's time and effort. It provides the reusability of the code. User don't need to write the same functions and data members again and again for every other classes. New class can inherit all the members and functions of existing class. This existing class is called as Base class and new class is called as Derived class. Derived class will have all the functionalities of the base class and also it can have its own new members and function itself.

     

    We can understand this concept via real life example. We can consider Human being as a base class and three new different classes of student, doctor and engineer as Derived class. As we know, all students, doctors and  engineers inherit the same features from Human being base class such as one nose two ears two eyes. So These derived classes inheriting the same features from base class but these derived can also have their different functionalities like students do studies, doctors do operations and engineers develop websites.

    Derived class syntax in c#:

     

    <acess-specifier> class <base_class>
    {
       ...//(members/functions)
    }
    class <derived_class> : <base_class>
    {
       ...//(members/functions)
    }

     

    For example:

    Consider a base class Shape and its derived class Square:

     

    using System;
    namespace InheritanceApplication
    {
       class Shape 
       {
          public void setSide(int a)
          {
             side = a;
          }
       
          protected int side;
         
    	 }
    
       // Derived class
       class Square: Shape
       {
          public int getArea()
          { 
             return (side * side); 
          }
       }
       
       class SquareArea
       {
          static void Main(string[] args)
          {
             Square Squ = new Square();
    
             Squ.setSide(5);
    
             // Print the area of the square.
             Console.WriteLine("Total area: {0}",  Squ.getArea());
             Console.ReadKey();
          }
       }
    }
    

     

    When you will debug your program, the output will be :

     

    Total area: 35

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: