<?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>DEV Community: Aldo Telese</title>
    <description>The latest articles on DEV Community by Aldo Telese (@aldotele).</description>
    <link>https://dev.to/aldotele</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%2F726677%2Fb05d1a2e-2bd6-442a-b397-b1f711f853cd.jpeg</url>
      <title>DEV Community: Aldo Telese</title>
      <link>https://dev.to/aldotele</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aldotele"/>
    <language>en</language>
    <item>
      <title>Use Git Stash and you won't regret it</title>
      <dc:creator>Aldo Telese</dc:creator>
      <pubDate>Sun, 17 Mar 2024 18:53:04 +0000</pubDate>
      <link>https://dev.to/aldotele/use-git-stash-and-you-wont-regret-it-1p83</link>
      <guid>https://dev.to/aldotele/use-git-stash-and-you-wont-regret-it-1p83</guid>
      <description>&lt;p&gt;Let's say we are working on a feature branch called &lt;em&gt;feature/human&lt;/em&gt; where we have to build a representation of humans. We are almost done with our development but not ready to commit yet as something is still missing.&lt;/p&gt;

&lt;p&gt;At the same time, we need to do an urgent fix on a previously developed feature (about animals), for which we need to switch back to the main branch and start a new fix branch (&lt;em&gt;fix/animal&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;We might end up with carrying around incomplete changes that were made for a given purpose, on other branches that have nothing to do with  it, causing confusion at the moment of the commit, as we will be forced to select from the changelist only the relevant files for the current scope. The more the changes, the more the confusion.&lt;/p&gt;

&lt;p&gt;Here is when &lt;strong&gt;Git Stash&lt;/strong&gt; comes to our aid by allowing us to freeze and save the state of our changes in the "stash" for a later retrieval, keeping the changelist clean and consistent on each involved branch. &lt;/p&gt;

&lt;p&gt;Let's see the example in action. For simplicity, we are going to limit the number of changes made.&lt;/p&gt;

&lt;p&gt;This is the initial state of the project on the &lt;em&gt;main&lt;/em&gt; branch:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fszkb5qt2c4ynqr4wa8ov.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fszkb5qt2c4ynqr4wa8ov.png" alt="Image description" width="800" height="576"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;that is printing to the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Lion makes Meow
Cat makes Roar
Dog makes Foow
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you might have noticed, there are two bugs: the sounds of lion and cat should be inverted, and the dog's sound is not spelled correctly. &lt;br&gt;
Let's imagine we did not notice the bugs yet and already started the new feature about humans.&lt;/p&gt;

&lt;p&gt;This is the &lt;em&gt;feature/human&lt;/em&gt; branch we are currently working on:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8fclp9on9g7n74czoajo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8fclp9on9g7n74czoajo.png" alt="Image description" width="800" height="629"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and here is the changelist:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9rxh2ihnbba533m9gbei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9rxh2ihnbba533m9gbei.png" alt="Image description" width="638" height="984"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We already made a bunch of changes but the development is incomplete as we still need to implement the &lt;code&gt;sayHello&lt;/code&gt; method.&lt;br&gt;
However, someone made us notice the bugs on the animal package, and we need to fix them urgently (they are live in production!) before going ahead with humans.&lt;br&gt;
We cannot operate on the current feature branch as it won't compile. What's more, the fix is not consistent with the scope of the branch and we don't want to create confusion by messing up the working tree.&lt;/p&gt;

&lt;p&gt;Using Git Stash will solve our problems.&lt;/p&gt;

&lt;p&gt;From the terminal, let's type the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git stash save "human - TODO sayHello"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;that will save our changes to the stash with a custom message, cleaning up the changelist so that we can switch branches without carrying around incomplete developments.&lt;/p&gt;

&lt;p&gt;Now we can switch back to our main branch, start a new fix branch for animals, fix the bugs and merge it.&lt;/p&gt;

&lt;p&gt;When finished, we can get back to the &lt;em&gt;feature/human&lt;/em&gt; branch we were working on and type on terminal:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git stash list&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;that will print:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stash@{0}: On human: human - TODO sayHello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as you see, the stash area contains the changelist that we saved before and &lt;em&gt;On human&lt;/em&gt; indicates we saved them from the &lt;em&gt;feature/human&lt;/em&gt; branch.&lt;/p&gt;

&lt;p&gt;to apply the changes again, we can use two alternative commands:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;git stash pop&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;will retrieve the changes and remove them from the stash. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;git stash apply&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;will retrieve the changes and keep the copy on the stash.&lt;/p&gt;

&lt;p&gt;This second option is useful especially when we want to save specific "states" of the project before going ahead with other changes that will potentially break up things. It can be a lifesaver for delicate and complex developments as it allows to re-apply different versions of the changelist.&lt;/p&gt;

&lt;p&gt;For example, let's say we want to try a different approach for the development to see if it works better. We might save the previous version of the changes, start the new approach and save the relative changelist as well on the stash.&lt;br&gt;
Here is how our stash area might look like after that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stash@{0}: On human: human - new approach
stash@{1}: On human: human - TODO sayHello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's imagine we showed both approaches to a senior developer for a review and we realise that the original approach actually works better than the new one. We can re-apply the first version state by typing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash pop stash@{1}

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

&lt;/div&gt;



&lt;p&gt;if we also want to remove it from the stash list, otherwise:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash apply stash@{1}

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

&lt;/div&gt;



&lt;p&gt;if we want to keep a copy instead.&lt;/p&gt;

&lt;p&gt;As you can see, the &lt;code&gt;stash@{...}&lt;/code&gt; identifies specific states, and we need to specify it only if we are not aiming at the most recent state change.&lt;/p&gt;

&lt;p&gt;To conclude with the basics of git stash, another command is worth mentioning:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git stash drop&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;that is used to remove a state change from the list. As for the &lt;code&gt;pop&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; operations, without specifying the &lt;code&gt;stash@{...}&lt;/code&gt; identifier it will remove the most recent changelist, otherwise we must specify it.&lt;/p&gt;

&lt;p&gt;We came to the end of this brief explanation of a real world scenario where git stash can help you keeping your workflow cleaner, saving you from confusion and potential compiling errors as well.&lt;/p&gt;

&lt;p&gt;As the title of this article states, use it and you won't regret.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed the article, feel free to leave a comment, your feedback is very welcome.&lt;/p&gt;

&lt;p&gt;See you soon and keep stashing!&lt;/p&gt;

</description>
      <category>git</category>
      <category>programming</category>
    </item>
    <item>
      <title>Polymorphism and abstract class easily explained in Java</title>
      <dc:creator>Aldo Telese</dc:creator>
      <pubDate>Thu, 14 Mar 2024 18:22:47 +0000</pubDate>
      <link>https://dev.to/aldotele/polymorphism-and-abstract-class-easily-explained-in-java-5f7c</link>
      <guid>https://dev.to/aldotele/polymorphism-and-abstract-class-easily-explained-in-java-5f7c</guid>
      <description>&lt;p&gt;&lt;strong&gt;Polymorphism&lt;/strong&gt; is one of the main concepts behind OOP, together with encapsulation and inheritance. Despite its definition being pretty straightforward, you might struggle in understanding the reasons why it is used and the benefits it can lead to.&lt;/p&gt;

&lt;p&gt;Polymorphism literally means that &lt;em&gt;&lt;strong&gt;an object can take on many forms&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Here is a very simple example of a scenario where we can benefit from using polymorphism. We are going to make use of an &lt;strong&gt;abstract class&lt;/strong&gt; for the purpose, taking the opportunity to explain this concept as well.&lt;/p&gt;

&lt;p&gt;Let's say we want to make a class representing a human saying hello in its own language.&lt;/p&gt;

&lt;p&gt;Since saying hello is very specific to the language spoken by the person, a good candidate to start with would be in fact an abstract class called &lt;code&gt;Person&lt;/code&gt;, containing a method called &lt;code&gt;sayHello&lt;/code&gt; to be implemented by concrete classes extending it (an &lt;code&gt;EnglishPerson&lt;/code&gt;, an &lt;code&gt;ItalianPerson&lt;/code&gt;, a &lt;code&gt;SpanishPerson&lt;/code&gt;, etc.)&lt;/p&gt;

&lt;p&gt;Here is our &lt;code&gt;Person&lt;/code&gt; class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public abstract class Person {
    public abstract void sayHello();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see an abstract class generally contains one or more methods declared without an implementation. &lt;br&gt;
Why? Because the class is too generic to have a single implementation, and it is expected to be extended by other classes that will then be required to implement such abstract method(s).&lt;/p&gt;

&lt;p&gt;Let's create two more classes now: &lt;code&gt;SpanishPerson&lt;/code&gt; and &lt;code&gt;ItalianPerson&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class SpanishPerson extends Person {
    @Override
    public void sayHello() {
        System.out.println("Hola");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class ItalianPerson extends Person {
    @Override
    public void sayHello() {
        System.out.println("Ciao");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have created two concrete classes extending our abstract class and implementing the single abstract method that was declared.&lt;/p&gt;

&lt;p&gt;Now we can leverage polymorphism by writing this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class HelloDemo {
    public static void main(String[] args) {
        Person mattia = new ItalianPerson();
        Person dolores = new SpanishPerson();

        mattia.sayHello();
        dolores.sayHello();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;that will print:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ciao
Hola
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically with polymorphism we are able to &lt;strong&gt;&lt;em&gt;assign a reference of a superclass to an instance of a subclass&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
In our case, &lt;code&gt;mattia&lt;/code&gt; and &lt;code&gt;dolores&lt;/code&gt; are two references of the superclass &lt;code&gt;Person&lt;/code&gt;, but in practice they are pointing to objects of type &lt;code&gt;ItalianPerson&lt;/code&gt; and &lt;code&gt;SpanishPerson&lt;/code&gt;, respectively.&lt;/p&gt;

&lt;p&gt;To better understand the power of such concept let's introduce a new &lt;code&gt;Greeter&lt;/code&gt; class containing a method that accepts an argument of type &lt;code&gt;Person&lt;/code&gt; that will invoke its &lt;code&gt;sayHello&lt;/code&gt; method as before.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Greeter {
    public void greet(Person person) {
        person.sayHello();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of creating two (or more) different greet methods, one with a parameter of type &lt;code&gt;ItalianPerson&lt;/code&gt; and the other one with a parameter of type &lt;code&gt;SpanishPerson&lt;/code&gt;, we can leverage polymorphism by using a single method with a parameter of the abstract superclass &lt;code&gt;Person&lt;/code&gt; to which we can pass either an &lt;code&gt;ItalianPerson&lt;/code&gt; or a &lt;code&gt;SpanishPerson&lt;/code&gt; instance, just as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class HelloDemo {
    public static void main(String[] args) {
        Person mattia = new ItalianPerson();
        Person dolores = new SpanishPerson();

        Greeter greeter = new Greeter();
        greeter.greet(mattia);
        greeter.greet(dolores);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;that again will print:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ciao
Hola
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;making our code more extensible, reusable and clean.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed this short explanation of one of the core concepts of object oriented programming.&lt;br&gt;
Please feel free to leave a comment for any feedback.&lt;/p&gt;

&lt;p&gt;See you soon&lt;/p&gt;

</description>
      <category>oop</category>
      <category>polymorphism</category>
      <category>java</category>
    </item>
    <item>
      <title>A Simple Way to Check if a Word is Palindrome in Java</title>
      <dc:creator>Aldo Telese</dc:creator>
      <pubDate>Wed, 08 Nov 2023 16:54:52 +0000</pubDate>
      <link>https://dev.to/aldotele/a-simple-way-to-check-if-a-word-is-palindrome-in-java-19e7</link>
      <guid>https://dev.to/aldotele/a-simple-way-to-check-if-a-word-is-palindrome-in-java-19e7</guid>
      <description>&lt;p&gt;&lt;em&gt;Racecar&lt;/em&gt;, &lt;em&gt;Civic&lt;/em&gt;, &lt;em&gt;Radar&lt;/em&gt;, &lt;em&gt;Level&lt;/em&gt;, &lt;em&gt;Kayak&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;What do all these words have in common? &lt;br&gt;
Try to read them backwards... did you notice anything? Of course you did!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A palindrome word is a word that reads the same backwards as forwards&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The scope of this article is to illustrate you a very simple yet effective way to recognize a palindrome word, using &lt;strong&gt;Java&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's first create a Palindrome class with a main method and an input string from above:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvrl21xr85n8r06a9408h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvrl21xr85n8r06a9408h.png" alt="Image description" width="800" height="236"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we want now is to pass our input string to a method that will return a &lt;strong&gt;boolean&lt;/strong&gt;: true if the word is palindrome, false otherwise.&lt;br&gt;
We'll make this method static:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiydmte4ztfc1hjh8wobd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiydmte4ztfc1hjh8wobd.png" alt="Image description" width="800" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The logic we are going to use is very straightforward: we will divide the length of the input string by 2, and iterate over the string both forwards and backwards to compare, at each iteration, the couple of characters that we find. We will do that as many times as the number we obtained from the previous division. &lt;br&gt;
What if the length is odd? We will round it down!&lt;/p&gt;

&lt;p&gt;Let's pick for example the word &lt;em&gt;kayak&lt;/em&gt;:&lt;br&gt;
the length is 5. Dividing 5 by 2 will give us 2.5, namely 2 (we always round it down).&lt;br&gt;
Since the result of the division is 2, we will do 2 iterations. At each iteration, we compare the two characters to the left and to the right of our string. If the two characters are not the same, it means the word is not a palindrome and we return false.&lt;br&gt;
If the whole loop terminates without returning false, then we return true: the word is a palindrome.&lt;/p&gt;

&lt;p&gt;Here is the code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx47sub26xydcpd45m4ug.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx47sub26xydcpd45m4ug.png" alt="Image description" width="800" height="612"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: in the case of a string with an odd length, just as in our example, the middle character will not be compared to anything, since there is no need to do so.&lt;br&gt;
In our example, the code executed two iterations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in the first iteration, character at index 0 ("k") was compared with character at index 4 ("k")&lt;/li&gt;
&lt;li&gt;in the second (and last) iteration, character at index 1 ("a"), was compared with character at index 3 ("a").&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An additional iteration would have compared the character at index 2 with itself, which is not needed. Therefore, the code we wrote makes the loop stop before this unnecessary computation.&lt;/p&gt;

&lt;p&gt;Please remember to pay attention to case and word separators (in case of sentences). If we used &lt;em&gt;Kayak&lt;/em&gt; as our input string, then the method would have returned false as "K" and "k" are different. The same applies when using sentences with whitespaces and punctuation, as they will affect the result. In these cases you need to take into account such additional elements, depending on the specific requirements.&lt;/p&gt;

&lt;p&gt;Hope you found this article useful! &lt;br&gt;
See you soon, and keep coding!&lt;/p&gt;

</description>
      <category>palindrome</category>
      <category>java</category>
      <category>programming</category>
    </item>
    <item>
      <title>OOP fundamentals: what is a Class? The Talking Dog example</title>
      <dc:creator>Aldo Telese</dc:creator>
      <pubDate>Sun, 02 Oct 2022 20:26:44 +0000</pubDate>
      <link>https://dev.to/aldotele/oop-fundamentals-what-is-a-class-java-example-3832</link>
      <guid>https://dev.to/aldotele/oop-fundamentals-what-is-a-class-java-example-3832</guid>
      <description>&lt;p&gt;Understanding the meaning of &lt;strong&gt;Class&lt;/strong&gt; is perhaps the very first step to take when diving into &lt;strong&gt;Object-Oriented Programming&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this article we will use a very basic example written in Java to explain the concepts of &lt;strong&gt;Class&lt;/strong&gt;, &lt;strong&gt;Object&lt;/strong&gt;, &lt;strong&gt;methods&lt;/strong&gt; and &lt;strong&gt;variables&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's consider the following sentence:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;a dog called Muffin is talking.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In our fictional world, we know that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;there is a dog, our "actor"&lt;/li&gt;
&lt;li&gt;the dog's name is Muffin&lt;/li&gt;
&lt;li&gt;the dog is doing an action: talking&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;we can use Object-Oriented programming to describe the previous scenario in terms of code.&lt;/p&gt;

&lt;p&gt;Breaking the scenario into &lt;em&gt;actor&lt;/em&gt; and &lt;em&gt;action&lt;/em&gt;, as we did, can help us better understand the meaning of &lt;em&gt;Class&lt;/em&gt; and &lt;em&gt;class method&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;We can usually refer to the Class as being our actor, while the method describes some kind of action. Lastly, the variable can be seen as something that "belongs" to our actor.&lt;/p&gt;

&lt;p&gt;Let's now convert into code what we have seen before, step-by-step, starting from our actor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Dog {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we just created a class called &lt;code&gt;Dog&lt;/code&gt;.&lt;br&gt;
Forget about the &lt;code&gt;public&lt;/code&gt; keyword, for now. Just assume that it means our class will be accessible from anywhere in our program.&lt;/p&gt;

&lt;p&gt;Let's go ahead. We want to make it possible, for a dog, to have a name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Dog {
    String name;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;finally, our dog will be able to talk:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Dog {
    String name;

    public void talk() {
        System.out.println("Hello, I am " + name + " and I am a talking dog.");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As for the &lt;code&gt;public&lt;/code&gt; keyword, do not focus too much on the &lt;code&gt;void&lt;/code&gt; keyword, it just means that our method does not return anything, it only prints something to the console. However, this will be covered more in-depth in another tutorial.&lt;/p&gt;

&lt;p&gt;Here is a view of our project so far:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2n23oi76bl35h0447rb1.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2n23oi76bl35h0447rb1.JPG" alt="Image description" width="800" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are not finished yet! The thing is, what we did so far was only designing a &lt;em&gt;blueprint&lt;/em&gt; for real-life dogs, &lt;u&gt;without actually creating any of them&lt;/u&gt;.&lt;br&gt;
And here comes the concept of &lt;strong&gt;Object&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An Object is a &lt;em&gt;class instance&lt;/em&gt;&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;In our example, it would be a dog "coming alive" with his own name.&lt;/p&gt;

&lt;p&gt;Let's create a new class that we will call &lt;code&gt;DogDemo&lt;/code&gt;. &lt;br&gt;
This class will be responsible of using our &lt;code&gt;Dog&lt;/code&gt; class to create actual dogs, making them talk as well. &lt;br&gt;
The class will have a single &lt;em&gt;main&lt;/em&gt; method to perform these operations. &lt;/p&gt;

&lt;p&gt;Try to only focus on the content inside the main method (don't worry about all that strange keywords &lt;code&gt;public static void String[] args&lt;/code&gt;, for now):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class DogDemo {
    public static void main(String[] args) {
        Dog muffin = new Dog();
        muffin.name = "Muffin";
        muffin.talk();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the main method of our demo class, we just made a dog called Muffin come alive.&lt;br&gt;
We created a &lt;code&gt;new Dog()&lt;/code&gt; and assigned the name Muffin to his instance variable &lt;code&gt;name&lt;/code&gt;.&lt;br&gt;
Finally, we invoked the &lt;code&gt;talk()&lt;/code&gt; method on the class instance.&lt;/p&gt;

&lt;p&gt;Here is our updated project:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9sebagwlxq6itw508hck.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9sebagwlxq6itw508hck.JPG" alt="Image description" width="800" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point, to see the output of our program, we need to run the main method of our &lt;code&gt;DogDemo&lt;/code&gt; class. &lt;/p&gt;

&lt;p&gt;Let's do that by using the command line.&lt;/p&gt;

&lt;p&gt;From the root directory of the project, launch these:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;cd src&lt;/code&gt;  --&amp;gt; to place yourself on the source code directory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;javac DogDemo.java&lt;/code&gt;  --&amp;gt; to compile the source code&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;java DogDemo&lt;/code&gt;  --&amp;gt; to execute the compiled code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the result of running them from the integrated IntelliJ terminal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyg5lkxaxc7gaymkwwk2g.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyg5lkxaxc7gaymkwwk2g.JPG" alt="Image description" width="800" height="424"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the final output is the one we were expecting: our dog talking.&lt;/p&gt;

&lt;p&gt;Note that two new files appeared within our project: &lt;code&gt;Dog.class&lt;/code&gt; and &lt;code&gt;DogDemo.class&lt;/code&gt;. These were generated by the compilation command, and executed in the third step.&lt;/p&gt;

&lt;p&gt;One last thing... &lt;/p&gt;

&lt;p&gt;This example was intentionally simplified in order to convey the meaning of Class and Object. There are a few concepts that are missing, in addition to the mentioned ones, and above all: &lt;em&gt;constructor&lt;/em&gt;, &lt;em&gt;getters&lt;/em&gt; and &lt;em&gt;setters&lt;/em&gt;, that will be covered very soon in another tutorial, by following the same example. They will specifically impact the way we created the Dog instance and assigned the name to our dog.&lt;/p&gt;




&lt;p&gt;And that was our first dive into the world of Object-Oriented Programming! &lt;/p&gt;

&lt;p&gt;Let me know if you found this example useful to grasp some of the basic OOP concepts, any feedback is welcome, just leave a comment below!&lt;/p&gt;

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

</description>
      <category>oop</category>
      <category>java</category>
    </item>
  </channel>
</rss>
