Inheritance and polymorphism

To be honest, I took a informational interview this morning.

Basically, I could answer most of questions properly. But, I could not answer a easy question because I was a little nervous. I made Big Big mistake……………………….

What is inheritance?

Inheritance allows one class to reuse the state and behavior of another class. The derived class inherits the properties and method implementations of the base class and extends it by overriding methods and adding additional properties and methods.

What is Polymorphism?

Polymorphism allows a client to treat different objects in the same way even if they were created from different classes and exhibit different behaviors. You can use implementation inheritance to achieve polymorphism in languages such as C++ and Java. Base class object’s pointer can invoke methods in derived class objects. You can also achieve polymorphism in C++ by function overloading and operator overloading.

Inheritance is a common concept. I am a human, except when I first wake up. I inherit certain properties from the class Human, such as my ability to converse more or less and my dependence on air, food, and carbohydrate-based beverages with lots of caffeine. The class Human inherits its dependencies on air, water, and nourishment from the class Mammal, Which inherits from the class Animal.

The ability to pass down properties is a powerful one. It enables you to describe things in an economical way. For example, if my son asks, "What’s a duck?" I can say, "It’s a bird that goes quack." Despite what you may think, that answer conveys a considerable amount of information. My son knows what a bird is, and now he knows all those same things about a duck pus the duck’s additional property of "quackness."

Please see the following sample code.

using System;
using System.Collections.Generic;
using System.Text;

namespace Car
{
    class Car {
        public int totalDistance;
        public int gas;

        public int Run(int distacen){
            gas = gas – distacen;
            totalDistance = totalDistance + distacen;
            return totalDistance;
        }

        public void Fillup(){
            gas = 60;
        }
    }

    class Porsche : Car{
        public string GetCarType(){
            return "Porsche";
        }
    }

    class BMW : Car{
        public string GetCarType(){
            return "BMW";
        }
    }

    class CarApp{
        static void Main(){

            Porsche porsche = new Porsche();
            BMW bmw = new BMW();

            porsche.Run(20);
            bmw.Run(30);

            Console.WriteLine(porsche.GetCarType() + ": Distance = " + porsche.totalDistance.ToString());
            Console.WriteLine(bmw.GetCarType() + ": Distance = " + bmw.totalDistance.ToString());

            Console.ReadLine();
        }
    }
}

 

Advertisements

Synchronize the execution of multiple threads using a semaphore.

Problem: You need to control the number of threds that can access a shared resource or section of code concurerently.

Solutuin: You can use the System.Threading.Semaphore class.

The Code: The following example demonstrates how to use a named semaphore to limit access to shared resource to two threads at any given time.

——————

using System;
using System.Threading;

namespace Synchronize_Semaphoere
{
    class SSemaphore
    {
        static bool terminate = false;

        private static void TraceMsg(string msg)
        {
            Console.WriteLine("[{0,3}] – {1} : {2}",
                Thread.CurrentThread.ManagedThreadId,
                DateTime.Now.ToString("HH:mm:ss.ffff"), msg);
        }

        private static void DisplayMessage()
        {
            using (Semaphore sem = Semaphore.OpenExisting("SemaphoreExample"))
            {
                TraceMsg("Thread started.");

                while (!terminate)
                {
                    sem.WaitOne();
                    TraceMsg("Thread owns the Semaphore.");
                    Thread.Sleep(1000);
                    TraceMsg("Thread releasing the Semaphore.");
                    sem.Release();
                    Thread.Sleep(100);
                }
                TraceMsg("Thread terminating.");
            }
        }

        public static void Main()
        {
            using (Semaphore sem = new Semaphore(2, 2, "SemaphoreExample"))
            {
                TraceMsg("Starting threads — press Enter to terminate.");
                Thread trd1 = new Thread(DisplayMessage);
                Thread trd2 = new Thread(DisplayMessage);
                Thread trd3 = new Thread(DisplayMessage);
                trd1.Start();
                trd2.Start();
                trd3.Start();

                Console.ReadLine();

                terminate = true;
                trd1.Join(5000);
                trd2.Join(5000);
                trd3.Join(5000);
            }
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Main method complete. Press Enter.");
            Console.ReadLine();
        }
    }
}

 

 

using System;
using System.Threading;

namespace Synchronize_Semaphoere
{
    class SSemaphore
    {
        static bool terminate = false;

        private static void TraceMsg(string msg)
        {
            Console.WriteLine("[{0,3}] – {1} : {2}",
                Thread.CurrentThread.ManagedThreadId,
                DateTime.Now.ToString("HH:mm:ss.ffff"), msg);
        }

        private static void DisplayMessage()
        {
            using (Semaphore sem = Semaphore.OpenExisting("SemaphoreExample"))
            {
                TraceMsg("Thread started.");

                while (!terminate)
                {
                    sem.WaitOne();
                    TraceMsg("Thread owns the Semaphore.");
                    Thread.Sleep(1000);
                    TraceMsg("Thread releasing the Semaphore.");
                    sem.Release();
                    Thread.Sleep(100);
                }
                TraceMsg("Thread terminating.");
            }
        }

        public static void Main()
        {
            using (Semaphore sem = new Semaphore(2, 2, "SemaphoreExample"))
            {
                TraceMsg("Starting threads — press Enter to terminate.");
                Thread trd1 = new Thread(DisplayMessage);
                Thread trd2 = new Thread(DisplayMessage);
                Thread trd3 = new Thread(DisplayMessage);
                trd1.Start();
                trd2.Start();
                trd3.Start();

                Console.ReadLine();

                terminate = true;
                trd1.Join(5000);
                trd2.Join(5000);
                trd3.Join(5000);
            }
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Main method complete. Press Enter.");
            Console.ReadLine();
        }
    }
}