{"id":271,"date":"2017-06-14T21:36:00","date_gmt":"2017-06-14T21:36:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2017\/06\/14\/encapsulation-inheritance-and-polymorphism-in-object-oriented-programming\/"},"modified":"2019-03-22T13:08:17","modified_gmt":"2019-03-22T12:08:17","slug":"encapsulation-inheritance-and-polymorphism-in-object-oriented-programming","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/encapsulation-inheritance-and-polymorphism-in-object-oriented-programming\/","title":{"rendered":"Encapsulation, Inheritance and Polymorphism in Object Oriented Programming"},"content":{"rendered":"<p>These three concepts are sometimes considered as the building block of Object Oriented Programming(OOP).<\/p>\n<p>We cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">Encapsulation<\/a><\/li>\n<li><a href=\"#t2\">Inheritance<\/a><\/li>\n<li><a href=\"#t3\">Polymorhysim<\/a><\/li>\n<li><a href=\"#t4\">Virtual Methods<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h3><strong id=\"t1\">1. Encapsulation<\/strong><\/h3>\n<hr \/>\n<p>Encapsulation is a construct in OOP that allows both the object and the operations performed on that object (methods) to be bundled into a class and restricts access to the methods of the class.<\/p>\n<p>When a class is defined, it is normally made up of properties and methods. The properties refers to the attributes of the class while the method are the operations that can be carried out on the object.<\/p>\n<p>Lets take for example, a class called Person, defined as follows:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">person<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span>\r\n<span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #008800; font-weight: bold;\">private<\/span> string Firstname<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #008800; font-weight: bold;\">private<\/span> string Fastname<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #008800; font-weight: bold;\">private<\/span> datetime DateOfBirth<span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #008800; font-weight: bold;\">protected<\/span> Age<span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #008800; font-weight: bold;\">public<\/span> datetime getDateOfBirth<span style=\"color: #333333;\">()<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #008800; font-weight: bold;\">return<\/span> person<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">DateOfBirth<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: Trebuchet MS, sans-serif;\">In the example above class definition, the person class have been defined with four attributes, Firstname, Lastname, Age and DateOfBirth.<\/span><br \/>\n<span style=\"font-family: Trebuchet MS, sans-serif;\"><br \/>\n<\/span><span style=\"font-family: Trebuchet MS, sans-serif;\">The class have a method GetDateOfBirth() which returns the Date of Birth of the person.<\/span><br \/>\n<span style=\"font-family: Trebuchet MS, sans-serif;\">In the person class, encapsulation is achieved by the used of the keywords, private, public and protected.<\/span><br \/>\n<span style=\"font-family: Trebuchet MS, sans-serif;\"><br \/>\n<\/span><b><span style=\"font-family: Trebuchet MS, sans-serif;\">The public keyword<\/span><\/b><br \/>\n<span style=\"font-family: Trebuchet MS, sans-serif;\">This keyword allows makes the method or attribute accessible both from inside and outside the class.<\/span><br \/>\n<span style=\"font-family: Trebuchet MS, sans-serif;\"><br \/>\n<\/span><b><span style=\"font-family: Trebuchet MS, sans-serif;\">The private keyword<\/span><\/b><br \/>\n<span style=\"font-family: Trebuchet MS, sans-serif;\">Attributes and methods declared as private can only be accessible by methods inside the class. They can&#8217;t be accessible by external classes.<\/span><br \/>\n<span style=\"font-family: Trebuchet MS, sans-serif;\"><br \/>\n<\/span><b><span style=\"font-family: Trebuchet MS, sans-serif;\">The protected Keyword<\/span><\/b><br \/>\n<span style=\"font-family: Trebuchet MS, sans-serif;\">Attributes declared as protected can be accessed by from within the \u00a0class and is also accessible by derived from that class.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3><b id=\"t2\">2. Inheritance<\/b><\/h3>\n<hr \/>\n<p>Inheritance is an OOP concept that allows us to define a class bases on another class. So if two two object have similar attributes and one have been defined, the second object can inherit from the first one.<br \/>\nThis concepts make it easy to reuse existing code as well as reduces the size of code to be written. So when creating a new class, the programmer need not write completely new data members and functions, the programmer can designate the class to inherit attributes and functions of an existing class. Here the existing class is called the bases class and the new class that inherits the attributes is called derived class.<\/p>\n<p>The code below implemented in C# illustrates the concept of inheritance:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/This is the base class<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Shape<\/span>\r\n<span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #008800; font-weight: bold;\">protected<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> width<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #008800; font-weight: bold;\">protected<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> height<span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> setWidth <span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> w<span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> width <span style=\"color: #333333;\">=<\/span> w<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> setHeight <span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> h<span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> height <span style=\"color: #333333;\">=<\/span> h<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #333333;\">}<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n\r\n<span style=\"color: #888888;\">\/\/This is the derived class<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #bb0066; font-weight: bold;\">Triangle<\/span> <span style=\"color: #333333;\">:<\/span> Shape <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #888888;\">\/\/Triangle has Shape as the base class<\/span>\r\n<span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #008800; font-weight: bold;\">public<\/span> getArea<span style=\"color: #333333;\">()<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">\/<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span> <span style=\"color: #333333;\">*<\/span> width <span style=\"color: #333333;\">*<\/span> height<span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In the first class, Shape, the width and the height have is declared as protected, which means that they can be accessed by classes derived from it.<\/p>\n<p>The second class, Triangle does not have attributes of width and height but is meant to derive these properties from the base class, Shape<\/p>\n<p>The syntax for defining a derived class is<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: 'courier new' , 'courier' , monospace;\">class &lt;derived_class&gt; : &lt;base_class&gt;<\/span><\/p>\n<h3><\/h3>\n<h3><b id=\"t3\">3. Polymorphism<\/b><\/h3>\n<hr \/>\n<p>Polymorphism in Programming is a feature that allows an entity to have many forms. So a function is polymorphic if it can be applied on various types.<br \/>\nThere are three types of polymorphism, namely:<\/p>\n<p><b>Ad hoc Polymorphism<\/b>: In this type the same function perform different operations depending on the type of argument supplied.<br \/>\nThis is also called function overloading or operator overloading. We can illustrate this using the code fragment below:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Demo<\/span>\r\n<span style=\"color: #333333;\">{<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Add<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> x<span style=\"color: #333333;\">,<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> y<span style=\"color: #333333;\">)<\/span> <span style=\"color: #888888;\">\/\/ Performs <\/span>\r\n   integer addition\r\n   <span style=\"color: #333333;\">{<\/span>\r\n      <span style=\"color: #333399; font-weight: bold;\">int<\/span> result <span style=\"color: #333333;\">=<\/span> x<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #333333;\">+<\/span> y\r\n      Console<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">Writeln<\/span><span style=\"color: #333333;\">(<\/span>result<span style=\"color: #333333;\">);<\/span>\r\n   <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Add<\/span><span style=\"color: #333333;\">(<\/span>string s<span style=\"color: #333333;\">,<\/span> string t<span style=\"color: #333333;\">)<\/span> <span style=\"color: #888888;\">\/\/Concatenates the two strings<\/span>\r\n   <span style=\"color: #333333;\">{<\/span>\r\n      string result <span style=\"color: #333333;\">=<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span>s<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">Concat<\/span><span style=\"color: #333333;\">(<\/span>t<span style=\"color: #333333;\">);<\/span>\r\n      Console<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">Writeln<\/span><span style=\"color: #333333;\">(<\/span>result<span style=\"color: #333333;\">);<\/span>\r\n   <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>From the above class Demo, we see that two functions have been defined both having the same name. So the question would be: which of the definitions would be used when a call to this function is made? The function that would be called would depend on the parameters that are passed in at run time. If integer parameters are given at run time, then the first Add function is called which adds the two numbers together and displays the result. If however, two strings are passed in as parameter, the second function is called which concatenates the two strings and finds the result.<\/p>\n<p><b>Parametric Polymorphism<\/b>: Here, a function or data type is written generically such that it can handle values uniformly irrespective of their types. A function is a polymorphic function if it can evaluate or be applied to different types.<\/p>\n<p><b>Subtyping<\/b>: This is also called Subtype Polymorphism or inclusion polymorphism. This is when a method name denotes instances of different classes related by a base class. This is the most common type of polymorphism.<\/p>\n<p>The code fragments below illustrates this various types of polymorphism.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Shape<\/span>\r\n<span style=\"color: #333333;\">{<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> in x <span style=\"color: #333333;\">{<\/span>get<span style=\"color: #333333;\">;<\/span> set<span style=\"color: #333333;\">;}<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> y <span style=\"color: #333333;\">{<\/span> get<span style=\"color: #333333;\">;<\/span> set<span style=\"color: #333333;\">;}<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> height <span style=\"color: #333333;\">{<\/span>get<span style=\"color: #333333;\">;<\/span> set<span style=\"color: #333333;\">}<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> width <span style=\"color: #333333;\">{<\/span>get<span style=\"color: #333333;\">;<\/span> set<span style=\"color: #333333;\">}<\/span>\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> virtual <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Render<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #888888;\">\/\/This is a virtual method<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> Console<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">Write<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Initializing drawing tasks...\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The above class is the base class, Shape. Other classes would derive from this class for instance, Triangle, Rectangle, Circle, Square etc.<br \/>\nNote that the class method Draw have been declared as virtual. This means that if a class is derived from this base class, the derived class can also have a method with the same name and this method in the derived class can provide a definition that overrides the method in the base class.<\/p>\n<p>&nbsp;<\/p>\n<h3><b id=\"t4\">4. About Virtual Methods<\/b><\/h3>\n<p>A derived class can override a base class member only if the base class member have been declared as virtual or abstract. The derived member must use the override keyword to show that the method should override the one in the base class.<br \/>\nVirtual methods allow you to work with a number of related objects. So if we have a drawing application that have a base class Shape and a number of derived classes for particular shapes, then you may not know at the compile time which specific type of shape the user will create. This means that when the Draw method is invoked on a shape, the definition would be based on the particular shape it is being invoked on and this is determined at run time.<\/p>\n<p>The code below gives example of some derived classes from the base class, Shape.<\/p>\n<p>The code below is the derived class for Triangle<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Derived class for Triangle<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Triangle<\/span> <span style=\"color: #333333;\">:<\/span> Shape\r\n<span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">double<\/span> Area<span style=\"color: #333333;\">()<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #008800; font-weight: bold;\">return<\/span> height <span style=\"color: #333333;\">*<\/span> width<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #008800; font-weight: bold;\">public<\/span> override <span style=\"color: #333399; font-weight: bold;\">void<\/span> Draw<span style=\"color: #333333;\">()<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> Console<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">WriteLine<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Drawing a Triangle\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> base<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">Draw<\/span><span style=\"color: #333333;\">():<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The code below is the derived class for Circle<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Derived class for Circle<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Circle<\/span> <span style=\"color: #333333;\">:<\/span> Shape\r\n<span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> radius\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">double<\/span> Area<span style=\"color: #333333;\">()<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">22<\/span><span style=\"color: #333333;\">\/<\/span><span style=\"color: #0000dd; font-weight: bold;\">7<\/span> <span style=\"color: #333333;\">*<\/span> radius <span style=\"color: #333333;\">*<\/span> radius<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #008800; font-weight: bold;\">public<\/span> override <span style=\"color: #333399; font-weight: bold;\">void<\/span> Draw<span style=\"color: #333333;\">()<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> Console<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">WriteLine<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Drawing a circle\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> base<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">Draw<\/span><span style=\"color: #333333;\">():<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The code below is the derived class for Rectangle<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Derived class for Rectangle<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Rectangle<\/span> <span style=\"color: #333333;\">:<\/span> Shape\r\n<span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">double<\/span> Area<span style=\"color: #333333;\">()<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #008800; font-weight: bold;\">return<\/span> width <span style=\"color: #333333;\">*<\/span> height<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #333333;\">}<\/span>\r\n\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #008800; font-weight: bold;\">public<\/span> override <span style=\"color: #333399; font-weight: bold;\">void<\/span> Draw<span style=\"color: #333333;\">()<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #333333;\">{<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> Console<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">WriteLine<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Drawing a Rectangle...\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> base<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">Draw<\/span><span style=\"color: #333333;\">():<\/span>\r\n<span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\u00a0<\/span><span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In the three derived classes above, we see that even though the base class of Shape have a function for Draw, each of the derived classes also defines a function Draw. The Draw function in the derived classes have been declared with the override keyword and this is to indicate that the method would be used instead of the method in the base class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>These three concepts are sometimes considered as the building block of Object Oriented Programming(OOP). We cover the following: Encapsulation Inheritance Polymorhysim Virtual Methods &nbsp; 1. &hellip; <\/p>\n","protected":false},"author":1,"featured_media":851,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[414],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/271"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=271"}],"version-history":[{"count":4,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/271\/revisions"}],"predecessor-version":[{"id":850,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/271\/revisions\/850"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media\/851"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}