<?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: VasDev</title>
    <description>The latest articles on DEV Community by VasDev (@vasdev).</description>
    <link>https://dev.to/vasdev</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%2F1630689%2Fb1bda6fa-19b4-4079-af01-449860bd13b4.png</url>
      <title>DEV Community: VasDev</title>
      <link>https://dev.to/vasdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vasdev"/>
    <language>en</language>
    <item>
      <title>Java OOP, in a Nutshell</title>
      <dc:creator>VasDev</dc:creator>
      <pubDate>Tue, 18 Jun 2024 09:10:55 +0000</pubDate>
      <link>https://dev.to/vasdev/java-oop-in-a-nutshell-1ne0</link>
      <guid>https://dev.to/vasdev/java-oop-in-a-nutshell-1ne0</guid>
      <description>&lt;p&gt;This blog is about the implementation and working of various object oriented programming concepts in Java. If you want a quick overview or recap, then this blog is for you!&lt;/p&gt;

&lt;p&gt;Firstly, lets quickly understand the core concepts of OOP:&lt;/p&gt;

&lt;h2&gt;
  
  
  Encapsulation
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition :&lt;/strong&gt; The action of enclosing something in or as if in a &lt;u&gt;capsule&lt;/u&gt;. (&lt;em&gt;Oxford&lt;/em&gt;)&lt;br&gt;&lt;br&gt;
In programming, encapsulation ensures that the user has limited access to the application data, that too via methods defined inside the application.&lt;br&gt;
Everytime, we encapsulate the data, we define &lt;strong&gt;getters&lt;/strong&gt; and &lt;strong&gt;setters&lt;/strong&gt; to retrieve and assign the values of variables that holds the data, respectively.&lt;br&gt;&lt;br&gt;
The encapsulation of variables and methods in java is achieved with the help of access modifiers. Here are a few of them:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;public&lt;/code&gt; : These methods can be accessed from anywhere, from any file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;private&lt;/code&gt; : These methods can only be accessed from within the class. Useful for &lt;em&gt;encapsulation&lt;/em&gt; of data.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;protected&lt;/code&gt; : These methods can be accessed only from within the class and its subclasses. Applies only when &lt;strong&gt;inheritence&lt;/strong&gt; in involved in the program&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, lets create a simple program, step by step to understand encapsulation!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step-1:&lt;/strong&gt; Create a empty public class &lt;u&gt;&lt;em&gt;Person&lt;/em&gt;&lt;/u&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 Person {
    // code will go here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-2:&lt;/strong&gt; Declare data members &lt;u&gt;&lt;em&gt;name&lt;/em&gt; &lt;/u&gt;of type &lt;u&gt;&lt;em&gt;string&lt;/em&gt;&lt;/u&gt;, and &lt;u&gt;&lt;em&gt;age&lt;/em&gt; &lt;/u&gt;of type &lt;u&gt;&lt;em&gt;int&lt;/em&gt; &lt;/u&gt;. Make sure the access modifier of these data members should be &lt;code&gt;private&lt;/code&gt; as we dont want users to access them directly.&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 Person {
    private String name; // name of the person
    private int age; // age of the person
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-3:&lt;/strong&gt; Now, we have variables to hold name and age of a person, but since user cant directly access them, we need to define methods to set the values of these variables. These methods are called &lt;em&gt;setters&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

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

// sets the name of the person
public void setName(String name) { 
    this.name = name;
}

// sets the age of the person
public void setAge(int age) {
    this.age = age;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-4:&lt;/strong&gt; After defining setters, we need to define some more methods which will help the users to retrieve the encapsulated data, these methods are called &lt;em&gt;getters&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

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

// returns the name of the person
public String getName() { 
    return name;
}

// returns the age of the person
public int getAge() {
    return age;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You are all set! In just 4 steps, you have implemented encapsulation.&lt;/p&gt;

&lt;p&gt;Now, you can create an object of this class in a driver class (Main class) and retrieve/assign the values to the variables without directly accessing them like 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 Main {
    public static void main(String[] args) {
        Person person = new Person(); // creating an instance

        // setting person's name and age
        person.setName("Robert");
        person.setAge("29");

        // getting person's name and age
        person.getName();
        person.getAge();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Inheritance
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition :&lt;/strong&gt; When a class (sub/child class) derives properties (data members) and behaviors (class methods) from another class (super/parent class), enabling code reuse and extension.&lt;br&gt;
To inherit the properties and behaviors of a super class, we use &lt;code&gt;extends&lt;/code&gt; keyword in java&lt;/p&gt;

&lt;p&gt;Implementing inheritance with an example:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step-1&lt;/strong&gt; : Create a super class &lt;u&gt;&lt;em&gt;Animal&lt;/em&gt;&lt;/u&gt; with a method &lt;u&gt;&lt;em&gt;eat()&lt;/em&gt;&lt;/u&gt;, since every animal eats.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// base/super/parent class
class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-2&lt;/strong&gt; : Create a sub class &lt;u&gt;&lt;em&gt;Dog&lt;/em&gt;&lt;/u&gt; inheriting &lt;u&gt;&lt;em&gt;Animal&lt;/em&gt;&lt;/u&gt; class with a method &lt;u&gt;&lt;em&gt;bark()&lt;/em&gt;&lt;/u&gt;, since dog is the animal that barks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Derived/sub/child class
class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-3&lt;/strong&gt; : Create an instance of &lt;u&gt;&lt;em&gt;Dog&lt;/em&gt;&lt;/u&gt; class in driver class, and call the methods of both classes.&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 Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.eat();  // Inherited method
        myDog.bark(); // Method specific to Dog
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congrats, you have successfully implemented simple Inheritance in Java! Similarly, we can implement &lt;u&gt;multilevel&lt;/u&gt;, &lt;u&gt;hierarchical &lt;/u&gt;and &lt;u&gt;hybrid&lt;/u&gt; inheritance also.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note : Java doesn't explicitly support multiple inheritance, to implement it, we define abstract methods in interface/s.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Polymorphism
&lt;/h2&gt;

&lt;p&gt;The word polymorphism is derived from Greek and means "having multiple forms."&lt;br&gt;
&lt;strong&gt;Definition :&lt;/strong&gt; the ability of a method to operate on different types of objects, allowing for different behaviors based on the object's actual class.&lt;br&gt;
We use method overloading and method overriding to achieve polymorphism in java. Here's the implementation:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step-1&lt;/strong&gt; : Take the above super class &lt;u&gt;&lt;em&gt;Animal&lt;/em&gt;&lt;/u&gt; and define a method &lt;u&gt;&lt;em&gt;makeSound()&lt;/em&gt;&lt;/u&gt; in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// base class
class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }

    void makeSound() { // Method to be overridden
        System.out.println("Some sound of the animal");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-2&lt;/strong&gt; : Write another &lt;u&gt;&lt;em&gt;makeSound()&lt;/em&gt;&lt;/u&gt; but, with parameter &lt;em&gt;sound&lt;/em&gt; of type &lt;em&gt;string&lt;/em&gt;. (i.e., &lt;em&gt;makeSound( String sound )&lt;/em&gt;). This is known as &lt;strong&gt;Method Overloading&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// base class
class Animal {
    void makeSound() { // Method to be overridden
        System.out.println("Some sound");
    }
    void makeSound(String sound) { // Overloaded method
        System.out.println(sound);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-3&lt;/strong&gt; : Create a derived class &lt;u&gt;&lt;em&gt;Cat&lt;/em&gt;&lt;/u&gt;, inheriting &lt;u&gt;&lt;em&gt;Animal&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// derived class
class Cat extends Animal {
    // method overriding goes here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-4&lt;/strong&gt; : Rewrite the &lt;u&gt;&lt;em&gt;makeSound()&lt;/em&gt;&lt;/u&gt; method with cat specific sound. This is known as &lt;strong&gt;Method Overriding&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Derived class
class Cat extends Animal {
    @Override // just a convention, not necessary to write
    void makeSound() { // Overriding method
        System.out.println("Meow");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-5&lt;/strong&gt; : Create a &lt;em&gt;Cat&lt;/em&gt; object in driver class, and run the overridden and overloaded methods.&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 Main {
    public static void main(String[] args) {
        Animal myAnimal = new Cat(); // A Cat object of type Animal
        myAnimal.makeSound(); // Calls overridden method, outputs "Meow"
        myAnimal.makeSound("Growl"); // Calls overloaded method, outputs "Growl"
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have implemented polymorphism in just 5 steps!&lt;/p&gt;

&lt;h2&gt;
  
  
  Abstraction
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definiton :&lt;/strong&gt; The process of hiding complex implementation details and exposing only the essential features of an object or system.&lt;/p&gt;

&lt;p&gt;Abstraction in Java is achieved with the help of :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Abstract Classes :&lt;/em&gt;&lt;/strong&gt; A superclass that defines generalized structure without actual implmentation of methods. It is achiever with the help of &lt;code&gt;abstract&lt;/code&gt; keyword, used as both, class and method specifier. It can have non-abstract methods as well.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Interfaces :&lt;/em&gt;&lt;/strong&gt; An interface is a class blueprint that only includes &lt;u&gt;static&lt;/u&gt; and &lt;u&gt;final&lt;/u&gt; methods and variables. It can only contains abstract methods&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's a simple implementation of Abstraction in Java :&lt;br&gt;
&lt;strong&gt;Step-1&lt;/strong&gt; : Create an abstract superclass &lt;u&gt;&lt;em&gt;Vehicle&lt;/em&gt;&lt;/u&gt; with an abstract method &lt;u&gt;&lt;em&gt;startEngine()&lt;/em&gt;&lt;/u&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 abstract class Vehicle {
    public abstract void startEngine();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-2&lt;/strong&gt; : Create its subclass &lt;u&gt;&lt;em&gt;Car&lt;/em&gt;&lt;/u&gt; and override the &lt;u&gt;&lt;em&gt;startEngine()&lt;/em&gt;&lt;/u&gt; method with car specific content.&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 Car extends Vehicle {
    @Override
    public void startEngine() {
        System.out.println("Car engine started");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-3&lt;/strong&gt; : Create an interface &lt;u&gt;&lt;em&gt;flyable&lt;/em&gt;&lt;/u&gt; with method &lt;u&gt;&lt;em&gt;fly()&lt;/em&gt;&lt;/u&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 interface Flyable {
    void fly();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-4&lt;/strong&gt; : Create another subclass of &lt;u&gt;&lt;em&gt;Vehicle&lt;/em&gt;&lt;/u&gt; named &lt;u&gt;&lt;em&gt;Airplane&lt;/em&gt;&lt;/u&gt; that also implements the &lt;u&gt;&lt;em&gt;flyable&lt;/em&gt;&lt;/u&gt; interface.&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 Airplane extends Vehicle implements Flyable {
    // overriding of methods goes here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-5&lt;/strong&gt; : Override &lt;u&gt;&lt;em&gt;startEngine()&lt;/em&gt;&lt;/u&gt; and &lt;u&gt;&lt;em&gt;fly()&lt;/em&gt;&lt;/u&gt; methods with airplane specific content.&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 Airplane extends Vehicle implements Flyable {
    @Override // overriding abstract class method
    public void startEngine() {
        System.out.println("Airplane engine started");
    }

    @Override // overriding interface method
    public void fly() {
        System.out.println("Airplane is flying");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-6&lt;/strong&gt; : Create a &lt;em&gt;Car&lt;/em&gt; and a _Airplane_object in driver class, and run the overridden methods.&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 Main {
    public static void main(String[] args) {
        Vehicle car = new Car();
        car.startEngine();  // Output: Car engine started

        Airplane airplane = new Airplane();
        airplane.startEngine();  // Output: Airplane engine started
        airplane.fly();  // Output: Airplane is flying
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have successfully implemented Abstraction in Java with the help of Abstract classes and Interfaces!&lt;/p&gt;

&lt;p&gt;In this blog, we have explored and successfully implemented all fundamental Object-Oriented Programming (OOP) concepts. From encapsulation and inheritance to polymorphism and abstraction, these concepts are key to building robust and maintainable software solutions. Thanks for reading..!&lt;/p&gt;

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