<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ibrahim Suleiman</title>
    <description>The latest articles on Forem by Ibrahim Suleiman (@ibs).</description>
    <link>https://forem.com/ibs</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F682009%2F747ddad1-f0d5-4d9f-b518-42715d9ed4b3.jpg</url>
      <title>Forem: Ibrahim Suleiman</title>
      <link>https://forem.com/ibs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ibs"/>
    <language>en</language>
    <item>
      <title>Using React Higher-Order Components (HOC) in your React apps</title>
      <dc:creator>Ibrahim Suleiman</dc:creator>
      <pubDate>Wed, 06 Apr 2022 11:24:19 +0000</pubDate>
      <link>https://forem.com/ibs/using-react-higher-order-components-hoc-in-your-react-apps-5421</link>
      <guid>https://forem.com/ibs/using-react-higher-order-components-hoc-in-your-react-apps-5421</guid>
      <description>&lt;p&gt;You have probably used Higher-Order functions in your JavaScript apps at one point or the other and the power and functionality it gives to developers fascinate you. But do you know you can achieve similar satisfaction using React? This is where React’s Higher-Order component comes to play!&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Prerequisites&lt;/li&gt;
&lt;li&gt;Introduction to Higher-Order Components&lt;/li&gt;
&lt;li&gt;
Recap of Higher-Order functions

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;.map()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.forEach()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.filter()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Deep dive into Higher-Order Components&lt;/li&gt;

&lt;li&gt;Using a Higher-Order Component&lt;/li&gt;

&lt;li&gt;Other Use Cases&lt;/li&gt;

&lt;li&gt;Conclusion&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;This is an advance concept in React. To follow along you will need the following: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/" rel="noopener noreferrer"&gt;Fundamental knowledge of JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/tutorial/tutorial.html" rel="noopener noreferrer"&gt;Fundamental knowledge of React&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nodejs.org/en/download/" rel="noopener noreferrer"&gt;NPM installed on your computer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;The latest version of React &lt;a href="https://www.freecodecamp.org/news/install-react-with-create-react-app/" rel="noopener noreferrer"&gt;installed&lt;/a&gt; on your computer, and &lt;/li&gt;
&lt;li&gt;The grit to complete the tutorial :-)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Introduction to Higher-Order Components
&lt;/h3&gt;

&lt;p&gt;The concept of a Higher-Order component (HOC) in React is an advanced technique that allows the reuse of a components’ logic by other components. It enables software developers to factor out common patterns or functionalities among various components or a group of components and creates a single component that you can then reuse in place of all the components having common structures or properties. &lt;/p&gt;

&lt;p&gt;A typical React application comprises of various components and sometimes these components might have some similarities. Because of the hassle of having to copy and paste similar components next to each other just to change that little detail between them, the React team came up to use “Higher-Order components”.&lt;/p&gt;

&lt;p&gt;HOCs allow software developers to write cleaner and more maintainable code by not repeating the same piece of code repeatedly. This abides by the principle of don’t-repeat-yourself (DRY) which is a core principle to follow in every software development. &lt;/p&gt;

&lt;p&gt;HOCs in React are built upon the foundation Higher-Order functions. So before we go deep into the details of HOCs, let’s do a quick recap of Higher-Order functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recap of Higher-Order Functions
&lt;/h3&gt;

&lt;p&gt;A Higher-Order function in JavaScript is a function that takes in functions as parameters and/or returns a function as an output.&lt;/p&gt;

&lt;p&gt;Below are some examples of the built-in Higher-Order functions built into JavaScript.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.map()&lt;/code&gt;: When called on an array, the method executes a callback function on each element of the array. The callback function takes in each array element one at a time, performs an operation with it, and then returns the result. It then stores the result in a new array which the method then returns. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Please note that this method does not modify the original array it is called upon, but it returns a new array whose elements might differ from the original array it was called upon. &lt;/p&gt;

&lt;p&gt;The example below shows the &lt;code&gt;.map()&lt;/code&gt; method in action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [1, 2, 3, 4]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [1, 4, 9, 16]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first log statement shows that the original array is not modified. The second log statement shows how the method returns a new array whose element is determined by the callback function.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: A callback function is a function that is passed into another function as an argument, usually Higher-Order functions. This callback function can then be executed inside the Higher-Order function it was passed in.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.forEach()&lt;/code&gt;: This method also takes in a callback function as an argument and passes each element of the array it was called upon into the callback function one at a time. The array element passed into the callback function is then used inside it to operate. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike the &lt;code&gt;.map()&lt;/code&gt; method, this method returns &lt;code&gt;undefined&lt;/code&gt;. It does not modify the array elements it was called upon as well.&lt;/p&gt;

&lt;p&gt;The example below shows the &lt;code&gt;.forEach()&lt;/code&gt; method in action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;even&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;even&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;span class="c1"&gt;// 5&lt;/span&gt;
&lt;span class="c1"&gt;// 7&lt;/span&gt;
&lt;span class="c1"&gt;// 9&lt;/span&gt;
&lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code example increments each number by 1 and logs the output. The last line (&lt;code&gt;undefined&lt;/code&gt;) shows the return value of the &lt;code&gt;.forEach()&lt;/code&gt; method.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.filter()&lt;/code&gt;: The method behaves similar to the two methods mentioned above, i.e. it takes a callback function that works on each element of the original array it was called upon. The only difference is that it returns an array that is a subset of the original array. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As the name applies, this method filters an array to return a new array. It does not modify the elements of the original array.&lt;br&gt;
The elements that get added from the original array to this new array are dependent on whether the callback function returns &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Below is an example of using the &lt;code&gt;.filter()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;even&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;even&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//[ 2, 4, 6, 8, 10 ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first log statement above shows that this method does not modify the original array, and the second array shows the returned array that was filtered (even numbers) from the original array. &lt;/p&gt;

&lt;p&gt;The above methods are just a few examples and uses of Higher-Order functions. There are a bunch of Higher-Order functions in JavaScript that you can utilize in your code, and the best thing is that you can also create your own.&lt;/p&gt;

&lt;p&gt;Now that we have had a quick recap of Higher-Order functions, it’s a good time to continue with the discussion on Higher-Order Components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deep dive into Higher-Order Components
&lt;/h3&gt;

&lt;p&gt;In simple terms, a Higher-Order component is a function that takes in a component and returns a new component, which can then be stored as a component. The new component returned wraps over and does not modify the previous input component. &lt;/p&gt;

&lt;p&gt;HOCs were inspired by Higher-Order functions, which is the main reason we had to go over a quick recap of what Higher-Order functions are.&lt;/p&gt;

&lt;p&gt;Below is a pattern that most HOCs follow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;higherOrderComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;InputComponent&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Wrapper&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;InputComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;Wrapper&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Below are a few things you need to know about HOCs&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;They are functions&lt;/strong&gt;&lt;br&gt;
This might sound conflicting, but let us look at it closely. When a HOC takes in a component, it returns a new component, which is then stored in a variable. This variable is then referred to as a component because it contains a component returned from the function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;They take in one or more components as an argument&lt;/strong&gt;&lt;br&gt;
In most cases, they also take in another parameter along with these components. This parameter is then used to determine the output of the new component. The parameter can either be a callback function, a data object, a variable, or whatever you chose.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;They return a new component&lt;/strong&gt;&lt;br&gt;
This new component that is returned is the input component wrapped with more functionality or design.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;They are pure functions&lt;/strong&gt;&lt;br&gt;
HOCs have no effects on the component they take in. They don’t change the behavior of the input components.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Using a Higher-Order Component
&lt;/h3&gt;

&lt;p&gt;Having discussed a bit of the story about HOCs, it’s now time we get into the application. The examples below show where you can use HOCs in your everyday apps. From the examples, you will get to know more about HOCs and why you need to use them in your React applications.&lt;/p&gt;

&lt;p&gt;For this example, we will create a simple prototype of a blog site that contains social media links, navigation links, and the actual blog content of the page. Each page should contain navigation links and the blog’s social media handles. &lt;/p&gt;

&lt;p&gt;If you are new to React, ensure to go through the note below, else you can simply just jump through.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: We will use &lt;code&gt;create-react-app&lt;/code&gt; to bootstrap our project. Before starting on the project, follow these steps below: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to your desired directory and run the command &lt;code&gt;npx create-react-app my-app&lt;/code&gt;. This will create a my-app directory, which is your project directory containing all you need to spin up a react app in your browser.&lt;/li&gt;
&lt;li&gt;Navigate to the &lt;code&gt;src&lt;/code&gt; folder and delete all its content except &lt;code&gt;index.js&lt;/code&gt; and &lt;code&gt;App.js&lt;/code&gt;. We are building a very simple app and we won’t need the other files.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s start by creating a sample blog page.&lt;br&gt;
Once you are inside the &lt;code&gt;src&lt;/code&gt; folder in the project directory, create a file named &lt;code&gt;page1.js&lt;/code&gt; and add the following contents to the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Page1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Page&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Page&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="nx"&gt;contents&lt;/span&gt; &lt;span class="nx"&gt;here&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Page1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above creates a simple functional component that returns some content. This component is then exported so it can be imported into other components. &lt;/p&gt;

&lt;p&gt;We will create three pages for this tutorial to simulate a website with one blog post per page. The second and third pages will contain similar contents to &lt;code&gt;page1.js&lt;/code&gt; we just created. You can create both of them as &lt;code&gt;page2.js&lt;/code&gt; and &lt;code&gt;page3.js&lt;/code&gt;. Then add the contents of &lt;code&gt;page1.js&lt;/code&gt; we just created above to the two pages you just created. The last step is to change every occurrence of “Page 1” to “Page 2” and “Page 3” in both files, respectively.&lt;/p&gt;

&lt;p&gt;After creating the pages, the next step is to create a wrapper we will use to wrap each page. This is because we want each page of the blog to have a link to the blog’s social media accounts and the navigation bar.  &lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;src&lt;/code&gt; directory, create a file named &lt;code&gt;Wrapper.js&lt;/code&gt; and the following contents into the newly created file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Wrapper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PageWrapper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="nx"&gt;vh&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nx"&gt;Connect&lt;/span&gt; &lt;span class="kd"&gt;with&lt;/span&gt; &lt;span class="nx"&gt;us&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="err"&gt;“”&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Facebook&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/a&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="err"&gt;“”&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Instagram&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/a&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="err"&gt;“”&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Twitter&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/a&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;hr&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Home&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;About&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;hr&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;

       &lt;span class="c1"&gt;// Input component displays here&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Page&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;PageWrapper&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We created a functional component named &lt;code&gt;Wrapper&lt;/code&gt; which takes in another component named &lt;code&gt;Page&lt;/code&gt;. Inside of the &lt;code&gt;Wrapper&lt;/code&gt; component is another component named &lt;code&gt;PageWrapper&lt;/code&gt; that is used to wrap around the &lt;code&gt;Page&lt;/code&gt; component. The &lt;code&gt;PageWrapper&lt;/code&gt; contains other contents we would like to be displayed on every page on the blog, and then the &lt;code&gt;&amp;lt;Page /&amp;gt;&lt;/code&gt; component, which contains contents specific to that page alone.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: We added a little styling to the wrapper component to make each page occupy the entire height of the screen. This is just to simulate the behavior of a typical blog page.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Towards the end of the file, the &lt;code&gt;PageWrapper&lt;/code&gt; component is returned to the &lt;code&gt;Wrapper&lt;/code&gt; component. The return statement implies that when we call the &lt;code&gt;Wrapper&lt;/code&gt; component with an input component (&lt;code&gt;Page&lt;/code&gt; in this case) we get the &lt;code&gt;PageWrapper&lt;/code&gt; component in return.&lt;/p&gt;

&lt;p&gt;The last line of the file is exporting the component so it can be used outside of the file.&lt;/p&gt;

&lt;p&gt;Having created a wrapper that can wrap all the pages, the next step is to use the wrapper on each of the pages we created. We can achieve this by importing the wrapper into each of the pages like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Wrapper&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;Wrapper&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then replace the export statement at the bottom of each Page component with this line (please edit the &lt;code&gt;Wrapper&lt;/code&gt; argument as appropriate i.e to match &lt;code&gt;Page2&lt;/code&gt; and &lt;code&gt;Page3&lt;/code&gt; components respectively):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nc"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Page1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we just did up there was taking the whole content of each page and wrapping it with the wrapper before exporting it. Recall that &lt;code&gt;Wrapper&lt;/code&gt; takes in a component, so we must pass in this input component wherever &lt;code&gt;Wrapper&lt;/code&gt; is called.&lt;/p&gt;

&lt;p&gt;Now that we are done creating the files, the next step is to import these components into our &lt;code&gt;App.js&lt;/code&gt;. Replace the content of your &lt;code&gt;App.js&lt;/code&gt; file with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;react&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;WrappedPage1&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;Page1&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;WrappedPage2&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;Page2&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;WrappedPage3&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;Page3&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;WrappedPage1&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;WrappedPage2&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;WrappedPage3&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first set of import statements imports the contents of our blog pages with a suitable name that can refer to these imported pages. &lt;/p&gt;

&lt;p&gt;The next block of code is a functional component named &lt;code&gt;App&lt;/code&gt; which returns a &lt;code&gt;div&lt;/code&gt;. Inside of this &lt;code&gt;div&lt;/code&gt; is where we include the wrapped components we imported earlier. You might notice that we are using the tag &lt;code&gt;&amp;lt;&lt;/code&gt; and &lt;code&gt;/&amp;gt;&lt;/code&gt; to display these imported components. This is because the &lt;code&gt;Wrapped&lt;/code&gt; component we created earlier returns another component. So for React to treat it as a component, we must include the tag. &lt;/p&gt;

&lt;p&gt;At this point, if you navigate to the project directory inside your terminal and run the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will open a new window in your browser, which will look similar to the one below: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdwmk4jtkp8k8tdhyoysv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdwmk4jtkp8k8tdhyoysv.png" alt="Output" width="476" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And if you scroll down to the next page, you will notice the navigation and the social media links are still present at the top of the page. This is the same for the third page as well.&lt;/p&gt;

&lt;p&gt;So from the example above, you notice that because we are using Higher-Order components; we didn’t have to repeat similar patterns between pages. We simply create a component that has all these similarities and wraps all other components with it.&lt;/p&gt;

&lt;p&gt;The steps we went through above might seem stressful just to achieve something as little as that. But imagine if you have over 3 pages, say 5, or 10, or even 50 pages, it will become hard and eventually become impossible to copy and paste the social media account and navigation links into all the pages.&lt;/p&gt;

&lt;p&gt;Another benefit we derived from using HOC for the above example is that it made our code simple and easy to debug. If there is any change that needs to be done on any of the social media links, we can make the change only in the wrapper component and it will reflect everywhere.&lt;/p&gt;

&lt;h3&gt;
  
  
  Other Use Cases
&lt;/h3&gt;

&lt;p&gt;The example we illustrated above is just one out of the many areas where you can apply HOCs in your React apps. Below are some other areas where they can be used: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Controlled display&lt;/strong&gt;: You can use a HOC to control the components that will be displayed to the currently logged-in user based on their role. For instance, users who are not logged in will only be able to see the Login component. Logged in users will see the page content but cannot see all users of the app, nor those online. But the admin can see all of these.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This might seem like something that can be achieved using conditional rendering. But imagine an app where tens or even hundreds of components on a single page where the currently logged-in user can be multiple roles from a bunch of roles. Having such logic in the main app component is not the ideal design. In some situations, the main app component might also have some logic running within it, so combining that with the logic of conditional rendering will be cumbersome and hard to maintain.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Automatically pass properties into components&lt;/strong&gt;: You might have a bunch of properties you want to pass into several components. This process might seem repetitive. You can easily achieve this using a HOC to wrapping each component. The HOC then passes these props into every component it takes in.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Display loading page&lt;/strong&gt;: You might also want to display a loading page for the user while a data fetch from an external source is still loading. And then display the page contents when the data is done loading. Both the loading page and the content page will be wrapped inside a HOC, which will then return one component at a time. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a good design practice because it abstracts that little functional logic in our code from the main logic.&lt;/p&gt;

&lt;p&gt;The above examples are a few of the use cases of HOCs. There are many more out there that are easy to detect. Compare them with the examples we have discussed here and you will not have a hard time deciding whether to use a HOC in your next React app or not.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The aim of this tutorial is to enlighten the readers on what HOCs are and how they can use them in their everyday React applications. In this tutorial, you just learned the basics of Higher-Order functions, what HOCs really are, and the application of HOCs.&lt;/p&gt;

&lt;p&gt;The concept of Higher-Order components might be difficult for beginners to follow along. It is advisable go grasp the basic of JavaScript and Higher-Order functions before coming back to the tutorial.&lt;/p&gt;

&lt;p&gt;With Higher-Order components, we are able to factor out common fragments in our code and reuse them across these components. We have also use HOCs to filter what components to display, as well as reducing the logic of conditional rendering on the main component. This made our code easy to debug and new developers can easily follow along. If you are interested in learning more about React HOCs, do well to checkout the &lt;a href="https://reactjs.org/docs/higher-order-components.html" rel="noopener noreferrer"&gt;React documentation&lt;/a&gt; where they dive deeper into using HOCs. I hope you start using HOCs in you apps as soon as you grasp the concept. &lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Making Decisions in Python</title>
      <dc:creator>Ibrahim Suleiman</dc:creator>
      <pubDate>Thu, 09 Dec 2021 12:44:01 +0000</pubDate>
      <link>https://forem.com/ibs/making-decisions-in-python-5elj</link>
      <guid>https://forem.com/ibs/making-decisions-in-python-5elj</guid>
      <description>&lt;p&gt;As humans and an intelligent species, we always make decisions before taking some actions. These decisions can either be based on knowledge or choice. It is true that every human has the ability to make decisions, but what about the computer? Is it yet intelligent enough to make decisions for itself? We will find out from this article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Table of Contents
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
Making decisions in Python

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;if&lt;/code&gt; statement&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;if … else&lt;/code&gt; statement&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;if … elif … else&lt;/code&gt; statement&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Conclusion&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Prerequisite
&lt;/h3&gt;

&lt;p&gt;Have the latest version of &lt;a href="https://python.org/downloads" rel="noopener noreferrer"&gt;Python&lt;/a&gt; installed on your computer&lt;/p&gt;

&lt;h3&gt;
  
  
  Making Decisions In Python
&lt;/h3&gt;

&lt;p&gt;Python allows us to write programs that are executed only if some conditions are true. This paradigm of programming are sometimes called control flows. They control the flow of execution from one part of the program to another. They decide which part of our programs should be executed and which part should not. This is the basis on which the computer makes decisions.&lt;/p&gt;

&lt;p&gt;Python provides for us mainly three constructs for making decisions, which are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;if&lt;/code&gt; statement&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;if … else&lt;/code&gt; statement&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;if … elif … else&lt;/code&gt; statement&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  The "if" Statement
&lt;/h4&gt;

&lt;p&gt;The basic syntax for the &lt;code&gt;if&lt;/code&gt; statement is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;if &lt;code&gt;this is true&lt;/code&gt;:&lt;br&gt;
      &lt;code&gt;do this&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Two important things to note from this syntax are the colon &lt;code&gt;:&lt;/code&gt; and the indentation. Unlike other languages like Java and JavaScript that use curly braces to wrap a block of code, Python uses indentation instead. You will see the indentation syntax more often when you start dealing with functions and methods in python.&lt;/p&gt;

&lt;p&gt;For your code to run properly, the colon and the indentation must be properly written. The recommended way to indent in Python is to use 4 space indentation according to &lt;a href="https://pep8.org/#indentation" rel="noopener noreferrer"&gt;PEP8 style guide&lt;/a&gt;. If the statement to execute when an expression is true is not indented, it will still execute, but irrespective of whether the boolean expression is true or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Greet&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Greet&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Hello, World!&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;## output
# Hello, World!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The program above checks for the value of &lt;code&gt;command == Greet&lt;/code&gt; and prints &lt;strong&gt;”Hello, World!”&lt;/strong&gt; to the console if it evaluates to true. This condition is known as a &lt;em&gt;Boolean expression&lt;/em&gt;. An expression that evaluates to either true or false. Also, note the &lt;code&gt;==&lt;/code&gt; operator that was used for the boolean expression. This is called the &lt;em&gt;comparison operator&lt;/em&gt; and it’s different from the assignment operator &lt;code&gt;=&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Below are the comparison operators Python uses to evaluate a boolean expression:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;==&lt;/td&gt;
&lt;td&gt;is equal to&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;!=&lt;/td&gt;
&lt;td&gt;is not equal to&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;gt;&lt;/td&gt;
&lt;td&gt;is strictly greater than&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;lt;&lt;/td&gt;
&lt;td&gt;is strictly less than&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;gt;=&lt;/td&gt;
&lt;td&gt;is greater than or equal to&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;lt;=&lt;/td&gt;
&lt;td&gt;is less than or equal to&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These operators return either &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt; when used and they all accept two arguments, to the left, and to the right.&lt;/p&gt;

&lt;p&gt;Let’s now modify the program above to see what will happen if the boolean expression is not true:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Greet&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="c1"&gt;# when the boolean expression is not true
&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;greet&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Hello, World!&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Bye&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;## output
# Bye
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code inside the &lt;code&gt;if&lt;/code&gt; block was not executed because &lt;code&gt;Greet&lt;/code&gt; is not equal to &lt;code&gt;greet&lt;/code&gt;. They both have different Unicode values. The &lt;code&gt;if&lt;/code&gt; statement is the basic way our programs can make decisions by themselves.&lt;/p&gt;

&lt;h4&gt;
  
  
  The "if … else" Statement
&lt;/h4&gt;

&lt;p&gt;The syntax for the &lt;code&gt;if … else&lt;/code&gt; statement is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;if &lt;code&gt;&amp;lt;this is true&amp;gt;&lt;/code&gt;:&lt;br&gt;
      &lt;code&gt;&amp;lt;do this&amp;gt;&lt;/code&gt;&lt;br&gt;
  else:&lt;br&gt;
      &lt;code&gt;&amp;lt;then do this&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This syntax is similar to the &lt;code&gt;if&lt;/code&gt; statement, but the code in the &lt;code&gt;else&lt;/code&gt; block is executed only when the conditions in the &lt;code&gt;if&lt;/code&gt; block evaluate to false. The &lt;code&gt;else&lt;/code&gt; block is never executed when the &lt;code&gt;if&lt;/code&gt; block evaluates to true.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Hello, World!&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;The system is unable to greet you&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Bye&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;## output
# The system is unable to greet you
# Bye
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variable &lt;code&gt;greet&lt;/code&gt; is used to hold a boolean value, which is what a boolean expression will return when executed. But &lt;code&gt;greet&lt;/code&gt; is false, which means the &lt;code&gt;if&lt;/code&gt; block won’t get executed, leaving the &lt;code&gt;else&lt;/code&gt; block to get executed. And the last line of the code that prints &lt;strong&gt;”Bye”&lt;/strong&gt; gets executed no matter what. &lt;/p&gt;

&lt;h5&gt;
  
  
  Nested "if" statement
&lt;/h5&gt;

&lt;p&gt;The nested &lt;code&gt;if&lt;/code&gt; statements allow you to nest an &lt;code&gt;if&lt;/code&gt; statement inside another &lt;code&gt;if&lt;/code&gt; statement. This is useful when you want to make decisions if a particular decision is true or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;i_am_human&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i_am_human&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
       &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;I am a tall human&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
       &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;I am an average height human&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;I am not a human&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;## output
# I am a tall human
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of the program is based on the fact that &lt;code&gt;i_am_human&lt;/code&gt; is true and &lt;code&gt;height&lt;/code&gt; is 6 or above. If &lt;code&gt;i_am_human&lt;/code&gt; was false, then the program won’t bother to check for the inner conditions, but rather jump to the &lt;code&gt;else&lt;/code&gt; block and output &lt;strong&gt;” I am not a human”&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  The "if … elif … else" Statement
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;if … elif … else&lt;/code&gt; statements solve the problem when there exists more than one condition to be tested. In a more complex program, if a condition fails to be true, the system doesn’t quit immediately. Some other conditions need to be tested before quitting.&lt;br&gt;
The syntax for the &lt;code&gt;if … elif … else&lt;/code&gt; statements is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If &lt;code&gt;&amp;lt;this is true&amp;gt;&lt;/code&gt;:&lt;br&gt;
     &lt;code&gt;&amp;lt;do this&amp;gt;&lt;/code&gt;&lt;br&gt;
elif &lt;code&gt;&amp;lt;the above is not true&amp;gt;&lt;/code&gt;:&lt;br&gt;
    &lt;code&gt;&amp;lt;do this&amp;gt;&lt;/code&gt;&lt;br&gt;
else:&lt;br&gt;
    &lt;code&gt;&amp;lt;do this if none of the above is true&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;elif&lt;/code&gt; is short for &lt;code&gt;else if&lt;/code&gt;. You can have as many &lt;code&gt;elif&lt;/code&gt; as desired in your programs. The last &lt;code&gt;else&lt;/code&gt; is optional. You can choose to add it or leave it out of your programs. Let's see how this works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;I am 17 years old&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;I am 18 years old&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;I am 19 years old&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Age not found&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;## output
# I am 19 years old
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose you change the value of age to 18 as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the output is returned:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;I am 18 years old
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what if the age is not among the ages to be compared. This is where the &lt;code&gt;else&lt;/code&gt; block is executed. When the age from the example above is set to 21, then none of the conditions are satisfied. Only then, the else block is executed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Age not found
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The advent of control structures in programming that allows programmers to write programs that makes decision is big turn around as programmers don't have to programs that only run in sequence anymore. Python took advantage of this structure and implemented it in using selection statements like &lt;em&gt;"if"&lt;/em&gt;, &lt;em&gt;"if ... else"&lt;/em&gt;, and &lt;em&gt;"if ... elif ... else"&lt;/em&gt;.&lt;br&gt;
This article gives a brief overview of all you need to know about the basics of selection statements in Python and how to apply them if your everyday programs.&lt;br&gt;
To know more about selection statements and Python as a whole, I recommend you check out the &lt;a href="https://docs.python.org/3/tutorial/index.html" rel="noopener noreferrer"&gt;Python documentation tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to drop your thoughts and suggestions in the discussion box. I will be available to attend to them. And also, if you have any questions, you can as well drop them in the discussion box.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>python</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Data Types in Python</title>
      <dc:creator>Ibrahim Suleiman</dc:creator>
      <pubDate>Sun, 05 Dec 2021 20:16:34 +0000</pubDate>
      <link>https://forem.com/ibs/data-types-in-python-2mlg</link>
      <guid>https://forem.com/ibs/data-types-in-python-2mlg</guid>
      <description>&lt;p&gt;Have you ever stored some piece of information in the computer and wondered how the computer represents these information? Well, for you it might seems like a bunch of text or a bunch of numbers. But the computer sees this information differently. This is the reason why the computer can perform more rigorous and complex computations than all the human efforts combined. This is made possible through the concept of &lt;strong&gt;”Data Types”&lt;/strong&gt;. &lt;br&gt;
Because the computer knows how to represent this data in the memory, that is why it can manipulate the data so well. A human will out of the box tell you that &lt;code&gt;1 + 1 = 2&lt;/code&gt;, but a computer can’t directly give you the answer without first checking how the expression is represented in the memory. This is where the concept of data types comes to play.&lt;/p&gt;
&lt;h3&gt;
  
  
  Table of Contents
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;What is a data type&lt;/li&gt;
&lt;li&gt;
Basic Data types in Python

&lt;ul&gt;
&lt;li&gt;
Numeric Types

&lt;ul&gt;
&lt;li&gt;Integer&lt;/li&gt;
&lt;li&gt;Float&lt;/li&gt;
&lt;li&gt;Complex&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Boolean Values&lt;/li&gt;
&lt;li&gt;
Sequence Types

&lt;ul&gt;
&lt;li&gt;Strings&lt;/li&gt;
&lt;li&gt;List&lt;/li&gt;
&lt;li&gt;Tuples&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Prerequisite
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Have the latest version of &lt;a href="https://www.python.org/downloads/" rel="noopener noreferrer"&gt;Python&lt;/a&gt; installed on your computer&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  What is a Data Type
&lt;/h3&gt;

&lt;p&gt;A datatype is an attribute of a data which tells the computer how a data is to be used. The data type of a variable defines the meaning of a data, the operations that can be performed on that data, and how the data is stored in the memory.&lt;/p&gt;
&lt;h3&gt;
  
  
  Basic Data Types in Python
&lt;/h3&gt;

&lt;p&gt;Python provides for us a rich set of built-in data types that can be used in programs without creating them from scratch. However, you can also write your own data type in Python to add some of the custom features not included in the ones provided by Python.&lt;br&gt;
This article discuses some of the basic data types in Python.&lt;/p&gt;
&lt;h3&gt;
  
  
  Numeric Types
&lt;/h3&gt;

&lt;p&gt;The three numeric data types python provides are integers, float, and complex.&lt;/p&gt;
&lt;h4&gt;
  
  
  Integer
&lt;/h4&gt;

&lt;p&gt;An integer is a whole number, it can be positive or negative. In Python, there is no limit to how long an integer can be. It can be as long as you want it, except that it is constrained by the memory of your computer. Numbers like -1, 0, 3, 99999999999999999 are a perfect fit for integers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhw18ked7vqsypm5vwtq.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhw18ked7vqsypm5vwtq.PNG" alt="Shell - Integers" width="763" height="236"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Float
&lt;/h4&gt;

&lt;p&gt;A float in Python is any number that has a decimal point. It designates a floating-point number. Floats can be positive or negative numbers. It also doesn't have a limit to how long it can be just like integers. Numbers like -0.1, 0.0, 1.5, 12345.678890000000. Note that floating-point numbers can contain only 1 decimal point. Using more than one decimal point for a floating number will throw a &lt;code&gt;SyntaxError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You could also append the letter &lt;code&gt;e&lt;/code&gt; or &lt;code&gt;E&lt;/code&gt; followed by an integer to represent a scientific notation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ruk30ctz65nhmvdkkvy.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ruk30ctz65nhmvdkkvy.PNG" alt="Python shell" width="800" height="452"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Python represents floating-point numbers with 64-bit double-precision values in almost all platforms. This implies that the maximum value a floating-point number can be is approximately 1.8 ⨉ 10&lt;sup&gt;308&lt;/sup&gt;. Any number greater than this will be indicated with the string &lt;code&gt;inf&lt;/code&gt; implying Infinity.&lt;/p&gt;

&lt;p&gt;Similarly, the minimum value a floating number can be is  5.0 ⨉ 10&lt;sup&gt;-324&lt;/sup&gt;. Any number smaller than this will be effectively zero.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flm95fj263wlmpwlk0vcs.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flm95fj263wlmpwlk0vcs.PNG" alt="Python shell" width="800" height="354"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Complex
&lt;/h4&gt;

&lt;p&gt;A complex number is a number containing a real and imaginary part. A typical complex number looks like &lt;code&gt;a+bj&lt;/code&gt;, where a is the real part and b is the imaginary part. You can create a complex number using the &lt;code&gt;complex()&lt;/code&gt; function. This function takes in two arguments. The first is the real part and the second is the imaginary part.&lt;/p&gt;

&lt;p&gt;You can alternatively create a complex number by just appending a &lt;code&gt;j&lt;/code&gt; to the end of the imaginary part. This will set the real part to zero and the imaginary part to whatever value was specified.&lt;/p&gt;

&lt;p&gt;Complex numbers also provide two attributes that you can use to get the real and imaginary parts of the complex number. If the variable &lt;code&gt;a&lt;/code&gt; is a complex number, then you can access the real and imaginary parts of &lt;code&gt;a&lt;/code&gt; using &lt;code&gt;a.real&lt;/code&gt; and &lt;code&gt;a.imag&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdv6yzs2fqajpgoyx9ih4.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdv6yzs2fqajpgoyx9ih4.PNG" alt="Python shell" width="800" height="409"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Boolean Values
&lt;/h3&gt;

&lt;p&gt;Boolean values in Python are used to represent truth values. They consist of constants &lt;code&gt;True&lt;/code&gt; and &lt;code&gt;False&lt;/code&gt;. Boolean values can also behave like integers when used in numeric concepts, where &lt;code&gt;True&lt;/code&gt; evaluates to 1 and &lt;code&gt;False&lt;/code&gt; evaluates to 0.&lt;br&gt;
Python provides for us the in-built function &lt;code&gt;bool()&lt;/code&gt; which can be used to convert any value to a Boolean. The &lt;code&gt;bool()&lt;/code&gt; function takes in an input and returns the boolean values &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt; depending on the input. By default, the output is &lt;code&gt;True&lt;/code&gt;. It is &lt;code&gt;False&lt;/code&gt; if it falls under any of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every constant returns &lt;code&gt;False&lt;/code&gt;, including &lt;code&gt;None&lt;/code&gt; and &lt;code&gt;False&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The zero value of any numeric data type returns &lt;code&gt;False&lt;/code&gt;. E.g &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;0.0&lt;/code&gt;, &lt;code&gt;0j&lt;/code&gt;, &lt;code&gt;Decimal(0)&lt;/code&gt;, &lt;code&gt;Fraction(0, 1)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Empty sequence returns &lt;code&gt;False&lt;/code&gt;. E.g &lt;code&gt;''&lt;/code&gt;, &lt;code&gt;()&lt;/code&gt;, &lt;code&gt;[]&lt;/code&gt;, &lt;code&gt;{}&lt;/code&gt;, &lt;code&gt;set()&lt;/code&gt;, &lt;code&gt;range(0)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff2l6tab9c8fgxbgswxro.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff2l6tab9c8fgxbgswxro.PNG" alt="Python shell" width="725" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Boolean values are handy when using conditionals and loops such as &lt;code&gt;if … else&lt;/code&gt;, &lt;code&gt;while&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt; in your programs. The truth value of a boolean expression is what determines whether a particular section of code is executed on not as in loops and conditionals.&lt;/p&gt;
&lt;h3&gt;
  
  
  Sequence Types
&lt;/h3&gt;

&lt;p&gt;In Python, a sequence is an ordered collection of data. This data can be similar or different data types. Some of the sequence types in Python include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strings&lt;/li&gt;
&lt;li&gt;List&lt;/li&gt;
&lt;li&gt;Tuples&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Strings
&lt;/h4&gt;

&lt;p&gt;A string is a sequence of letters, space, punctuation marks, or even numbers. It is an immutable sequence of code points. Python handles any form of textual data using the &lt;code&gt;str&lt;/code&gt; object. Unlike integers, strings are represented differently in the computer’s memory that is why they cannot be used to perform arithmetic operations. &lt;/p&gt;

&lt;p&gt;Strings are represented using either single, double, or triple quotes. Python does not have a type for a single character. A single character is a string with a length of 1.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating Strings
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Creating a string with a single quote
&lt;/span&gt;&lt;span class="n"&gt;single&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Hello, World!&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;single&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Creating a string with double-quote
&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hi, i&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;m Prince&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Creating a string with a triple quote
&lt;/span&gt;&lt;span class="n"&gt;triple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
            The conversation went as follows:
            Me: Hello
            Her: Hey
        &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;triple&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;## Output
&lt;/span&gt;
&lt;span class="c1"&gt;# Hello, World!
# Hi, i'm Prince
#             The conversation went as follows:
#             Me: Hello
#             Her: Hey
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Python represents strings in either single, double, or triple quotes. The starting quote of a string must be the same as the end quote. A string cannot start with a single quote and end with a double quote. This will throw a &lt;code&gt;SyntaxError&lt;/code&gt;. But a single quote string can contain a double quote string inside, and a double quote string can contain a single quote string inside, like the &lt;code&gt;double&lt;/code&gt; variable in the above snippet.&lt;/p&gt;

&lt;p&gt;The triple quote string can span multiple lines. Making it possible to write multi-line strings.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accessing the elements of a string&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Python, the elements of a string can be accessed using &lt;code&gt;Indexing&lt;/code&gt;. Indexing allows us to use the index of a string to access it. The first index of a string is 0, the second is 1, and so on. The last index of a string is -1, the second to last is -2, and so on.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Accessing elements of a string
&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello, World!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;# The first element of the string
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;# The last element of a string
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;# IndexError
&lt;/span&gt;

&lt;span class="c1"&gt;## output
&lt;/span&gt;
&lt;span class="c1"&gt;# H
# !
# IndexError: string index out of range
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When accessing string elements using indexing, ensure that the index is within the range of the string. If the index entered is out of range, Python will throw an &lt;code&gt;IndexError&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  List
&lt;/h4&gt;

&lt;p&gt;A list is a mutable sequence of data that is used to store data. List in Python is similar to arrays in other languages, but they don’t have to be of the same type. A list can be created either by using a pair of square brackets containing &lt;code&gt;iterables&lt;/code&gt; or using the type constructor &lt;code&gt;list()&lt;/code&gt; that takes in an &lt;code&gt;iterable&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An &lt;code&gt;iterable&lt;/code&gt; is any sequence or container that can be iterated upon (loop through).&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# List
# create an empty list
&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# list with values of different types
&lt;/span&gt;&lt;span class="n"&gt;mixed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;c&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;d&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mixed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# using the list() function
&lt;/span&gt;&lt;span class="n"&gt;list_function&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mixed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# create a copy of the `mixed` list
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list_function&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;iterable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;tuple_inside_function&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tuple_inside_function&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;## output
&lt;/span&gt;
&lt;span class="c1"&gt;# []
# []
# [1, 2, 3, 4, True, False, 'a', 'b', 'c', 'd']
# [1, 2, 3, 4, True, False, 'a', 'b', 'c', 'd']
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# [1, 2, 3]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A list can either be a single-dimensional or multidimensional list. A single-dimensional list contains a sequence of data. A multidimensional list contains &lt;code&gt;iterable&lt;/code&gt; such as a list. A list containing lists is an example of a multidimensional list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# List dimensions
# single dimensional
&lt;/span&gt;&lt;span class="n"&gt;alphanumeric&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;c&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;z&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alphanumeric&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alphanumeric&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# multidimensional
&lt;/span&gt;&lt;span class="n"&gt;alphanumeric&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;c&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;d&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alphanumeric&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alphanumeric&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;


&lt;span class="c1"&gt;## output
&lt;/span&gt;
&lt;span class="c1"&gt;# ['a', 'b', 'c', 'z', 0, 1, 2, 9]
# 1
# [['a', 'b', 'c', 'd'], [0, 1, 2, 9]]
# c
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The syntax for the multidimensional list is &lt;code&gt;list[a][b]&lt;/code&gt;, where &lt;code&gt;a&lt;/code&gt; is the outer list and &lt;code&gt;b&lt;/code&gt; is the inner list or &lt;code&gt;iterable&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Tuples
&lt;/h4&gt;

&lt;p&gt;Tuples in Python are immutable sequences of data that cannot be modified after they are created. Similar to lists, tuples can contain different data types and the elements are accessed using indexing.&lt;/p&gt;

&lt;p&gt;Tuples are created using the built-in tuple function that takes in an &lt;code&gt;iterable&lt;/code&gt;. They are also created with or without the use of parentheses containing the tuple element. A tuple can also contain a single element, but the element must have a trailing comma for it to be a tuple.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Tuples
# empty tuple
&lt;/span&gt;&lt;span class="n"&gt;empty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# singleton tuple
&lt;/span&gt;&lt;span class="n"&gt;single&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;single&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# tuple with strings
&lt;/span&gt;&lt;span class="n"&gt;string_tuple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string_tuple&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# tuple from list
&lt;/span&gt;&lt;span class="n"&gt;_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;list_tuple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;tuple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list_tuple&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# nested tuple
&lt;/span&gt;&lt;span class="n"&gt;tuple_a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;c&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;tuple_b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;combined&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tuple_a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tuple_b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;combined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;## output
# ()
# (2,)
# ('Hello',)
# (1, 2, 3)
# (('a', 'b', 'c'), (1, 2, 3))
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Python has a bunch of built-in types that you can checkout &lt;a href="https://docs.python.org/3/library/stdtypes.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;. While these types are handy to use and provides basic functionalities, Python also allow you to write your own data structures that can be used as types and add more custom functionalities to it. &lt;br&gt;
This article discussed the basic data types needed to get you started in your Python journey. To know more about Python built-in data types, checkout the &lt;a href="https://docs.python.org/3/library/stdtypes.html" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to drop your thoughts and suggestions in the discussion box. I will be available to attend to them. And also, if you have any questions, you can as well drop them in the discussion box.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Variables in Python</title>
      <dc:creator>Ibrahim Suleiman</dc:creator>
      <pubDate>Sat, 04 Dec 2021 17:13:14 +0000</pubDate>
      <link>https://forem.com/ibs/variables-in-python-3foe</link>
      <guid>https://forem.com/ibs/variables-in-python-3foe</guid>
      <description>&lt;p&gt;A variable in programming is generally a container or location in the computer’s memory used to store values temporarily. Variables in Python are nothing different from the general programming definition of a variable. Values stored in the variable can then later be used or modified in our programs. This article discusses all the basics you need to know in Python programming language.&lt;/p&gt;

&lt;h3&gt;
  
  
  Table of Contents
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;What are variables?&lt;/li&gt;
&lt;li&gt;
Variables in Python

&lt;ul&gt;
&lt;li&gt;Variable Naming convention&lt;/li&gt;
&lt;li&gt;Variable declaration and usage&lt;/li&gt;
&lt;li&gt;Reassigning variables&lt;/li&gt;
&lt;li&gt;Operation on variables&lt;/li&gt;
&lt;li&gt;Variable scope: Local and global variables&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Conclusion&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  What are Variables?
&lt;/h3&gt;

&lt;p&gt;Variable are reserved locations in the memory of the computer used to hold values that can be used later.&lt;br&gt;
Because the computer stores variables in the Random Access Memory (RAM), variables are created in the memory only when our program is in execution and are lost immediately after the program is terminated. What this means is that the value of a variable can only be accessed when a program is running. When the program has finished execution, these variables are not accessible anymore from anywhere in the computer. In order to use them again, you will have to run the program once again.&lt;/p&gt;

&lt;p&gt;If you wish to store and retrieve variable values in your programs, consider using a file or database to store these variables. Variables stored in files and databases are stored in the secondary storage of the computer (e.g. Hard Disk Drive (HDD)) and remains for as long as you want them. They can also be retrieved from the computer’s memory even after shutting down and booting up the computer.&lt;/p&gt;
&lt;h3&gt;
  
  
  Variables in Python
&lt;/h3&gt;

&lt;p&gt;In Python, variables are nothing different from the definition above. Python allows us to create variables and use them in our programs, as well as manipulate the values of these variables. &lt;/p&gt;
&lt;h4&gt;
  
  
  Variable Naming Convention
&lt;/h4&gt;

&lt;p&gt;Below are some of the rules that variables you must abide by when naming variables in Python.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Do not use reserved keywords as a variable name. For example , keywords like class, int, def, while, else, try, e.t.c. &lt;/li&gt;
&lt;li&gt;You cannot use special characters such as @, #, $, %, ^, &amp;amp; , e.t.c. when naming variables&lt;/li&gt;
&lt;li&gt;Variable names are case sensitive. For example, age and Age are two different variables.&lt;/li&gt;
&lt;li&gt;Variable names should start with an alphabet or an underscore(_) character.&lt;/li&gt;
&lt;li&gt;A variable name can only contain the characters A-Z, a-z, 0-9 and underscore(_).&lt;/li&gt;
&lt;li&gt;You cannot start the variable name with a number.&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  Variable Declaration and Usage
&lt;/h4&gt;

&lt;p&gt;Variables in python can be declared and used in different ways. Here, we will be exploring some of the ways variables can be declared and used.&lt;br&gt;
The snippet below declares and used the variable &lt;code&gt;message&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# prints the value of message to the console
&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello, World!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 


&lt;span class="c1"&gt;## output
# Hello, World!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The snippet above is the simplest way you can declare a variable in Python. But wait, what if I need to make my variable names more descriptive?  Python has got your back. According to &lt;a href="https://www.python.org/dev/peps/pep-0008/#id43" rel="noopener noreferrer"&gt;PEP 8 Style guide&lt;/a&gt;, variable names should be lowercase, with words separated by underscores as necessary to improve readability. In a situation where you might want to use long and descriptive variable names, you can simply separate it with an underscore like in the snippet below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# long and descriptive variable names
&lt;/span&gt;&lt;span class="n"&gt;secret_message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hey, here is my secret&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secret_message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;## output
# Hey, here is my secret
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python also allows you to declare variables in a more flexible and dynamic way. Unlike other statically typed programming languages like Java, Python is a dynamically typed language and you don’t need to care much about the type of a variable during declaration. You can declare multiple variables of different types on the same line. Let’s see how this looks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Declare multiple variables in a single line
# doing it the conventional way
&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Prince&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;
&lt;span class="n"&gt;hobby&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Swimming&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Name:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; Age:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; Hobby: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hobby&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# doing it the Python way
&lt;/span&gt;&lt;span class="n"&gt;my_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_hobby&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Prince&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Swimming&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Name:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; Age:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; Hobby: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_hobby&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;## output
# Name: Prince  Age: 12  Hobby:  Swimming
# Name: Prince  Age: 12  Hobby:  Swimming
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Reassigning Variables
&lt;/h4&gt;

&lt;p&gt;Python allows you to reassign variables after they are declared, just like most programming languages allow you to. This process is known as “writing into a variable”. When you reassign a variable, the previous value is lost and the new value is then stored.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Reassigning variables
&lt;/span&gt;&lt;span class="n"&gt;my_age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# reassign the value of my_age
&lt;/span&gt;&lt;span class="n"&gt;my_age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;## output
# 20
# 21
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value of &lt;code&gt;my_age&lt;/code&gt; was initially 20, but after printing it on the console and then reassigning it, the value changed to 21. &lt;/p&gt;

&lt;h4&gt;
  
  
  Operation on Variables
&lt;/h4&gt;

&lt;p&gt;You can also perform some operations on the values of your variables. This is one of the great benefits variables give to us. In a situation where you want to perform operations on a very lengthy value many times in your programs. You don’t need to use that length value everywhere in your program. You can simply store the value with a shorter variable name and use it throughout your program. Let’s try this out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Operations on variables
&lt;/span&gt;
&lt;span class="n"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.142857143&lt;/span&gt;
&lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt;
&lt;span class="n"&gt;perimeter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;perimeter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;## output
# 276.571428584
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can perform any arithmetic operations on numbers in Python. From the snippet above, we can also operate on variables and literals (2 from the code example), provided they are all numbers (either integers or with decimal points). The value 3.142857143  from the code example is long and you might have a hard time holding it in your head. Storing it in PI makes it easily accessible and can be used throughout our programs.&lt;br&gt;
Strings are also not left out here as well as you can perform operations on strings as shown in the code example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# String operations
&lt;/span&gt;&lt;span class="n"&gt;msg1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;msg2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;World&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;full_msg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;msg2&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;full_msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;## output
# HelloWorld
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The most common operation on string is &lt;strong&gt;concatenation&lt;/strong&gt;. This is simply appending one string to the end of the other using the (+) operator. You can only concatenate a string with another string. If you try to do otherwise, Python will throw a &lt;code&gt;TypeError&lt;/code&gt;.&lt;br&gt;
The (*) operator can also be used on strings. But instead of appending to the end of the string, it rather duplicates the string according to the integer value specified.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# String operations
&lt;/span&gt;&lt;span class="n"&gt;fav_language&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Python&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fav_language&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;## output
# PythonPythonPythonPythonPython
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also note that the value specified must be an integer. Using otherwise will also throw a &lt;code&gt;TypeError&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Variable Scope: Local and Global Variables
&lt;/h4&gt;

&lt;p&gt;Variables declared in the programs we have written so far are available throughout the program. This means it can be accessed from anywhere in the program.&lt;br&gt;
But things begin to get complicated when we start writing complex programs that involve functions which carry out a particular task. This is where the concept of &lt;strong&gt;variable scope&lt;/strong&gt; come in.&lt;br&gt;
The scope of a variable is the region of our program where the variable can be accessed. A variable can either have two scopes: Local and Global. &lt;br&gt;
A &lt;code&gt;local&lt;/code&gt; variable is a variable accessible within the function or method it is defined. This is useful when we want to isolate a variable from the remaining part of our program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Variable scope
# local variable 
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;my_real_age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_real_age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# my__real_age is locally available in some_function
&lt;/span&gt;
&lt;span class="nf"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# execute some_function
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_real_age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# my_real_age is not available outside some_function 
&lt;/span&gt;

&lt;span class="c1"&gt;## output
# 19
# NameError: name 'my_real_age' is not defined
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;some_function()&lt;/code&gt; is executed, it prints out the value of &lt;code&gt;my_real_age&lt;/code&gt; to the console because &lt;code&gt;my_real_age&lt;/code&gt; is defined inside the function. But again, when we tried to print &lt;code&gt;my_real_age&lt;/code&gt; outside &lt;code&gt;some_function&lt;/code&gt;, a &lt;code&gt;NameError&lt;/code&gt; was thrown because &lt;code&gt;my_real_age&lt;/code&gt; is only available inside &lt;code&gt;some_function&lt;/code&gt; and not throughout the program. This is because &lt;code&gt;my_real_age&lt;/code&gt; is a local variable.&lt;br&gt;
A &lt;code&gt;global&lt;/code&gt; variable is a variable accessible throughout our program. It remains the same throughout our program and also throughout the module. Use the global variable when you want to use the variable throughout the methods and functions of your program. Let us use the same example from above, but this time making &lt;code&gt;my_real_age&lt;/code&gt; a global variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Variable scope
# global variable
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;global&lt;/span&gt; &lt;span class="n"&gt;my_real_age&lt;/span&gt;
    &lt;span class="n"&gt;my_real_age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt; &lt;span class="c1"&gt;# globally available throughout this program
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_real_age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

&lt;span class="nf"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_real_age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# my_real_age is now available outside the some_function 
&lt;/span&gt;

&lt;span class="c1"&gt;## output
# 19
# 19
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The program above runs successfully without any errors because &lt;code&gt;my_real_age&lt;/code&gt; is now declared as a global variable.&lt;br&gt;
Declaring variables either local or global is useful in cases where you wouldn't want variables to clash in your Python packages and modules. Other programming languages like Java use class variables, instance variables, and local variables for this concept. But it is more simplified with just local and global variables as presented by Python.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Having a good knowledge of variables in any programming language is as important as learning the language itself. This article discusses an overview of variables in Python. It starts with a general view of a variable and then narrows down to the Python view of a variable.&lt;br&gt;
And then discussed the naming convention of variables, declaration and usage, how to reassign variables, the numerous operations you can perform on variables, and finally the scope of a variable.&lt;/p&gt;

&lt;p&gt;Check out the &lt;a href="https://docs.python.org/3/tutorial/index.html" rel="noopener noreferrer"&gt;official Python documentation &lt;/a&gt; to know more about variables.&lt;/p&gt;

&lt;p&gt;Feel free to drop your thoughts and suggestions in the discussion box. I will be available to attend to them. And also, if you have any questions, you can as well drop them in the discussion box.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>writing</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Python Hello World</title>
      <dc:creator>Ibrahim Suleiman</dc:creator>
      <pubDate>Fri, 03 Dec 2021 20:34:05 +0000</pubDate>
      <link>https://forem.com/ibs/python-hello-world-50h6</link>
      <guid>https://forem.com/ibs/python-hello-world-50h6</guid>
      <description>&lt;p&gt;&lt;code&gt;Hello, World!&lt;/code&gt; seems to be the first line to code any programmer would run to test if the coding environment is properly set up. This culture has been a norm for both new and existing developers. As a new developer, or new to a language, you would definitely want your first program to be the famous &lt;strong&gt;“Hello, World!”&lt;/strong&gt; program. &lt;/p&gt;

&lt;p&gt;One reason why this culture is still practiced in the developer’s ecosystem is because the famous &lt;strong&gt;“Hello, World!”&lt;/strong&gt; program is one of the easiest and bug-free programs you would write in any programming language.&lt;/p&gt;

&lt;p&gt;In this article, we would maintain the same culture and write our first &lt;strong&gt;“Hello, World!”&lt;/strong&gt; using the Python programming language. But wait, what the heck is Python? That snake? Well, we’ll find out soon enough.&lt;/p&gt;

&lt;h3&gt;
  
  
  Table of Contents
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Brief history of Python&lt;/li&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Your First Hello World in Python&lt;/li&gt;
&lt;li&gt;Breaking down the Hello World program&lt;/li&gt;
&lt;li&gt;Ways you can write your first Hello World&lt;/li&gt;
&lt;li&gt;Adding comments to the Hello World program&lt;/li&gt;
&lt;li&gt;Advantages of documenting our programs&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Have the latest version of &lt;a href="https://www.python.org/downloads/" rel="noopener noreferrer"&gt;Python&lt;/a&gt; installed on your computer&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Brief History of Python
&lt;/h3&gt;

&lt;p&gt;In the late ’80s, a Dutch programmer was looking for a hobby project to work on during the Christmas break. So he decided to write an interpreter for the new scripting language he had in mind for some while. He chose &lt;strong&gt;&lt;em&gt;“Python”&lt;/em&gt;&lt;/strong&gt; as the title of the project (Being a big fan of “Monty Python Flying Circus”. The old BBC television comedy sketch series). His name is &lt;a href="https://en.wikipedia.org/wiki/Guido_van_Rossum" rel="noopener noreferrer"&gt;Guido van Rossum&lt;/a&gt;. He created the &lt;a href="https://en.wikipedia.org/wiki/Python_(programming_language)" rel="noopener noreferrer"&gt;Python programming language&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Python is a general-purpose, high-level, and object-oriented programming language. It is a widely-used and interpreted language, which makes it more human-readable.&lt;/p&gt;

&lt;p&gt;Python is a good choice for beginners because you can use it to write complex programs in an easy and efficient way. Unlike programming languages like Java that provide support for object-oriented paradigm, Python has support for many programming paradigms. Some of the paradigms include functional, object-oriented, and procedural (structured) programming.&lt;/p&gt;

&lt;h3&gt;
  
  
  Your First “Hello, World!” in Python
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello, World!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yay! You just wrote your first &lt;strong&gt;“Hello, World!”&lt;/strong&gt; in Python. While this looks extremely easy and straightforward, it is not the case with other programming languages like C# and Java. Any beginner would be able to relate to the program above because it looks like writing plain English to the computer. This is one of the advantages Python provides over other object-oriented languages.&lt;/p&gt;

&lt;h3&gt;
  
  
  Breaking Down The “Hello, World!” Program
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;“Hello, World!”&lt;/strong&gt; above seems easy and straightforward, but I bet you will still need an explanation for the syntax and how things are the way they are.&lt;/p&gt;

&lt;p&gt;The program above instructs the python interpreter to print the string &lt;strong&gt;“Hello, World!”&lt;/strong&gt; to the console. The &lt;code&gt;print()&lt;/code&gt; function is a built-in function (it comes installed with Python) that takes in a couple of arguments and prints it out to the console. The &lt;code&gt;print()&lt;/code&gt; function is one of the many built-in functions that come installed with python, and it is made available during execution.&lt;/p&gt;

&lt;p&gt;In Python, functions are invoked using parentheses immediately after the function name. If the function takes in arguments, then these arguments are passed into the parenthesis -- just like our &lt;code&gt;print()&lt;/code&gt; function above.&lt;/p&gt;

&lt;p&gt;The print function does not only take in strings such as &lt;strong&gt;“Hello, World!”&lt;/strong&gt; above. It can take in as an argument any Python data type which can be numbers or boolean values and prints it to the console. It can also take in variables and print them to the console as well.&lt;/p&gt;

&lt;p&gt;Note that the &lt;strong&gt;“Hello, World!”&lt;/strong&gt; is enclosed in double-quotes. Python is flexible enough to let you print the same &lt;strong&gt;“Hello, World!”&lt;/strong&gt; in a single quote.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Hello, World!&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This flexibility is also what makes it popular among beginners, as you don’t need to bother about which to use when writing simple programs. Unlike Java that uses a single quote for a character and a double quote for strings, any of both can be used in Python for simple programs like the &lt;strong&gt;“Hello, World!”&lt;/strong&gt; above (except for some special cases).&lt;/p&gt;

&lt;h3&gt;
  
  
  Ways You Can Write Your First “Hello, World!”
&lt;/h3&gt;

&lt;p&gt;In the developer’s ecosystem, there are usually many ways of writing programs and solving problems as there are many ways that lead to the river. We will be exploring some of those ways in this section of the article.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Using the Python Shell&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Go to your terminal and type the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If python is properly installed on your machine, it will open the Python shell as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5hz0s3z255zt62c88ynp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5hz0s3z255zt62c88ynp.png" alt="Using Python Shell 1" width="568" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then go ahead and write your &lt;strong&gt;“Hello, World!”&lt;/strong&gt; program on the prompt as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9410bm54crv6m7dc15v8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9410bm54crv6m7dc15v8.png" alt="Using Python Shell 2" width="569" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Using the Python IDLE&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Locate the Windows icon on your taskbar (If you are using a Windows OS), select &lt;strong&gt;&lt;em&gt;“All apps”&lt;/em&gt;&lt;/strong&gt;, and then search for the Python as shown below. If you have different versions of Python installed,  they will all pop up here. Select one of them as shown below. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdwgslw481domrabycz2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdwgslw481domrabycz2.jpg" alt="Using IDLE 1" width="256" height="183"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the &lt;strong&gt;“IDLE”&lt;/strong&gt; app from the dropdown and a new window will pop up on the screen as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6e73p7lcchc7b7vwymj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6e73p7lcchc7b7vwymj.png" alt="Using IDLE 2" width="625" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then run your &lt;strong&gt;“Hello, World!”&lt;/strong&gt; code and see the output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3tbm24z4mnodads04r43.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3tbm24z4mnodads04r43.png" alt="Using IDLE 3" width="634" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Python IDLE allows you to run python codes on the Python shell. It behaves similar to the method above. The main difference is that you don’t have to type in the &lt;code&gt;python&lt;/code&gt; command before activating it. And also, the IDLE looks more aesthetically pleasing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Writing Python codes in a file&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One of the limitations of using the methods mentioned above is that it is not flexible to write multiple lines of python codes on either the Python shell or the Python IDLE. &lt;br&gt;
Python provides us a way around this by creating a file directly from the IDLE and writing your code in it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp3gv7pdq4ly5xbd58d3r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp3gv7pdq4ly5xbd58d3r.png" alt="using file 1" width="638" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Locate and click on &lt;strong&gt;&lt;em&gt;“File”&lt;/em&gt;&lt;/strong&gt; from the tabs above. Then select &lt;strong&gt;&lt;em&gt;“New file”&lt;/em&gt;&lt;/strong&gt; which will open up a new window to write your Python codes. Alternatively, you can enter &lt;strong&gt;“Ctrl + N”&lt;/strong&gt; on the keyboard which is a shortcut for opening a new window for your Python code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F28eulos3aa4n5grmfh9v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F28eulos3aa4n5grmfh9v.png" alt="using file 2" width="582" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Write your &lt;strong&gt;“Hello, World!”&lt;/strong&gt; code in the editor provided and save it with a suitable name, and in a suitable location where you can easily find it. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx03mpeaqlb4wd17clyue.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx03mpeaqlb4wd17clyue.png" alt="using file 3" width="535" height="162"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Locate and click on the &lt;strong&gt;&lt;em&gt;“Run”&lt;/em&gt;&lt;/strong&gt; button from the tabs above and select &lt;strong&gt;&lt;em&gt;“Run Module”&lt;/em&gt;&lt;/strong&gt; when the dropdown comes. Alternatively, you can use the shortcut key - &lt;strong&gt;&lt;em&gt;F5&lt;/em&gt;&lt;/strong&gt; to run the program directly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgiicy2dcioqqcgnim7se.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgiicy2dcioqqcgnim7se.png" alt="using file 4" width="765" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above methods are the most fundamental ways you can write and run your Python programs without having to install any code editors. All of the tools used above came bundled with Python, so you don't have to download them anymore. When you get more comfortable with the methods above, then try and use other third-party code editors like &lt;em&gt;PyCharm&lt;/em&gt;, &lt;em&gt;VSCode&lt;/em&gt;, &lt;em&gt;Atom&lt;/em&gt;, &lt;em&gt;Sublime&lt;/em&gt;, e.t.c.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding Comments To The “Hello, World!” Program
&lt;/h3&gt;

&lt;p&gt;Python allows us to add comments to our program which is a way of reminding us of what the program does, in case we will ever go through it in the future. Let us add a comment to our &lt;strong&gt;“Hello, World!”&lt;/strong&gt; program describing what it does.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fglxnhnxozxzuznx5cwz6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fglxnhnxozxzuznx5cwz6.png" alt="Adding comments" width="559" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Python allows us to use the pound/number (&lt;strong&gt;#&lt;/strong&gt;) sign in order to write comments in our code. When we use the pound sign in front of a statement, the Python compiler completely ignores anything after the pound sign, and then continues on the next line. There are several types of comments in Python but we will be considering the simplest one for our &lt;strong&gt;“Hello, World!”&lt;/strong&gt; program.&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages Of Documenting Our Programs
&lt;/h3&gt;

&lt;p&gt;Writing comments in your program is a means of documenting the program. This is one of the best practices every programmer is advised to stick to. Some advantages of writing comments includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Clarity:&lt;/strong&gt; When you need to make reference to your code writing some time ago and you can’t remember what the program actually does. The comments that are written along with the program can give you a hint of what each function or statement does. This is particularly useful in complex and large programs containing a bunch of functions and packages. With the help of the comments, it will be easy to map out what the program does.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; The fact that you can still understand and remember what your code does after a long time doesn't mean everybody can. In a case where you are deploying a package to be used by other developers, they might get stuck at some point and attempt to check the codebase. Without proper comment or description of what each code does, it will be hard for the programmers to identify where the problem is thereby discouraging them from using the package any further. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;OSS:&lt;/strong&gt; Open Source Software &lt;strong&gt;(OSS)&lt;/strong&gt; is software that anybody can modify because it is &lt;strong&gt;“Open”&lt;/strong&gt; and publicly accessible to everyone. Python is an example of an OSS that anyone can contribute to. OSS is mostly developed by programmers all over the world. Because of this diversity, confusion might come up because the codebase is written by different sets of people in different locations and at different points in time. These people might not (or never get to) know each other. This problem is solved by proper documentation of a project. Adding comments to programs is a way of documenting a program for future reference. This is the reason why many robust open-source software like Python is still in existence today.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Knowing the history and basics of a programming language is a very important as leaning the language itself. &lt;br&gt;
This article gives us a high level overview of the basics of the Python programming language, its creator, history, and how the name came about. It then moved on to breakdown the famous "Hello, World!" using Python. Lastly, it branches through the writing comments and its importance the programming.&lt;br&gt;
While this article covers some of the basics of Python, check out the &lt;a href="https://docs.python.org/3/tutorial/index.html" rel="noopener noreferrer"&gt;official Python documentation&lt;/a&gt; to know more about Python.&lt;/p&gt;

&lt;p&gt;Feel free to drop your thought and suggestions in the discussion box.  I will be available to attend to them. And also, if you have any questions, you can as well drop  them in the discussion box. &lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>writing</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>OOP Series: Abstraction and Interface In Java</title>
      <dc:creator>Ibrahim Suleiman</dc:creator>
      <pubDate>Tue, 09 Nov 2021 07:19:01 +0000</pubDate>
      <link>https://forem.com/ibs/oop-series-abstraction-and-interface-in-java-104g</link>
      <guid>https://forem.com/ibs/oop-series-abstraction-and-interface-in-java-104g</guid>
      <description>&lt;h2&gt;
  
  
  INTRODUCTION
&lt;/h2&gt;

&lt;p&gt;Abstraction is one of the four basic principles of Object-Oriented Programming (OOP). It enables us to give structure to our programs and applications. Abstract classes are used to create structure or design in which all other subclasses must follow. &lt;br&gt;
Consider a smartphone, for example, smartphones have a screen that the user can touch to interact with the phone. Other properties of the smartphone are camera, speaker, battery, earphones, buttons, etc., and a smartphone can do some things like play music, make a call, receive a call, take pictures/ videos, etc. Considering some models of phones like Android, iOS, Blackberry, etc. An android phone, for instance, must at least be able to make a call, receive a call and browse the internet before we can consider it a smartphone. &lt;br&gt;
For us to consider Android, iOS, and Blackberry in the class of phones, they must also be able to perform the basic functionalities listed above. Then can we conclude that we have abstracted Android and iOS from the Phone.&lt;/p&gt;
&lt;h2&gt;
  
  
  CONTENT
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Prerequisite&lt;/li&gt;
&lt;li&gt;Abstraction&lt;/li&gt;
&lt;li&gt;Abstract class and Concrete class&lt;/li&gt;
&lt;li&gt;Abstraction vs Encapsulation&lt;/li&gt;
&lt;li&gt;Code Example&lt;/li&gt;
&lt;li&gt;Interface &lt;/li&gt;
&lt;li&gt;Properties of Interface&lt;/li&gt;
&lt;li&gt;Code example&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  PREREQUISITE
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic understanding of OOP - Methods, Classes and Objects&lt;/li&gt;
&lt;li&gt;Some OOP principles - &lt;a href="https://dev.to/princeibs/oop-series-inheritance-in-java-4bn"&gt;Inheritance&lt;/a&gt;, &lt;a href="https://dev.to/princeibs/oop-series-encapsulation-in-java-1n51"&gt;Encapsulation&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  ABSTRACTION
&lt;/h2&gt;

&lt;p&gt;Abstraction in Java is the process of creating abstract classes and methods which define the structure of subclasses. We can achieve abstraction in Java by adding the keyword &lt;code&gt;abstract&lt;/code&gt; to a class declaration name and providing abstract methods to the class. &lt;br&gt;
It is also achieved using interfaces and abstract classes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An abstract method is an instance method that is declared with the keyword &lt;em&gt;abstract&lt;/em&gt; and does not have a body i.e. it is delimited by a semicolon.&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;testMethod&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below are some of the properties of an abstract class: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You must declare it with the keyword &lt;code&gt;abstract&lt;/code&gt;. Classes declared without the abstract keyword but containing at least one abstract method will throw a compilation error.&lt;/li&gt;
&lt;li&gt;It cannot be instantiated. We cannot create objects of the class using the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/li&gt;
&lt;li&gt;All subclasses inheriting from the abstract class must provide an implementation for the superclass abstract methods.&lt;/li&gt;
&lt;li&gt;It can be sub-classed. Abstract classes can also act as a subclass and inherit the properties of a superclass.&lt;/li&gt;
&lt;li&gt;It can contain zero or more abstract methods.&lt;/li&gt;
&lt;li&gt;It can contain concrete methods. Concrete methods are methods with a body and implementation, unlike abstract methods that are delimited by a semicolon and do not provide an implementation.&lt;/li&gt;
&lt;li&gt;All other members of the abstract class like instance variables and concrete methods continue to abide by the rules of inheritance. Subclasses handle them as they will for other concrete classes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Abstract class and Concrete class
&lt;/h2&gt;

&lt;p&gt;An abstract class is any class declared with the keyword abstract and contains some abstract methods. Some of the properties of the abstract class are listed above. A concrete class is somewhat opposite to an abstract class. A concrete class is a class that is not declared with the keyword abstract and does not contain any abstract method. While an abstract class cannot be instantiated, a concrete class can be instantiated. &lt;br&gt;
Meanwhile, a concrete subclass inheriting from an abstract superclass must implement all of the superclass abstract methods otherwise the subclass must also be declared as an abstract class.&lt;/p&gt;
&lt;h2&gt;
  
  
  Abstraction Vs Encapsulation
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;You can achieve encapsulation without abstraction but cannot achieve abstraction without encapsulation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Abstraction and encapsulation are two of the most misused concepts of OOP. Abstraction focuses on the design of the object in focus, while encapsulation focuses on the implementation. &lt;br&gt;
The key to understanding the difference between the two is&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Abstract all the things you need&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Encapsulate all the things you don’t need.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Consider a car, for example, some of the basic necessities of a car are the steering wheel, accelerator pedal, brake pedal, gear selector, etc. Since a car needs all these things to be classified as a car, we can conclude that these things are being abstracted from the car. This is because a car can't do without them. &lt;br&gt;
But the functionalities of these parts of the car are not made visible to the driver and the driver does not need to know how the steering wheel rotates the tire, or how the accelerator and brake pedal controls the speed of the car. This is known as encapsulation. The implementation and the details of these parts are hidden from the driver so the driver only interacts with the few parts made available by the car like the parts listed above. In this case, we are hiding the complexities from the driver, which is encapsulation.&lt;/p&gt;
&lt;h2&gt;
  
  
  Code Example
&lt;/h2&gt;

&lt;p&gt;The three programs below explain the concept of abstraction in Java using three classes. The &lt;code&gt;Phone&lt;/code&gt; abstract class and two other concrete classes - the &lt;code&gt;Android&lt;/code&gt; class and &lt;code&gt;IOS&lt;/code&gt; class.&lt;/p&gt;
&lt;h3&gt;
  
  
  The Phone Class
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foguu8qbq2o6b49ydwsqt.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foguu8qbq2o6b49ydwsqt.JPG" alt="Phone class" width="761" height="477"&gt;&lt;/a&gt;&lt;br&gt;
The image depicts the Phone class hierarchy. The Phone class is blurred in the image to signify it is an abstract class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;Abstraction&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Phone&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// initialize instance variables&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Phone&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// set and get methods&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setColor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getColor&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setBrand&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getBrand&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// abstract methods&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;makeCall&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;receiveCall&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;browseInternet&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// overrides Object's method&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s specs"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;getBrand&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Phone class starts by adding an abstract class declaration in its signature with the keyword &lt;code&gt;abstract&lt;/code&gt;. This is the default syntax for declaring a class as abstract in Java. &lt;br&gt;
The class moves on to declare its instance variables and then to the constructor used to initialize these variables, followed by setters and getters methods used to access and modify these instance variables.&lt;br&gt;
The next set of methods includes the abstract methods &lt;code&gt;makeCall()&lt;/code&gt;, &lt;code&gt;receiveCall()&lt;/code&gt;, and &lt;code&gt;browseInternet()&lt;/code&gt; that demonstrates the &lt;em&gt;must have&lt;/em&gt; of a smartphone. This is similar to the smartphones we use in real life, most smartphones perform these three basic functionalities. Every other type of phone inheriting the properties of our Phone class must be able to make a call, receive a call, and browse the internet. The implementation of these methods might vary, but functionality must be the same.&lt;br&gt;
One thing to note here is that the Phone class contains concrete methods with implementations along with its abstract methods.&lt;br&gt;
The next two classes will be inheriting these properties of the Phone class.&lt;/p&gt;
&lt;h3&gt;
  
  
  The Android Class
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;Abstraction&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Android&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Phone&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// android version&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Android&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// initialize superclass variables&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// initialize instance variable&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// set and get methods&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setVersion&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getVersion&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// abstract methods implementation&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;makeCall&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Calling with Android ..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Call ended."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;receiveCall&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Calling with Android ..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Call ended."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;browseInternet&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Browsing with iPhone ..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Session ended."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s%nVersion: %s%nColor: %s%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;getVersion&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getColor&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The Android class like any other regular subclass class inherits from a superclass which is the Phone class using the keyword &lt;code&gt;extends&lt;/code&gt; (similar to inheritance) in its class declaration. It includes an instance variable and a constructor to initialize the variable, then the setter and getter method to manipulate the variable. &lt;br&gt;
Our focus here are the methods &lt;code&gt;makeCall()&lt;/code&gt;, &lt;code&gt;receiveCall()&lt;/code&gt;, and &lt;code&gt;browseInternet()&lt;/code&gt;. They override the superclass methods using the &lt;code&gt;@Override&lt;/code&gt; annotation. Here, the Android class must override these abstract methods and provide implementation (a body) in order to be called a concrete method. This is because the Phone class it is inheriting from contains these abstract methods.&lt;br&gt;
This is very useful in designing smartphones in real life where each smartphone has a basic requirement they must meet. Because of these design constraints, users won't be overwhelmed by the variations of smartphone design each brand is producing.&lt;/p&gt;
&lt;h3&gt;
  
  
  The IOS Class
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;Abstraction&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;IOS&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Phone&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;camera&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// number of camera&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;IOS&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;camera&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// initialize superclass variables&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;camera&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;camera&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// initialize instance variable&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// set and get methods&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setCamera&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;camera&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;camera&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;camera&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// get the number of camera&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getCamera&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;camera&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// abstract methods implementation&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;makeCall&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Calling with iPhone ..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Call ended."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;receiveCall&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Receiving call with iPhone ..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Call ended."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;browseInternet&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Browsing with iPhone ..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Session ended."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// override Object's method&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s%nColor: %s%nNumber of Camera: %d%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getColor&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;getCamera&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The IOS class above is similar in structure to the Android class. It contains an instance variable, a constructor to initialize the instance variable, and a setter and getter method to manipulate the instance variable. &lt;br&gt;
The main difference between the Android class and the IOS class is the implementation of the inherited abstract methods. In this case, both have similar methods and prints different message which in real life it might be some block of code with actual functionalities. But for the sake of this article, we are keeping things simple by printing some text to the console. Now, if a user is to transition from an IOS to an Android device, they won't be much difference in the functionalities of the two devices. Because from the onset, both share the same design. This is another good advantage of abstraction in the real world.&lt;/p&gt;
&lt;h2&gt;
  
  
  The PhoneTest Class
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;Abstraction&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PhoneTest&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="c1"&gt;// create objects&lt;/span&gt;
        &lt;span class="nc"&gt;Android&lt;/span&gt; &lt;span class="n"&gt;android&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Android&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Android"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Diamond Black"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"8.9.9"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="no"&gt;IOS&lt;/span&gt; &lt;span class="n"&gt;ios&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;IOS&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"iOS"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Pale Blue"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// display objects&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;android&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ios&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// perform action with objects&lt;/span&gt;
        &lt;span class="n"&gt;android&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;makeCall&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"--------------------------------------"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// white space&lt;/span&gt;
        &lt;span class="n"&gt;ios&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;receiveCall&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We use the &lt;code&gt;PhoneTest&lt;/code&gt; Class above to test the implementation of the three classes created above - &lt;code&gt;Phone&lt;/code&gt;, &lt;code&gt;Android&lt;/code&gt;, and &lt;code&gt;IOS&lt;/code&gt; class.&lt;br&gt;
It starts by creating objects of both the Android and IOS classes and initializing their instance variables by passing the values into their constructor call.&lt;br&gt;
The next set of statements display the string representation of the respective objects created above. Note that we printed the objects without any helper methods but rather with the help of the &lt;code&gt;toString()&lt;/code&gt; method overridden inside each class. It helps to display objects of that class in a custom way.&lt;br&gt;
The next statements perform some actions with the objects by making a call with the &lt;code&gt;android&lt;/code&gt; object and then receiving a call with the &lt;code&gt;ios&lt;/code&gt; object, and the implementation of each method is printed in the console.&lt;/p&gt;

&lt;p&gt;Below is the output of executing the PhoneTest class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpux5959dqsj435h8ox51.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpux5959dqsj435h8ox51.JPG" alt="PhoneTest output - abstraction" width="616" height="325"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  INTERFACE
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Interface specifies &lt;em&gt;what&lt;/em&gt; to do and not &lt;em&gt;how&lt;/em&gt; to do it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An interface in Java is a type that contains static constants and abstract methods and defines the behavior of classes. Interface is an extreme case of abstraction and is used to achieve 100% abstraction. This implies that interfaces cannot contain concrete methods and all the abstract methods it contains must be implemented by the concrete subclass. &lt;br&gt;
Interface comes into play when classes that are not related need to share a common design. This happens when we need objects of different classes to respond to the same method call. &lt;br&gt;
Interface in Java acts as an &lt;em&gt;interface&lt;/em&gt; between different classes sharing a common design. Consider the TV remote control, for example, most remote control contains at least one button, so if a particular model/brand of remote control is to implement the remote interface, it must contain at least one button because the default design of a remote includes at least one button.&lt;/p&gt;
&lt;h2&gt;
  
  
  Properties of Interface
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Similar to abstract classes, interfaces cannot be instantiated. An object of an interface cannot be created using the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/li&gt;
&lt;li&gt;All fields in an interface are implicitly public, static, and final.&lt;/li&gt;
&lt;li&gt;Interface cannot have concrete methods.&lt;/li&gt;
&lt;li&gt;Interface cannot extend other classes.&lt;/li&gt;
&lt;li&gt;All methods in an interface are implicitly public and abstract methods. &lt;/li&gt;
&lt;li&gt;A class must implement all of the methods of an interface, otherwise it must be declared abstract.&lt;/li&gt;
&lt;li&gt;Interface can extend (but cannot implement) other interfaces.&lt;/li&gt;
&lt;li&gt;A class can implement multiple interfaces. This is done using a comma-separated list of interfaces after the keyword &lt;code&gt;implements&lt;/code&gt; in the class declaration.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;public class &lt;em&gt;&lt;code&gt;className&lt;/code&gt;&lt;/em&gt; extends &lt;em&gt;&lt;code&gt;superClassName&lt;/code&gt;&lt;/em&gt; implements &lt;em&gt;&lt;code&gt;interface1&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;interface2&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;interface3&lt;/code&gt;&lt;/em&gt;, ...&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Code Example
&lt;/h2&gt;

&lt;p&gt;The code snippets below explain the concept of interfaces in Java.&lt;/p&gt;
&lt;h3&gt;
  
  
  The Camera Class
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;Abstraction&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Camera&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;takePicture&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;flashlight&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;       

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The program above contains an interface &lt;code&gt;Camera&lt;/code&gt; that has only two methods. As seen from the program, the class declaration includes the keyword &lt;code&gt;interface&lt;/code&gt; they are no concrete methods here because interface in Java does not accept it. The methods that this interface has included some of the basic functionalities of a camera. So any class that wants to implement a Camera interface must be able to take a picture and includes a flashlight (at least).&lt;/p&gt;
&lt;h3&gt;
  
  
  Modification to the Android Class
&lt;/h3&gt;

&lt;p&gt;The two snippets below modify the Android class to implement the Camera interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Android&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Phone&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Camera&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The snippet above implements the Camera interface using the Android class by adding the keyword &lt;code&gt;implements&lt;/code&gt; followed by the name of the interface to implement, in this case; Camera is the interface to implement. This is the default syntax for implementing an interface in Java. In real life, for an Android phone to have a camera interface, it must be able to take pictures along with a flashlight, this is exactly what our Camera class is doing here - enforcing the Camera design on Android.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// overridden interface methods&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt; 
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;takePicture&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Taking picture ..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Picture taken"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;flashlight&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Flashlight is turned on ..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Flashlight is now off"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The snippet above shows another modification done to the Android class. All other parts of the program remain the same except for these two methods added to the class because of the interface the class is implementing. &lt;br&gt;
The two methods above provide implementation to the &lt;code&gt;takePicture()&lt;/code&gt; and &lt;code&gt;flashlight()&lt;/code&gt; methods in the Camera interface. As seen from the properties of an interface, &lt;strong&gt;all&lt;/strong&gt; of the methods must be implemented in the Android class else the compiler will throw an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="n"&gt;android&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;flashlight&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above line of code was added to the phone test class to demonstrate one of the methods the Android class is implementing.&lt;/p&gt;

&lt;p&gt;Below is an output of running the PhoneTest class with the modifications done to the android class.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft3e6mijxhl4zl77k3zec.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft3e6mijxhl4zl77k3zec.JPG" alt="PhoneTest output - interface" width="639" height="373"&gt;&lt;/a&gt;&lt;br&gt;
The output above remains similar to the first output except for the last two lines that display the output of implementing the &lt;code&gt;flashlight()&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This article starts by introducing the concept of abstraction in Java and its properties as well. The knowledge of Abstraction is a very valuable concept in OOP and programming as a whole because of its numerous advantages and applications in the real world.&lt;/p&gt;

&lt;p&gt;Interface, on the other hand, is an extreme case of abstraction. Most software engineers encourage the use of interface as it helps to preserve design and also helps in modeling software.&lt;br&gt;
This article briefly introduces the concept of abstraction and interface in Java. Visit the &lt;a href="https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html" rel="noopener noreferrer"&gt;official Java documentation&lt;/a&gt; to know more about the concepts discussed here.&lt;/p&gt;

</description>
      <category>java</category>
      <category>oop</category>
      <category>beginners</category>
    </item>
    <item>
      <title>OOP Series: Encapsulation In Java</title>
      <dc:creator>Ibrahim Suleiman</dc:creator>
      <pubDate>Fri, 15 Oct 2021 23:18:35 +0000</pubDate>
      <link>https://forem.com/ibs/oop-series-encapsulation-in-java-1n51</link>
      <guid>https://forem.com/ibs/oop-series-encapsulation-in-java-1n51</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Encapsulation is one of the four core principles of Object-Oriented Programming (OOP). It is also known as information hiding. Encapsulation refers to the packaging or bundling of data along with the methods that manipulate this data, thereby restricting direct external access to the data. These data are usually in form of variables with private access declared inside of a class, and a &lt;em&gt;setters&lt;/em&gt; and &lt;em&gt;getters&lt;/em&gt; method that can be used to update and retrieve the values of the variables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg9zkw3m8qzgz4jxku3v6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg9zkw3m8qzgz4jxku3v6.jpg" alt="Capsule image" width="800" height="533"&gt;&lt;/a&gt;&lt;br&gt;
An example of encapsulation can be seen from a medicine capsule. Here, the capsule protects its contents from direct external access. Before taking a capsule, we mostly don't open it to look at its content, but rather take the whole of it irrespective of the fact that we don't know what's inside and then expect it to start its functionalities. In this case, it is safe to say that the capsule has encapsulated its content from us. This is the same thing classes do; they protect their variables from external or clients access. In this case, the clients cannot manipulate this information because they don’t have direct access to them, and they cannot as well see the internal workings or implementations of objects created from that class.&lt;/p&gt;
&lt;h3&gt;
  
  
  Contents
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Background Knowledge&lt;/li&gt;
&lt;li&gt;Advantages of using Encapsulation&lt;/li&gt;
&lt;li&gt;The Account class&lt;/li&gt;
&lt;li&gt;The AccountTest class&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Expectations
&lt;/h3&gt;

&lt;p&gt;By the end of this article, you will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Have a high-level knowledge of what encapsulation is.&lt;/li&gt;
&lt;li&gt;Know some of the advantages of using encapsulation.&lt;/li&gt;
&lt;li&gt;Write simple Java applications that apply the concept of encapsulation.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Background Knowledge
&lt;/h3&gt;

&lt;p&gt;This article gives a high-level overview of one of the core principles of OOP. You are expected to have basic knowledge of OOP including: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Objects &lt;/li&gt;
&lt;li&gt;Classes and methods&lt;/li&gt;
&lt;li&gt;Inheritance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Please check out &lt;a href="https://dev.to/princeibs/oop-series-inheritance-in-java-4bn"&gt;Inheritance in Java&lt;/a&gt; for an article on inheritance.&lt;/p&gt;
&lt;h3&gt;
  
  
  Advantages of Using Encapsulation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Data hiding: The use of private access modifiers in fields declaration protects that field in the class from an external source. Therefore, they can be used to store important information without the fear of information leakage.&lt;/li&gt;
&lt;li&gt;Control: The use of setters and getters methods to access private fields in a class gives more control over that variable. Suppose the &lt;code&gt;setBalance&lt;/code&gt; method in an Account class processes only positive values and set negative values to zero, and a user wants to set an account balance to a negative value, because of the validation done in this method, zero will be stored in the balance instead of the negative value.&lt;/li&gt;
&lt;li&gt;Reduces Exceptions: Suppose a user enters their age in a name field, the program is supposed to throw an exception because a &lt;code&gt;String&lt;/code&gt; value is expected but it found an &lt;code&gt;int&lt;/code&gt; value. With encapsulation, this can be avoided by validating the input using a simple &lt;em&gt;if-else&lt;/em&gt; statement of regular expression and handling the exception if one happens instead of terminating the program due to exceptions.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  The Account Class
&lt;/h3&gt;

&lt;p&gt;The account class depicts a bank account containing information related to the account. This class is used to demonstrate how encapsulation works and its benefits as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;Encapsulation&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Account&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;   

    &lt;span class="c1"&gt;// set and get methods&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setFirstName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getFirstName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setLastName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getLastName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setBalance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// handles invalid balance&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; 
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getBalance&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s%n%s: %s%n%s: %s%n%s: %.2f%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
                &lt;span class="s"&gt;"Account Info"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"First Name"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;getFirstName&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;
                &lt;span class="s"&gt;"Last Name"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;getLastName&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="s"&gt;"Balance"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;getBalance&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The account class above declared all of its instance fields private thereby restricting access of these fields to the class alone.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, even objects of the class can’t access these private fields because they are &lt;em&gt;encapsulated&lt;/em&gt;. They can only be accessed through the public get and set methods declared inside of the class. In a more robust system, the set methods usually have complex validations that external values must pass through before setting them but for the sake of this article and simplicity, we will be using an if-else statement to validate the balance entered by the user.&lt;br&gt;
The &lt;code&gt;setBalance&lt;/code&gt; method first checks if the balance is a positive number (as there are no negative balance in most cases), similar to the deposit method. But the deposit method adds the deposit amount to the previous balance only if it is valid (deposit amount is mostly a positive value).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setBalance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// handles invalid balance&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; 
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a situation where the accountant mistakenly enters a negative amount for deposit, normally the program would have subtracted it from the previous balance left but because of encapsulation, this is not the case because an implementation that handles this kind of subtle error has been added to the program.&lt;br&gt;
Another implementation in the Account class is the &lt;code&gt;toString&lt;/code&gt; method that displays the string representation of an object of that class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s%n%s: %s%n%s: %s%n%s: %,.2f%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
                &lt;span class="s"&gt;"Account Info"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"First Name"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;getFirstName&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;
                &lt;span class="s"&gt;"Last Name"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;getLastName&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="s"&gt;"Balance"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;getBalance&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method overrides the default built-in &lt;code&gt;toString&lt;/code&gt; method inherited from the &lt;code&gt;Object&lt;/code&gt; class and adds implementation to it. The advantages of this will be seen later in this article.&lt;/p&gt;

&lt;h3&gt;
  
  
  The AccountTest Class
&lt;/h3&gt;

&lt;p&gt;AccountTest class contains an object of the Account class and it is used to test the functionalities of the Account class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;Encapsulation&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AccountTest&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;    
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;clerk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Account&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;clerk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setFirstName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Clark"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;clerk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setLastName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Tom"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;clerk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setBalance&lt;/span&gt;&lt;span class="o"&gt;(-&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Before depositing&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clerk&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// After depositing invalid amount&lt;/span&gt;
        &lt;span class="n"&gt;clerk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;deposit&lt;/span&gt;&lt;span class="o"&gt;(-&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"AFter depositing invalid amount %nBalance: %,.2f%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;clerk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBalance&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

        &lt;span class="c1"&gt;// After depositing valid amount&lt;/span&gt;
        &lt;span class="n"&gt;clerk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;deposit&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1500&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"AFter depositing valid amount %nBalance: %,.2f%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;clerk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBalance&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main method of the AccountTest class starts by creating an object of the Account class and then using the &lt;em&gt;set&lt;/em&gt; methods to initialize the values of the object. &lt;br&gt;
The image below shows all of the properties of the Account class. As it is seen from the image below; the first name, last name, and account balance are not visible to the user because they are declared private. They can only be accessed through the get methods declared public.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7vbrfbpacxatwcv3rxb4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7vbrfbpacxatwcv3rxb4.jpg" alt="Account object methods" width="800" height="519"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After setting the name, the object tries to set a negative amount as the account balance&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;clerk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setBalance&lt;/span&gt;&lt;span class="o"&gt;(-&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but recall that the Account class automatically sets the negative amount received to 0.0 in the account balance. Encapsulation is seen at work here because we have successfully added some implementation to the &lt;code&gt;setBalance&lt;/code&gt; method which is not visible to the users. &lt;br&gt;
Also, the &lt;code&gt;clerk&lt;/code&gt; object is displayed on the console using its object variable alone.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clerk&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
Notice we didn’t call any method to help us properly display the object but it's variable alone, this is because the implementation for that is already handled as well in the overridden &lt;code&gt;toString&lt;/code&gt; method in the Account class. The user doesn’t have to go through the stress of trying to format the output because it has been handled already.&lt;br&gt;
The next statement tries to deposit a negative amount to the user’s account and prints the new balance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;clerk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;deposit&lt;/span&gt;&lt;span class="o"&gt;(-&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same as the balance method, negative values are not allowed to be deposited and therefore the program skips adding this invalid deposit amount to the previous balance.&lt;br&gt;
The next statement deposits a valid amount to the user’s account balance. In this case, the amount is added to the balance because it has been validated and passed the validation, and the new balance is then displayed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;clerk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;deposit&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1500&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The image below shows the output running the AccountTest class stored in AccountTest.java. It consists of the initial state of the account when the user tries to set an invalid balance, then when the user tries to deposit an invalid amount. And then to when the user enters the valid amount.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg39snie9a85e7rjgpd5u.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg39snie9a85e7rjgpd5u.JPG" alt="Encapsulation Output" width="746" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This article starts by introducing the concept of encapsulation using the medicine capsule as an example, another example that can be used here is the car instance. A car has at least a brake and accelerator, the driver uses these two to control the speed of the car but has no idea how the car does it. Their functionalities are hidden from the driver because most of the time the driver doesn't need to know how these things work before using the car. This is another good example of encapsulation.&lt;br&gt;
We listed a few advantages of encapsulation, there are many other advantages out there but we chose only a few important ones for the sake of this article.&lt;br&gt;
We then proceed to use the account class to explain the concept properly using some code examples.&lt;br&gt;
Encapsulation is a broad concept in programming and the implementation slightly varies across different programming languages that support it. This article demonstrated a basic implementation of encapsulation using Java.&lt;/p&gt;

</description>
      <category>java</category>
      <category>oop</category>
      <category>encapsulation</category>
      <category>beginners</category>
    </item>
    <item>
      <title>OOP Series: Inheritance in Java</title>
      <dc:creator>Ibrahim Suleiman</dc:creator>
      <pubDate>Sun, 26 Sep 2021 16:47:29 +0000</pubDate>
      <link>https://forem.com/ibs/oop-series-inheritance-in-java-4bn</link>
      <guid>https://forem.com/ibs/oop-series-inheritance-in-java-4bn</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;INTRODUCTION&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Object-oriented Programming (OOP) is a programming paradigm based on the concepts of &lt;code&gt;Objects&lt;/code&gt; and &lt;code&gt;Classes&lt;/code&gt;. It allows the group or collection of related objects in a class and creates functionalities for use by the class with the help of methods.&lt;br&gt;
Object-oriented programming has four pillars or principles known as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inheritance&lt;/li&gt;
&lt;li&gt;Polymorphism&lt;/li&gt;
&lt;li&gt;Data Abstraction&lt;/li&gt;
&lt;li&gt;Encapsulation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These four principles define an Object-oriented language. In this article, we will be looking at &lt;em&gt;Inheritance&lt;/em&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;CONTENT&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Prerequisite knowledge&lt;/li&gt;
&lt;li&gt;Inheritance&lt;/li&gt;
&lt;li&gt;Direct and Indirect superclass&lt;/li&gt;
&lt;li&gt;Composition vs. Inheritance&lt;/li&gt;
&lt;li&gt;The Student class&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;PREREQUISITE KNOWLEDGE&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This article explains the concept of inheritance using the Java programming language. Thus, you need the following knowledge of Java:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic understanding of OOP.&lt;/li&gt;
&lt;li&gt;Methods.&lt;/li&gt;
&lt;li&gt;Classes and objects.
If you are new to Java or have a rusty knowledge of it, you can do well to check out &lt;a href="https://dev.to/princeibs/hello-world-breakdown-in-java-2b67"&gt;Hello World Breakdown in Java&lt;/a&gt; where we walk through the building blocks of a simple Java program.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;INHERITANCE&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Inheritance is one of the four pillars of the Object-Oriented Programming paradigm. Inheritance is a concept that allows us to create new classes from an existing class by acquiring or inheriting the properties of that class. And also, adding more functionality to the new class. Take the family hierarchy, for example, the children exhibit some characteristics of either the mother or the father. Some of these characteristics might include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;skin color&lt;/li&gt;
&lt;li&gt;height&lt;/li&gt;
&lt;li&gt;size&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here, we can say that the child inherits those characteristics from the parent. Since those characteristics are present in the parent. Sometimes, the child might inherit not only the characteristics of the parent but the grandparents as well. In other cases, the child might inherit the characteristics of both the parent and grandparent and still display their characteristics, which make the child unique. This is the basic concept of inheritance in OOP. Inheritance allows us to derive a new class from an existing class. The existing class from which we can derive the new classes is called the &lt;strong&gt;superclass&lt;/strong&gt; in Java. It is also known as the &lt;strong&gt;&lt;em&gt;base&lt;/em&gt;&lt;/strong&gt; class or &lt;strong&gt;&lt;em&gt;parent&lt;/em&gt;&lt;/strong&gt; class in some other languages like C++. While the new class that is derived is called the &lt;strong&gt;subclass&lt;/strong&gt; in Java. It is also known as &lt;strong&gt;&lt;em&gt;derived&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;child&lt;/em&gt;&lt;/strong&gt; class in some other object-oriented languages as well.&lt;/p&gt;

&lt;p&gt;Some advantages inheritance provides are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reusability: It allows the reuse of code written by us or other programmers. Hence, avoiding &lt;em&gt;reinventing the wheel&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Flexibility: When we need to update some group of classes, inheritance allows us to update the superclass only. Instead of making the update on all the affected classes.&lt;/li&gt;
&lt;li&gt;Redundancy: Inheritance allows us to write less code and build upon the previous ones. Thereby reducing redundancy and encouraging cleaner code.&lt;/li&gt;
&lt;li&gt;Data hiding: The use of private instance variables, and setters and getters methods to access them. This allows restricted access to the variables of a class.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Direct and Indirect Superclass&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We can categorize superclasses into two types, &lt;em&gt;direct&lt;/em&gt; superclass, and &lt;em&gt;indirect&lt;/em&gt; superclass. The direct superclass is the immediate class from which a new class inherits in the class hierarchy. It is the class immediately above the new class in the inheritance hierarchy tree. The indirect superclass is any class in the inheritance hierarchy above the direct superclass. The new class does not directly inherit any attribute from an indirect superclass but gets these attributes from the direct superclass.&lt;br&gt;
All classes in Java directly or indirectly inherit from the &lt;code&gt;Object&lt;/code&gt; class, which can be found in &lt;code&gt;java.lang.Object&lt;/code&gt;. The &lt;code&gt;Object&lt;/code&gt; class provides some functionalities such as &lt;code&gt;equals()&lt;/code&gt; (which checks whether two objects are equal), &lt;code&gt;getClass()&lt;/code&gt; (which returns the runtime class of a particular object), &lt;code&gt;toString()&lt;/code&gt; (which returns a string comprising the name of the class of which the object is an instance and the unsigned hexadecimal representation of the hash code of the object), &lt;code&gt;notify()&lt;/code&gt;, &lt;code&gt;notifyAll()&lt;/code&gt; and many others which the Object’s subclasses can utilize.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Composition vs. Inheritance&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Java only provides support for single inheritance. Unlike other programming languages like C++ that support both single and multiple inheritance. In single inheritance, we can derive a class from only one direct superclass, while in multiple inheritance, we can derive a class from more than one superclass.&lt;br&gt;
The relationship that can exist between two classes can either be &lt;strong&gt;&lt;em&gt;is-a&lt;/em&gt;&lt;/strong&gt; relationship or &lt;strong&gt;&lt;em&gt;has-a&lt;/em&gt;&lt;/strong&gt; relationship. &lt;em&gt;Is-a&lt;/em&gt; relationship implies inheritance, while &lt;em&gt;has-a&lt;/em&gt; relationship implies composition.&lt;br&gt;
In inheritance, we can treat an object of a subclass as an object of the superclass. For instance, if a Rectangle class extends the Shape class, then the Rectangle &lt;em&gt;is-a&lt;/em&gt; Shape and thus, we can treat it as a Shape. In composition, a class can to have references to objects of other classes as its member. For example, each student has a course of study, so it is necessary to include a reference of the Course class as a member of the Student class. In this case, we can say that each Student &lt;em&gt;has-a&lt;/em&gt; a Course.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;The Student Class&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In this article, we will consider the &lt;code&gt;Student&lt;/code&gt; class to explain the concept of inheritance. Below is an image that shows some classes that can we can derive from the Student class and their hierarchical relationship.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwd9tti6spey5kntekwto.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwd9tti6spey5kntekwto.JPG" alt="Student Hierarchy" width="659" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we can consider the Student class to be the base class of all classes. &lt;code&gt;Undergraduate&lt;/code&gt; and &lt;code&gt;Graduate&lt;/code&gt; inherit directly from the Student class. &lt;code&gt;FirstYear&lt;/code&gt; and &lt;code&gt;SecondYear&lt;/code&gt; inherit directly from the Undergraduate class. In this case, FirstYear and SecondYear have an indirect inheritance to the Student class and a direct inheritance to the Undergraduate class. Since FirstYear and SecondYear inherit directly from the Undergraduate but not directly from the Student class.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;Inheritance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Student.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// validate first name&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;matches&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"[A-Z][a-zA-Z]+"&lt;/span&gt;&lt;span class="o"&gt;)))&lt;/span&gt; 
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid first name."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// validate last name&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!(&lt;/span&gt;&lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;matches&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"[a-zA-Z]+(['-][a-zA-Z]+)*"&lt;/span&gt;&lt;span class="o"&gt;)))&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid last name"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// validate student id&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid ID"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;studentId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// set and get methods&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setFirstName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;matches&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"[A-Z][a-zA-Z]+"&lt;/span&gt;&lt;span class="o"&gt;)))&lt;/span&gt; 
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid first name."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;        
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getFirstName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setLastName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!(&lt;/span&gt;&lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;matches&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"[a-zA-Z]+(['-][a-zA-Z]+)*"&lt;/span&gt;&lt;span class="o"&gt;)))&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid last name"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getLastName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setStudentID&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid ID"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;studentId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getStudentId&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s: %s%n%s: %s%n%s: %s%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
                &lt;span class="s"&gt;"Student ID"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;getStudentId&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="s"&gt;"First Name"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;getFirstName&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;
                &lt;span class="s"&gt;"Last Name"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;getLastName&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Student.java first starts by declaring and initializing its instance private instance fields. Recall that any field or member declared as private is only accessible to the class alone, so therefore the instance fields declared in the Student class can’t be accessed outside the class. &lt;br&gt;
This is where the set and get methods come to help. In this case, the set methods first validate the values entered before making them available to the class for use and throw an error with a meaningful message to the user. It means that any class extending the Student class needs not to validate its first name, last name, and student id anymore because the Student class has already handled that. This is one of the advantages of inheritance provides. It prevents programmers from reinventing the wheel. This speeds up production and also reduces the complexity of a program since some implementations are done in the superclass.&lt;br&gt;
The toString() method at the end also shows that the Student class is overriding one of its superclass methods using the &lt;a class="mentioned-user" href="https://dev.to/override"&gt;@override&lt;/a&gt; annotation. Recall that every class in Java by default and directly or indirectly extends the Object superclass, so we don’t have to explicitly extend the Object class at the Student class declaration. This also shows that subclasses can modify or increase the capabilities of the superclass members by simply overriding them in the subclass.&lt;/p&gt;

&lt;p&gt;The next program containing the Undergraduate class inherits the properties of the Student class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;Inheritance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Undergraduate.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Undergraduate&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Undergraduate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// set and get methods&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setYear&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getYear&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%sYear: %s%n%s%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;getYear&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="s"&gt;"Undergraduate Student"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Undegraduate.java&lt;/code&gt; first starts at the class declaration by extending/inheriting from the Student class using the &lt;code&gt;extends&lt;/code&gt; keyword. &lt;code&gt;extends&lt;/code&gt; is a keyword in Java we use to implement inheritance and it can only extend one class since Java only supports single inheritance.&lt;br&gt;
The program continued by declaring an instance field and then declaring the constructor with parameters. Then next to the initializing the superclass variable using the &lt;code&gt;super&lt;/code&gt; keyword. A set of parentheses comprising the superclass constructor arguments immediately followed this. &lt;code&gt;super&lt;/code&gt; is also a keyword in Java and subclasses use it to initialize the variables of the superclass constructor. Java requires that the first task of any subclass constructor is to call the constructor of its direct superclass.&lt;br&gt;
We don’t need to validate firstName, lastName, and studentId anymore because the superclass has handled that already. And we can access them using the &lt;code&gt;super.&lt;/code&gt; followed by their get methods. Similar to the Student class, the Undergraduate class also has its own &lt;code&gt;toString()&lt;/code&gt; method which uses &lt;code&gt;super.toString()&lt;/code&gt; String representation of the superclass. We can access members of the superclass using the &lt;code&gt;super.&lt;/code&gt; followed by the member to access. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next program containing the Graduate class also inherits from the student class&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;Inheritance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Graduate.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Graduate&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Graduate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;program&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// set and get methods&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setProgram&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;program&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getProgram&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%sProgram: %s%n%s%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
                &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;getProgram&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="s"&gt;"Graduate Student"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similar to the Undergraduate class, the Graduate class also extends the Student class. And contains an instance variable and a constructor that takes in values to initialize the superclass variables. The implementation of the Graduate class is similar to the Undergraduate class. The main purpose of the Graduate class is to show how multiple subclasses can inherit from a single superclass—Student class.&lt;br&gt;
The next program contains the &lt;code&gt;StudentTest&lt;/code&gt; class that implements both the Undergraduate and Graduate classes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;Inheritance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// StudentTest.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StudentTest&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="c1"&gt;// Declare and initialize objects&lt;/span&gt;
        &lt;span class="nc"&gt;Undergraduate&lt;/span&gt; &lt;span class="n"&gt;undergraduate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Undergraduate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Bush"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"White"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"1021"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Graduate&lt;/span&gt; &lt;span class="n"&gt;graduate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Graduate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Alexa"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Brown"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"1102"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Masters"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// print string representation of objects&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;undergraduate&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;graduate&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Class StudentTest creates objects of both the Undergraduate and Graduate classes. And then passes the constructor values as well. It then proceeds to print the values of the objects created. Notice that we did not explicitly use the &lt;code&gt;toString()&lt;/code&gt; method to print the String representation of the objects. This is because we have already customized them from the Undergraduate and Graduate classes respectively.&lt;br&gt;
Below is the output of executing &lt;code&gt;StudentTest.java&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8ly48wwobpzfizsvayd.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8ly48wwobpzfizsvayd.JPG" alt="StudentTest.java output" width="681" height="295"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The output first displays the details of the undergraduate student. Followed by the graduate student. Notice that among the undergraduate and graduate student details is an extra detail specifying the type of student. Which was not included in the constructor when created.&lt;br&gt;
These details and the format of the output were all included in the &lt;code&gt;toString()&lt;/code&gt; method of the respective subclasses.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Inheritance is a broad topic in Java and other object-oriented programming languages as well. This article implemented the concept of inheritance using the Student class. With Graduate and Undergraduate classes as subclasses. We learned about the concept of superclass and subclass and their usage in inheritance. And then used some keywords such as extends, super as well as using the &lt;a class="mentioned-user" href="https://dev.to/override"&gt;@override&lt;/a&gt; annotation to add functionalities to the default &lt;code&gt;toString&lt;/code&gt; method of every class. We then moved to use the setters and getters methods to access the private field of a superclass as well as validate the values before setting them.&lt;br&gt;
In the next article, we will build upon the concept of inheritance by introducing Polymorphism. The ability of objects to exist in many forms. Which is also a core principle of object-oriented programming.&lt;/p&gt;

</description>
      <category>java</category>
      <category>inheritance</category>
      <category>oop</category>
    </item>
    <item>
      <title>Access modifiers in Java</title>
      <dc:creator>Ibrahim Suleiman</dc:creator>
      <pubDate>Sat, 14 Aug 2021 22:47:30 +0000</pubDate>
      <link>https://forem.com/ibs/access-modifiers-in-java-4loh</link>
      <guid>https://forem.com/ibs/access-modifiers-in-java-4loh</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Imagine downloading a software package into your project for integration then you decide to use it inside your program,  on checking for the classes inside that package, boom! Before you are 1200 class and what you need is just 5 to 10 maximum, then you'll have to start searching for the few relevant ones. How frustrating? This is exactly what Object Oriented Programming languages like Java are here for. One of the motivations for access modifiers is a principle in computer science and information security known as &lt;strong&gt;The Principle of Least Privilege&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The principle of least privilege states that the code should be granted only the amount of privilege and access it needs to complete its designated task.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Access modifiers in Java are keywords used to determine the accessibility of classes, methods, and other members. Access modifiers implement one of the core Object-Oriented Programming concepts known as &lt;strong&gt;Encapsulation&lt;/strong&gt; or &lt;strong&gt;Data hiding&lt;/strong&gt;. The idea behind the concept was to give clients and users authorized access to &lt;em&gt;controlled&lt;/em&gt; information. This will limit the chances of data compromise or data hijacking.&lt;/p&gt;

&lt;p&gt;The access modifiers in Java are four with only three having keywords. They are&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Public access modifier&lt;/li&gt;
&lt;li&gt;Private access modifier&lt;/li&gt;
&lt;li&gt;Protected access modifier&lt;/li&gt;
&lt;li&gt;Default access modifier&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Public&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As the name suggests, public makes available the members (instance fields and methods) of a class to all other classes in the package and objects of that class as well, and even subclasses. The public access modifier is mostly used when we want to make an instance field or methods available to all the objects of the class, or available from other classes in the same package, or outside it (when imported).&lt;/p&gt;

&lt;p&gt;One of the major trade-offs in declaring a member public is that subclasses can easily modify the variable thereby making the class fragile.  And because that member is public, other classes or objects can have unauthorized access to the member thereby making it prone to malicious attack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Tip&lt;/em&gt;&lt;/strong&gt;: If a class must have public data, then make it final to avoid modification.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Private&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;salesAmount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Contrary to public, private gives a variable or field restricted access to the fields or methods themselves. Declaring a field or method private signifies that that method or field is available to only the members of that particular class. This means that other classes in the same package or objects of the class, or even subclasses don’t have access to these methods. &lt;br&gt;
The main reason for this is to prevent instance variable unauthorized access or modification. This is to fulfill one of OOPs important concepts known as Encapsulation or Data Hiding. &lt;br&gt;
A solution to private instance variables is to make corresponding public get and set methods and control their access to these instance fields and validate data to be stored in the field as well. This is one of the major uses of the get  and set methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Tip&lt;/em&gt;&lt;/strong&gt;: It is advisable to always use private fields in your class and use get and set methods to access them because this is a good software engineering practice.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Protected&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Protected bridges the gap between public and private members of a class.&lt;br&gt;
It provides an intermediate level of protection between public and private access.&lt;br&gt;
It allows a member to be accessed by the members of that class, by members of its subclass, and also members of other classes in the same package (protected has package access). &lt;br&gt;
The main difference between protected and public members is that public members can be accessed from anywhere in the package or even other packages (when imported) but protected is not available to members of other packages outside its current package.&lt;/p&gt;

&lt;p&gt;One of the drawbacks of using the protected access modifier is that members of the subclass can modify the variables easily without using the set methods, in which attackers can take advantage of this to alter the variables leading to inconsistent behavior in the class.&lt;br&gt;
If the implementation of the class was to change next time, this means all other subclass inheriting from this class will have to be modified to fit this change, for instance, modifying an instance variable from lastName to surname, this means all other subclass using firstName will have to be modified to surname, which is not a good practice. &lt;br&gt;
Another drawback is that the protected members are visible to all other classes in the same package which might not be desirable for some problems.&lt;/p&gt;

&lt;p&gt;Usage: Just like public and private, protected is used in similar ways.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Default&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;dateOfBirth&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a class's fields or methods are declared without an access modifier, then the &lt;strong&gt;default&lt;/strong&gt; access modifier is assumed in this case. Java takes that field or method to be a default access modifier, therefore, making access to that field or method limited.&lt;br&gt;
With the default access modifier, other members of the class and classes in the same package can have access to it. But classes from other packages don't have access to it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;br&gt;
Below is an image from &lt;a href="https://upload.wikimedia.org/wikipedia/commons/2/20/JavaAccessSpecifier.jpg" rel="noopener noreferrer"&gt;Wikimedia&lt;/a&gt; summarizing the difference between the access modifiers in Java.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9sdp4rapbomj55tulq43.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9sdp4rapbomj55tulq43.jpg" alt="Access modifiers relationship" width="537" height="409"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
The use of access modifiers in an object-oriented language like Java is highly encouraged to prevent information leakage mostly in industry-standard software that is reusable. Sticking to this principle will go a long way in preventing malicious attacks on most of our software today.&lt;br&gt;
Sticking to this principle will go a long way in preventing malicious attacks on most of our software today.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>accessmodifier</category>
      <category>programming</category>
    </item>
    <item>
      <title>Hello World Breakdown In Java</title>
      <dc:creator>Ibrahim Suleiman</dc:creator>
      <pubDate>Tue, 10 Aug 2021 22:16:55 +0000</pubDate>
      <link>https://forem.com/ibs/hello-world-breakdown-in-java-2b67</link>
      <guid>https://forem.com/ibs/hello-world-breakdown-in-java-2b67</guid>
      <description>&lt;p&gt;Hello World 👋.&lt;br&gt;
Today, we will be going through the famous &lt;code&gt;Hello World!&lt;/code&gt; program in Java. Yes, the famous Hello World! program. It's more like a culture in the developer's circle to first write the &lt;code&gt;Hello World&lt;/code&gt; program as their first line of code in a new language. Enjoy&lt;/p&gt;
&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Java is a high-level, interpreted, and object-oriented programming language developed by James Gosling at Sun Microsystem (now acquired by Oracle) and released in the year 1995. It's a general programming language and platform-independent (It allows developers to write once and run anywhere - WORA). This implies that compiled Java code can run on any platform and operating system that supports Java without recompilation. &lt;br&gt;
Java was designed to look similar in syntax with languages like C or C++ so that application programmers would find it familiar. &lt;/p&gt;
&lt;h2&gt;
  
  
  Definition of terms
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Keyword&lt;/strong&gt;: A keyword in Java is any word that is reserved for use by Java. Java has 52 of these words and this implies that these words can't be used as names for variables, methods, classes, or any other identifier. It is otherwise known as &lt;em&gt;reserved keyword&lt;/em&gt;. It is always spelled in lowercase.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access modifier&lt;/strong&gt;: An access modifier is a keyword that determines the accessibility of a class, variable, method, and other members in a class to other subclasses or objects. It can either be public, private, protected, or default. Check &lt;a href="https://dev.to/princeibs/access-modifiers-in-java-4loh"&gt;here&lt;/a&gt; for more on access modifiers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Variable&lt;/strong&gt;: A variable is a location in the computer's memory that is used to store value for later use. Java is a statically typed language (variable types are explicitly declared and therefore determined as compile-time) so therefore variable types are declared before the variable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Identifier&lt;/strong&gt;: A series of characters made up of letters, digits, underscores (_), and a dollar sign ($). It does not contain spaces and does not begin with any digit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Class&lt;/strong&gt;: A class is a blueprint of an object. A class allows us to define the state, properties, and behaviors of our methods.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Method&lt;/strong&gt;: A method in Java is a block of code delimited by curly brackets ({)that performs a particular function in our class.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Subclass&lt;/strong&gt;: A subclass is a class that is derived from another class. The class it is derived from is called the &lt;em&gt;super class&lt;/em&gt; or &lt;em&gt;base class&lt;/em&gt;. All classes in Java are by default subclasses because they inherit from the &lt;code&gt;Object&lt;/code&gt; superclass.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Object&lt;/strong&gt;: An instance of a class. Classes can have multiple instances and these instances have their separate copy of the attributes of the class. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data types&lt;/strong&gt;: Data type in Java is a constraint that determines the value that a variable can take. It tells the Java compiler how to use the data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Instance variable/method&lt;/strong&gt;: An instance variable creates a separate copy of itself when used inside an object. Modifying an instance variable does not affect the class it was created from. The same also applies to methods.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// HelloWorld.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloWorld&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello world"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Breakdown
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Line 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;// HelloWorld.java&lt;/code&gt;&lt;/strong&gt; - This signifies a single line of comment in Java. It is used to make our code easier to understand by other programmers and for future references. Java has 3 types of comments namely: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Single line comment&lt;/em&gt;: As we've seen above. They are mostly used to describe code functionality.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Double line comment&lt;/em&gt;: &lt;code&gt;/* comment here */&lt;/code&gt;. It is used to comment on multiple lines of code.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Documentation/Javadoc comment&lt;/em&gt;: &lt;code&gt;/** comment here */&lt;/code&gt;. Used when writing code for projects or software packages. Check &lt;a href="https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html" rel="noopener noreferrer"&gt;here&lt;/a&gt; for more info about Javadoc comment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Line 2&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloWorld&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now let's consider this step by step. Firstly, &lt;br&gt;
&lt;strong&gt;&lt;code&gt;public&lt;/code&gt;&lt;/strong&gt; - a keyword in Java and hence can't be used as an identifier. It signifies that the class &lt;code&gt;HelloWorld&lt;/code&gt; is public and therefore global in our program and project as a whole. &lt;br&gt;
&lt;strong&gt;&lt;code&gt;class&lt;/code&gt;&lt;/strong&gt; - also a keyword in java that informs the Java compiler that a class is about to be created. A Java source file may only contain one public class but can contain multiple classes with non-public access modifiers. &lt;br&gt;
&lt;strong&gt;&lt;code&gt;HelloWorld&lt;/code&gt;&lt;/strong&gt; - is an identifier that is used to name the class that is being created. Java has naming conventions for identifiers like variables, classes, methods, etc. The naming convention for a class in Java is the Pascal case - The first letter of every word is capitalized. Identifiers in Java also have their rules guiding them, one of which a class name must be in Pascal's case.&lt;br&gt;
&lt;strong&gt;&lt;code&gt;{&lt;/code&gt;&lt;/strong&gt; - open curly bracket, signifies the beginning of our class. Unlike other languages like Python, Java uses open curly brackets to mark the start of our classes or methods. This signifies the boundaries of our class or method and can also be referred to as the body of the class or method.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Line 3
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;&lt;code&gt;public&lt;/code&gt;&lt;/strong&gt; - also implies that the method has &lt;code&gt;public access&lt;/code&gt; - other classes can use this method. &lt;br&gt;
&lt;strong&gt;&lt;code&gt;static&lt;/code&gt;&lt;/strong&gt; - signifies that the method is only visible to this class and other classes that this particular class is offering services to e.g. subclasses. This means that objects of this class cannot access this method because it is declared &lt;code&gt;static&lt;/code&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that the keyword static is omitted, then the method can be classified as an instance method.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;void&lt;/code&gt;&lt;/strong&gt; - is a return type in Java and it indicates that this method will perform a function and won't return any information. Other return types might be &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;short&lt;/code&gt;, &lt;code&gt;byte&lt;/code&gt; &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;long&lt;/code&gt;, &lt;code&gt;double&lt;/code&gt;, &lt;code&gt;char&lt;/code&gt;. These are also data types in Java. Any method having a return type other than &lt;code&gt;void&lt;/code&gt; must end with the keyword &lt;code&gt;return&lt;/code&gt; followed by a value that is of the same type as the return type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;age&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;main&lt;/code&gt;&lt;/strong&gt; - is the name of the method and it is also an identifier. This is the starting point of every Java application. When we run any Java application, this is the first place it begins its execution, no matter its location in the program. The naming convention of a method is the Camel case - The first letter of every other word must be capitalized except the first word, which is in lower case. Java requires that every program must have the main method within which the program starts its execution.&lt;br&gt;
&lt;strong&gt;&lt;code&gt;(String[] args)&lt;/code&gt;&lt;/strong&gt; - is called the parameter list in Java. The parameter list is used to receive data into our methods that can only be used within the method's body. Java demands that the main method's parameter list takes in an array of &lt;code&gt;String&lt;/code&gt; objects and an identifier &lt;code&gt;args&lt;/code&gt; which is the default name by convention, is used to store the data received. Methods that take in parameters in Java are called &lt;em&gt;Parametric methods&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example:  the &lt;code&gt;public static void main(String[] args)&lt;/code&gt; method takes in an array of Strings and stores it in variable &lt;code&gt;args&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;while methods that don't take parameters are called &lt;em&gt;Non-parametric methods&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example: &lt;code&gt;public static String getName()&lt;/code&gt; - here, the parameter list is empty.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;{&lt;/code&gt;&lt;/strong&gt; - The opening curly bracket here similar to the one in our class definition also performs the same function. It delimits the method and can also be called the method's body. Any statement, variables, or expressions within the opening curly bracket and the closing bracket belong to the method. They can't be used outside the method's scope.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The method's scope is defined by the opening and closing curly brackets.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Line 4
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello world"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;System.out&lt;/code&gt;&lt;/strong&gt; - an object in Java already known as the standard output object that is already imported by default into all Java programs by &lt;em&gt;java.lang&lt;/em&gt; package. It is used to display information on the command window from our Java programs.&lt;br&gt;
&lt;strong&gt;&lt;code&gt;println("Hello world");&lt;/code&gt;&lt;/strong&gt; - a &lt;code&gt;System.out&lt;/code&gt; method that prints string literals within but not including the double quotation marks to the command window. Some other methods are &lt;code&gt;printf()&lt;/code&gt;, &lt;code&gt;print()&lt;/code&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Most statements in Java ends with a semicolon to signify the end of that statement.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Line 5 and Line 6
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;       &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
`&lt;br&gt;
are both closing curly brackets that close our method and class respectively. They signify the end and scope of the method, and the class as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Please note that the above is just a minimal breakdown of the popular &lt;code&gt;Hello World!&lt;/code&gt; program and it is more complex and deeper than explained above. Some of the details are not included because they need some prerequisite knowledge before they can be understood. Please refer to the &lt;a href="https://docs.oracle.com/en/java/" rel="noopener noreferrer"&gt;official Java documentation&lt;/a&gt; for more details.&lt;/p&gt;

</description>
      <category>java</category>
      <category>helloworld</category>
      <category>breakdown</category>
    </item>
  </channel>
</rss>
