Showing posts with label inheritance. Show all posts
Showing posts with label inheritance. Show all posts

Thursday, September 6, 2018

Types of Inheritance in c#

Single Inheritance - Derived class is created from a single base class. SUPPORTED

Multi-level inheritance - Derived class is created from another derived class. SUPPORTED

Multiple inheritance - A derived class is created from more than one base class. NOT SUPPORTED

Multipath inheritance - In this inheritance, a derived class is created from multiple derived classes and each of these derived classes have same base class. NOT SUPPORTED

Hierarchial inheritance - In this inheritance, more than one derived classes are created from a single base. SUPPORTED

Hybrid inheritance - This is combination of more than one inheritance. So depending on type of combination we can say SUPPORTEDor NOT SUPPORTED

Sunday, August 17, 2014

Understanding Runtime Polymorphism

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

namespace ConsoleApps
{
    class BaseClass
    {
        public void Normal()
        {
            Console.WriteLine("Base Class print");
        }
        public virtual void RuntimePoly()
        {
            Console.WriteLine("Base Class print");
        }
    }

    class DerivedClass : BaseClass
    {
        public new void Normal()
        {
            Console.WriteLine("Derived Class print");
        }

        public override void RuntimePoly()
        {
            Console.WriteLine("Derived Class print");
        }
    }

class UnderstandingRuntimePolymorphism
{
 public static void Main()
{
           // Here Base class method is called
           BaseClass nm = new DerivedClass();
           nm.Normal();

          // Here Derived class method is called
            // Due to run time decision (late binding or runtime polymorphism)
            // since the methods are overriden with ovveride keyword in derived class
            // and virtual keyword in base class of the methods

           BaseClass poly = new DerivedClass();
           poly.RuntimePoly();
     }
 }
}


Monday, July 28, 2014

Understanding Inheritance Concept in C#

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

namespace Apps
{

    class Doctor
    {
        protected string name, specialization;
        public Doctor(string name, string specialization)
        {
            this.name = name;
            this.specialization = specialization;
        }
        public void Print()
        {
            Console.WriteLine(name);
            Console.WriteLine(specialization);
        }
    }
    class ResidentalDoctors : Doctor
    {
        protected int salary;
        public ResidentalDoctors(string name, string specialization, int salary)
            : base(name, specialization)
        {
            this.salary = salary;
        }
        public new void Print()
        {
            base.Print();
            Console.WriteLine(salary);
        }
        public int GetPay()
        {
            return salary;
        }
    }
    class Consultant : Doctor
    {
        protected int fees, visitNum;
        public Consultant(string name, string specialization, int fees, int visitNum)
            : base(name, specialization)
        {
            this.fees = fees;
            this.visitNum = visitNum;
        }
        public new void Print()
        {
            base.Print();
            Console.WriteLine(fees);
            Console.WriteLine(visitNum);
        }
        public int GetPay()
        {
            return fees * visitNum;
        }
    }

    class UnderstandingInheritanceConcept
    {
        public static void Main()
        {
            ResidentalDoctors rd = new ResidentalDoctors("Steve""General", 100);
            Consultant c = new Consultant("Chitti""General", 300, 10);
            Console.WriteLine(rd.GetPay());
            Console.WriteLine(c.GetPay());
            rd.Print();
            c.Print();
        }
    }
}