Inheritance - DotNet and DataBase

Tuesday, 17 July 2018

Inheritance





inheritance is a process in which one class (derived class) acquires all the properties and behaviors of its parent class(base class) automatically based on the access modifiers supplied in the parent class.

Syntax:
class baseclass
    {
      
    }

class Derivedlass: baseclass
    {
       
    }

Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InheritenceDiscussion1
{
    class Inheritance
    {
        public void method2()
        {
            baseclass dc = new Derivedlass();
            dc.method1();
        }
        static void Main(string[] args)
        {
            Inheritance i = new Inheritance();
            i.method2();
            Console.Read();
        }
    }

    class baseclass
    {
        public virtual void method1()
        {
            Console.WriteLine("I'm a base class member");
        }
    }

    class Derivedlass : baseclass
    {
        public override void method1()
        {
            Console.WriteLine("I'm a derived class member");
        }
    }
}



Types of inheritance:
1)single inheritance
2)multilevel inheritance
3)Hierarchical inheritance

Single inheritance:
One base inherited by one derived class is called single inheritance.

Syntax:
namespace StaticClass
{    
    class baseclass
    {
        
    }
    class derivedclass:baseclass
    {
        
    }
}


Multilevel inheritance:
One derived class is derived from another derived class is called multilevel inheritance.

Syntax:
namespace StaticClass
{    
    class baseclass
    {
        
    }
    class derivedclass:baseclass
    {
    }
    class derivedclass1:derivedclass
    {
    }
}

Hierarchical inheritance:
Multiple derived classes are derived from one base class is called Hierarchical inheritance.



Syntax:
namespace StaticClass
{    
    class baseclass
    {
        
    }
    class derivedclass:baseclass
    {
    }
    class derivedclass1:baseclass
    {
    }
    class derivedclass2:baseclass
    {
    }
}


Example of inheritance:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InheritenceDiscussion
{
    class Program
    {
        static void Main(string[] args)
        {
            derivedClass2 dc = new derivedClass2();
            dc.bcMethod1();
            dc.dcMethod2();
            dc.dcMethod3();
            Console.Read();
        }
    }
    //Base class
    public class baseclass
    {
        public void bcMethod1()
        {
            Console.WriteLine("I'm a base class member name bcMethod1");
        }
    }
    //Single Inheritence
    public class derivedclass : baseclass
    {
        public void dcMethod2()
        {
            Console.WriteLine("I'm a first derived class member name dcMethod2");
        }
    }
    ///*Multi Level Inheritence*/
    public class derivedClass2 : derivedclass
    {
        public void dcMethod3()
        {
            Console.WriteLine("I'm a second derived class member name dcMethod2");
        }
    }
    ///*Hierarchial Inheritence*/
    public class derivedClass3 : baseclass
    {
        public void dcMethod3()
        {
            Console.WriteLine("I'm a second derived class member name dcMethod2");
        }
    }
}


Note: .Net framework doesn't support multiple inheritance. We can achieve multiple inheritance using interfaces.


No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.