OOP. Inheritance

      Comments Off on OOP. Inheritance

This topic contains 0 replies, has 1 voice, and was last updated by  Васильев Владимир Сергеевич 2 weeks, 4 days ago.

  • Author
    Posts
  • #4086

    Inheritance is a way to extend the existing functionality of data types by creating new ones on their base.

    For example, these can be types that are defined as a part of library.

    There is a rule which claims: never change something that works, so adding new functions to existing classes is a bad idea. To write it all over again from scratch is also a bad idea, because a part of the required functionality is already implemented. It should only be extended according to new requirements.

    For example, we have a class named Clock. Its functionality is very simple: it allows us to set and check the current time whenever we need:

    class Clock
    {
       DateTime current_Time;
       public void SetTime()
       {
          current_Time = DateTime.Now;
    
       }
       public void SetTime(DateTime dt)
       {
         //...
      } 
       public DateTime CheckCurrentTime()
      {   
         //...
       }
       public void PrintCurrentTime()
       {
          //...
       }
    }

    This is how we are testing its functionality: we create an instance of a class Clock, set current time with the appropriate method and then print it to console in the required format.

      class Program
        {
            static void Main()
            {
                Clock clock = new Clock();
                clock.SetTime();
                Console.WriteLine( clock.CheckCurrentTime().ToString("T"));
            }
        }

    Let’s assume that for a while we have been using this class, and we were totally satisfied with its functionality. But after some time we suddenly discover that in some cases we need more functionality than our class provides. What we need now is to set the time of alarm, to give a signal, to start and stop it. But in other cases what we need is just a normal clock.

    What should we do? Add missing methods to a class Clock? Bad idea.

    Create a new class and duplicate functionality of a simple clock class and add what we are missing? No.

    The solution is to create a new class AlarmClock which will inherit the existing functionality of class Clock , and add the missing functionality to the new child class. After that we can use it in those cases when we need not just a clock but an alarm clock.

    This is how our new inherited class will look like:

        class AlarmClock: Clock
        {
            //required alarm time
            DateTime alarmTime;
            //defines if the class is working as an alarm clock
            bool is_active;      
            public void SetAlarmTime(DateTime dt)
            {
              //...
                alarmTime = dt;
            }  
            void DoAlarm()
            {
               //...
                StopAlarm();
            }    
            public void StartAlarm()
            {
                is_active = true;
                Console.WriteLine("AlarmClock started");
                //...      
            }   
            void StopAlarm()
            {
                //...
                is_active = false;
                Console.WriteLine("AlarmClock stopped");
            }
        }

    Now let’s test how it works:

    class Program
    {
       static void Main()
       {
          AlarmClock aclock = new AlarmClock();
          //calling base class method
          //setting current time
          aclock.SetTime();
          //checking current time
          Console.WriteLine(aclock.CheckCurrentTime().ToString("T"));
          //calling method of inherited class
          aclock.SetAlarmTime(DateTime.Now.AddMinutes(10));
          aclock.StartAlarm();
          Console.ReadKey();
       }
    }

    Now we have 2 classes. A Parent class – Clock and the one derived from it – AlarmClock. The advantage of inheritance is that the derived type has a functionality of its parent class as well as its own, and there is no duplication in the code. As a result we have 2 full-fledged classes, and each of them can be used in appropriate situations.

    We shall make a conclusion by saying that the AlarmClock class INHERITS the functionality of its base class Clock.

You must be logged in to reply to this topic.