<?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: ADITYA SASANE</title>
    <description>The latest articles on DEV Community by ADITYA SASANE (@addysasane77).</description>
    <link>https://dev.to/addysasane77</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%2F900136%2Fe8846505-137e-4ce6-b5b0-b2f6f2784d17.jpg</url>
      <title>DEV Community: ADITYA SASANE</title>
      <link>https://dev.to/addysasane77</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/addysasane77"/>
    <language>en</language>
    <item>
      <title>What are OOPs Concepts ?</title>
      <dc:creator>ADITYA SASANE</dc:creator>
      <pubDate>Fri, 29 Jul 2022 20:27:00 +0000</pubDate>
      <link>https://dev.to/addysasane77/what-are-oops-concepts--17bj</link>
      <guid>https://dev.to/addysasane77/what-are-oops-concepts--17bj</guid>
      <description>&lt;p&gt;Java  is a class-based object-oriented programming (OOP) language built around the concept of objects. OOP concepts are intended to improve code readability and reusability by defining how to structure your Java program efficiently. The core principles of object-oriented programming are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Abstraction&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Encapsulation&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;3. Inheritance&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;4. Polymorphism&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;5. Association&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;6. Aggregation&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;7. Composition&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Java comes with specific code structures for each OOP concept, such as the extends keyword for the inheritance principle or the getter and setter methods for the encapsulation principle.&lt;/p&gt;

&lt;p&gt;While these concepts are crucial for creating well-structured Java programs in the development phase, implementing crash reporting can also help you catch the errors your end-users encounter in the operation and maintenance phase of the software development life cycle.&lt;/p&gt;

&lt;p&gt;In this guide, we’ll look into both the theory and practice of object-oriented programming to help you write performant and error-free Java code.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;What are OOP concepts in Java?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;OOP concepts allow us to create specific interactions between Java objects. They make it possible to reuse code without creating security risks or harming performance and code readability.&lt;/p&gt;

&lt;p&gt;There are four main and three secondary principles of object-oriented Java programming. Let’s take a look at what they are and why they’re useful.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;1. Abstraction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Abstraction aims to hide complexity from users and show them only relevant information. For example, if you’re driving a car, you don’t need to know about its internal workings.&lt;/p&gt;

&lt;p&gt;The same is true of Java classes. You can hide internal implementation details using abstract classes or interfaces. On the abstract level, you only need to define the method signatures (name and parameter list) and let each class implement them in their own way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Abstraction in Java:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hides the underlying complexity of data&lt;/li&gt;
&lt;li&gt;Helps avoid repetitive code&lt;/li&gt;
&lt;li&gt;Presents only the signature of internal functionality&lt;/li&gt;
&lt;li&gt;Gives flexibility to programmers to change the implementation of abstract behavior&lt;/li&gt;
&lt;li&gt;Partial abstraction (0-100%) can be achieved with abstract classes&lt;/li&gt;
&lt;li&gt;Total abstraction (100%) can be achieved with interfaces&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;2. Encapsulation&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Encapsulation helps with data security, allowing you to protect the data stored in a class from system-wide access. As the name suggests, it safeguards the internal contents of a class like a capsule.&lt;/p&gt;

&lt;p&gt;You can implement encapsulation in Java by making the fields (class variables) private and accessing them via their public getter and setter methods. JavaBeans are examples of fully encapsulated classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Encapsulation in Java:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Restricts direct access to data members (fields) of a class&lt;/li&gt;
&lt;li&gt;Fields are set to private&lt;/li&gt;
&lt;li&gt;Each field has a getter and setter method&lt;/li&gt;
&lt;li&gt;Getter methods return the field&lt;/li&gt;
&lt;li&gt;Setter methods let us change the value of the field&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;3. Inheritance&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Inheritance makes it possible to create a child class that inherits the fields and methods of the parent class. The child class can override the values and methods of the parent class, but it’s not necessary. It can also add new data and functionality to its parent.&lt;/p&gt;

&lt;p&gt;Parent classes are also called superclasses or base classes, while child classes are known as subclasses or derived classes as well. Java uses the extends keyword to implement the principle of inheritance in code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inheritance in Java:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A class (child class) can extend another class (parent class) by inheriting its features&lt;/li&gt;
&lt;li&gt;Implements the DRY (Don’t Repeat Yourself) programming principle&lt;/li&gt;
&lt;li&gt;Improves code reusability&lt;/li&gt;
&lt;li&gt;Multi-level inheritance is allowed in Java (a child class can have its own child class as well)&lt;/li&gt;
&lt;li&gt;Multiple inheritances are not allowed in Java (a class can’t extend more than one class)&lt;/li&gt;
&lt;li&gt;Abstraction, encapsulation, polymorphism, and inheritance are the four main theoretical principles of object-oriented programming. But Java also works with three further OOP concepts: association, aggregation, and composition. Aggregation is a special form of association, while composition is a special form of aggregation. While that may sound a bit convoluted, we’re about to explain. Read on!&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;4. Polymorphism&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Polymorphism refers to the ability to perform a certain action in different ways. In Java, polymorphism can take two forms: method overloading and method overriding.&lt;/p&gt;

&lt;p&gt;Method overloading happens when various methods with the same name are present in a class. When they are called, they are differentiated by the number, order, or types of their parameters. Method overriding occurs when a child class overrides a method of its parent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Polymorphism in Java:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The same method name is used several times&lt;/li&gt;
&lt;li&gt;Different methods of the same name can be called from an object&lt;/li&gt;
&lt;li&gt;All Java objects can be considered polymorphic (at the minimum, they are of their own type and instances of the Object class)&lt;/li&gt;
&lt;li&gt;Static polymorphism in Java is implemented by method overloading&lt;/li&gt;
&lt;li&gt;Dynamic polymorphism in Java is implemented by method overriding&lt;/li&gt;
&lt;li&gt;5. Association&lt;/li&gt;
&lt;li&gt;Association means the act of establishing a relationship between two unrelated classes. For example, when you declare two fields of different types (e.g. Car and Bicycle) within the same class and make them interact with each other, you have created an association.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;5. Association&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Association means the act of establishing a relationship between two unrelated classes. For example, when you declare two fields of different types (e.g. Car and Bicycle) within the same class and make them interact with each other, you have created an association&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Association in Java:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Two separate classes are associated through their objects&lt;/li&gt;
&lt;li&gt;The two classes are unrelated, each can exist without the other one&lt;/li&gt;
&lt;li&gt;Can be a one-to-one, one-to-many, many-to-one, or many-to-many relationship&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;6. Aggregation&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Aggregation is a narrower kind of association. It occurs when there’s a one-way (HAS-A) relationship between the two classes we associate through their objects.&lt;/p&gt;

&lt;p&gt;For example, every Passenger has a Car, but a Car doesn’t necessarily have a Passenger. When you declare the Passenger class, you can create a field of the Car type that shows which car the passenger belongs to. Then, when you instantiate a new Passenger object, you can access the data stored in the related Car as well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Aggregation in Java:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One-directional association&lt;/li&gt;
&lt;li&gt;Represents a HAS-A relationship between two classes&lt;/li&gt;
&lt;li&gt;Only one class is dependent on the other&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;7. Composition&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Composition is a stricter form of aggregation. It occurs when the two classes you associate are mutually dependent and can’t exist without each other.&lt;/p&gt;

&lt;p&gt;For example, take a Car and an Engine class. A Car cannot run without an Engine, while an Engine also can’t function without being built into a Car. This kind of relationship between objects is also called a PART-OF relationship.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Composition in Java:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A restricted form of aggregation&lt;/li&gt;
&lt;li&gt;Represents a PART-OF relationship between two classes&lt;/li&gt;
&lt;li&gt;Both classes are dependent on each other&lt;/li&gt;
&lt;li&gt;If one class ceases to exist, the other can’t survive alone&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Abstraction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0DAJR73C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l93g12ndi1k132adfwgz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0DAJR73C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l93g12ndi1k132adfwgz.png" alt="abstraction image" width="880" height="866"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As mentioned above, abstraction allows you to hide the internal workings of an object and only show the features the user needs to know about.&lt;/p&gt;

&lt;p&gt;Java provides two ways to implement abstraction: abstract classes and interfaces. With abstract classes, you can achieve partial abstraction, while interfaces make total (100%) abstraction possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Abstract classes&lt;/strong&gt;&lt;br&gt;
An abstract class is a superclass (parent class) that cannot be instantiated. To create a new object, you need to instantiate one of its child classes. Abstract classes can have both abstract and concrete methods. Abstract methods contain only the method signature, while concrete methods declare the method body as well. Abstract classes are defined with the abstract keyword.&lt;/p&gt;

&lt;p&gt;In the code example below, we create an abstract class called Animal with two abstract and one concrete method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;abstract  class  Animal  {
// abstract methods
abstract  void  move();
abstract  void  eat();
// concrete method
void  label()  {
System.out.println("Animal's data:");
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we extend it with two child classes: Bird and Fish. Both of them define their own implementations of the move() and eat() abstract methods&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class  Bird  extends Animal {
void  move()  {
System.out.println("Moves by flying.");
}
void  eat()  {
System.out.println("Eats birdfood.");
}
}
&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;class  Fish  extends Animal {
void  move()  {
System.out.println("Moves by swimming.");
}
void  eat()  {
System.out.println("Eats seafood.");
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we’ll test it with the help of the TestBird and TestFish classes. Both initialize an object (myBird and myFish) and call the one concrete (label()) and the two abstract (move() and eat()) methods.&lt;/p&gt;

&lt;p&gt;Note, however, that you don’t necessarily have to call all the methods if you don’t want to — this is how abstract classes make partial abstraction possible (for example, you could call just move()).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class  TestBird  {
public  static  void  main(String[] args)  {
Animal myBird =  new Bird();
myBird.label();
myBird.move();
myBird.eat();
}
}
&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;class  TestFish  {
public  static  void  main(String[] args)  {
Animal myFish =  new Fish();
myFish.label();
myFish.move();
myFish.eat();
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see below, the concrete method has been called from the Animal abstract class, while the two abstract methods have been called from Bird and Fish, respectively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Console output of TestBird]
Animal's data:
Moves by flying.
Eats birdfood.
&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;[Console output of TestFish]
Animal's data:
Moves by swimming.
Eats seafood.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M-gKXR1G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wyw1d8abohtbi0e3g3h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M-gKXR1G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wyw1d8abohtbi0e3g3h.png" alt="encapsulation" width="880" height="743"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With encapsulation, you can protect the fields of a class. To do so, you need to declare the fields as private and provide access to them with getter and setter methods.&lt;/p&gt;

&lt;p&gt;The Animal class below is fully encapsulated. It has three private fields, and each has its own pair of getter and setter methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class  Animal  {
private String name;
private  double averageWeight;
private  int numberOfLegs;

// Getter methods
public String getName()  {
return name;
}
public  double  getAverageWeight()  {
return averageWeight;
}
public  int  getNumberOfLegs()  {
return numberOfLegs;
}
// Setter methods
public  void  setName(String name)  {
this.name  = name;
}
public  void  setAverageWeight(double averageWeight)  {
this.averageWeight  = averageWeight;
}
public  void  setNumberOfLegs(int numberOfLegs)  {
this.numberOfLegs  = numberOfLegs;
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The TestAnimal class first creates a new Animal object (called myAnimal), then defines a value for each field with the setter methods, and finally prints out the values using the getter methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class  TestAnimal  {
public  static  void  main(String[] args)  {
Animal myAnimal =  new Animal();
myAnimal.setName("Eagle");
myAnimal.setAverageWeight(1.5);
myAnimal.setNumberOfLegs(2);
System.out.println("Name: "  + myAnimal.getName());
System.out.println("Average weight: "  + myAnimal.getAverageWeight()  +  "kg");
System.out.println("Number of legs: "  + myAnimal.getNumberOfLegs());
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see below, the Java console returns all the values we have set with the setter methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
[Console output of TestAnimal]
Name: Eagle
Average weight: 1.5kg
Number of legs: 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Interfaces&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;An interface is a 100% abstract class. It can only have static, final, and public fields and abstract methods. It’s frequently referred to as a blueprint of a class as well. Java interfaces allow you to implement multiple inheritances in your code, as a class can implement any number of interfaces. Classes can access an interface with the implements keyword.&lt;/p&gt;

&lt;p&gt;In the example, we define two interfaces: Animal with two abstract methods (interface methods are abstract by default) and Bird with two static fields and an abstract method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
interface  Animal  {
public  void  eat();
public  void  sound();
}
&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;interface  Bird  {
int numberOfLegs = 2;
String outerCovering =  "feather";
public  void  fly();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The class Eagle implements both interfaces. It defines its own functionality for the three abstract methods. The eat() and sound() methods come from the Animal class, while fly() comes from Bird.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class  Eagle  implements Animal, Bird {
public  void  eat()  {
System.out.println("Eats reptiles and amphibians.");
}
public  void  sound()  {
System.out.println("Has a high-pitched whistling sound.");
}
public  void  fly()  {
System.out.println("Flies up to 10,000 feet.");
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the TestEagleInterfaces test class, we instantiate a new Eagle object (called myEagle) and print out all the fields and methods to the console.&lt;/p&gt;

&lt;p&gt;As static fields (numberOfLegs and outerCovering) don’t belong to a specific object but to the interface, we need to access them from the Bird interface instead of the myEagle object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class  TestEagleInterfaces  {
public  static  void  main(String[] args)  {
Eagle myEagle =  new Eagle();

myEagle.eat();
myEagle.sound();
myEagle.fly();

System.out.println("Number of legs: "  + Bird.numberOfLegs);
System.out.println("Outer covering: "  + Bird.outerCovering);
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Java console returns all the information we wanted to access:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Console output of TestEagleInterfaces]
Eats reptiles and amphibians.
Has a high-pitched whistling sound.
Flies up to 10,000 feet.
Number of legs: 2
Outer covering: feather
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cRifWrLq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2akgfl4lwqpnlt7bco7i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cRifWrLq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2akgfl4lwqpnlt7bco7i.png" alt="polymorphism" width="880" height="834"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Polymorphism makes it possible to use the same code structure in different forms. In Java, this means that you can declare several methods with the same name as long as they are different in certain characteristics.&lt;/p&gt;

&lt;p&gt;As mentioned above, Java provides two ways to implement polymorphism: method overloading and method overriding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Static polymorphism (method overloading)&lt;/strong&gt;&lt;br&gt;
Method overloading means that you can have several methods with the same name within a class. However, the number, names, or types of their parameters need to be different.&lt;/p&gt;

&lt;p&gt;For example, the Bird() class below has three fly() methods. The first one doesn’t have any parameters, the second one has one parameter (height), and the third one has two parameters (name and height).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class  Bird  {
public  void  fly()  {
System.out.println("The bird is flying.");
}
public  void  fly(int height)  {
System.out.println("The bird is flying "  + height +  " feet high.");
}
public  void  fly(String name,  int height)  {
System.out.println("The "  + name +  " is flying "  + height +  " feet high.");
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The test class instantiates a new Bird object and calls the fly() method three times: first, without parameters, second, with one integer parameter for height, and third, with two parameters for name and height.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class  TestBirdStatic  {
public  static  void  main(String[] args)  {
Bird myBird =  new Bird();
myBird.fly();
myBird.fly(10000);
myBird.fly("eagle", 10000);
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the console, you can see that Java could have differentiated the three polymorphic fly() methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Console output of TestBirdStatic]
The bird is flying.
The bird is flying 10000 feet high.
The eagle is flying 10000 feet high.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Dynamic polymorphism (method overriding)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using the method overriding feature of Java, you can override the methods of a parent class from its child class.&lt;/p&gt;

&lt;p&gt;In the code example below, the Bird class extends the Animal class. Both have an eat() method. By default, Bird inherits its parent’s eat() method. However, as it also defines its own eat() method, Java will override the original method and call eat() from the child class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class  Animal  {
public  void  eat()  {
System.out.println("This animal eats insects.");
}
}
&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;class  Bird  extends Animal {
public  void  eat()  {
System.out.println("This bird eats seeds.");
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The TestBirdDynamic class first instantiates a new Animal object and calls its eat() method. Then, it also creates a Bird object and calls the polymorphic eat() method again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class  TestBirdDynamic  {
public  static  void  main(String[] args)  {
Animal myAnimal =  new Animal();
myAnimal.eat();
Bird myBird =  new Bird();
myBird.eat();
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The console returns the values of the relevant methods properly because Java could have differentiated the two eat() methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Console output of TestBirdDynamic]
This animal eats insects.
This bird eats seeds.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--N_3JPpFV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mf99ew8up4f6te0al3m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--N_3JPpFV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mf99ew8up4f6te0al3m.png" alt="Inheritance" width="880" height="751"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Summary&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;OOP concepts in Java help you to structure your program more efficiently. The seven object-oriented principles we’ve explored here (abstraction, encapsulation, polymorphism, inheritance, association, aggregation, and composition) can help you reuse your code, prevent security issues, and improve the performance of your Java applications.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Introduction to Java 8</title>
      <dc:creator>ADITYA SASANE</dc:creator>
      <pubDate>Fri, 29 Jul 2022 18:32:33 +0000</pubDate>
      <link>https://dev.to/addysasane77/introduction-to-java-8-27hk</link>
      <guid>https://dev.to/addysasane77/introduction-to-java-8-27hk</guid>
      <description>&lt;p&gt;Oracle rolled out a fresh release of Java on March 18, 2014. This is termed Java 8 and touted as a landmark release with many new as well as useful features. Read on to get acquainted with the features of Java 8. It includes new functionality, upgrades and bug fixes to increase efficiency in designing and operating Java programs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Top Features of Java 8
&lt;/h2&gt;

&lt;p&gt;Below are the top features of Java 8 that make Java 8 more understanding and more useful:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. New Date/Time API&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The old Date-Time API of Java had major drawbacks. In place of it, there is a fresh Date-Time API in Java 8. We take a look at the drawbacks below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tough to Handle Timezone&lt;/strong&gt;: Programmers needed many lines of code to tackle timezone issues.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Low-Quality Design:&lt;/strong&gt; The earlier API had relatively few direct functions for date operations. Java 8 API offers many functions for date operations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Absence of Thread Safe Property&lt;/strong&gt;: java.util.date lacked thread-safe property. Hence programmers had to face concurrency issues while employing data. Java 8 Date-Time API is immutable and without setter methods.&lt;br&gt;
In Java 8 there is a fresh Date-Time API contained in the package java.time. Below are the 2 significant classes contained in the java. time package.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Zoned&lt;/strong&gt;: Specialized API to handle different timezones.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Local&lt;/strong&gt;: Simplified API without the complexity of handling timezones.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.&lt;strong&gt;Nashorn JavaScript&lt;/strong&gt;&lt;br&gt;
The earlier version of Java contained the Rhino JavaScript engine. In Java 8, Rhino is replaced by a superior JavaScript engine namely Nashorn. The latter offers two to ten times superior performance when benchmarked against its predecessor. This is possible as it directly compiles the lines of code in memory followed by passing the bytecode to the Java Virtual Machine. Nashorn employs the invoke dynamics feature of Java 7 in order to enhance performance.&lt;/p&gt;

&lt;p&gt;3.&lt;strong&gt;Optional Class&lt;/strong&gt;&lt;br&gt;
Optional simply put is a container object which is employed to store not-null objects. One major use of the Optional object is to represent null having absent value. This important class has different utility methods to help code handle values as ‘not available’ or ‘available’ rather than to check null values. The class was added in Java 8 and is analogous to what the Optional class is in Guava.&lt;/p&gt;

&lt;p&gt;4.&lt;strong&gt;Base64&lt;/strong&gt;&lt;br&gt;
The new version of Java contains an inbuilt encoder as well as a decoder for the purpose of Base64 encoding. Programmers can employ 3 kinds of Base64 encoding.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;MIME:&lt;/strong&gt; Mapping is done of Output to the MIME format. The representation of the output is done in lines not exceeding 76 characters each. The line separator is a carriage return succeeded by a linefeed. There is no line separator at the termination of the encoded output.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;URL:&lt;/strong&gt; Mapping of Output is done to a group of characters present in A-Za-z0-9+_. The output is filename as well as the URL safe.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simple:&lt;/strong&gt; Mapping of Output is done to a group of characters present in A-Za-z0-9+&lt;em&gt;. No addition of any line feed in the output is done by the encoder. The decoder does accept any character differing from A-Za-z0-9+&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.&lt;strong&gt;Streams&lt;/strong&gt;&lt;br&gt;
The stream is a fresh abstract layer present in this new version of Java. By employing stream, data can be processed in a declarative manner analogous to that of SQL statements.&lt;/p&gt;

&lt;p&gt;Understanding Stream&lt;/p&gt;

&lt;p&gt;Stream simply put is a representation of a sequence of objects emanating from a source that has support for aggregate operations. Below are certain properties of a stream.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Iterations are Automatic&lt;/strong&gt;: Explicit iterations are mandatory in Collections. Whereas in Stream iterations are done internally over the supplied source elements.&lt;br&gt;
&lt;strong&gt;Pipelining&lt;/strong&gt;: The majority of the stream operation’s output is of the stream type. Thus, the output can be pipelined. The particular operations are termed intermediate operations. They accept input, do the necessary processing and give the output to the target.&lt;br&gt;
Some Aggregate Operations supported by Stream:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Match&lt;/li&gt;
&lt;li&gt;Find&lt;/li&gt;
&lt;li&gt;Reduce&lt;/li&gt;
&lt;li&gt;Limit&lt;/li&gt;
&lt;li&gt;Map&lt;/li&gt;
&lt;li&gt;Filter&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6.&lt;strong&gt;Functional Interfaces&lt;/strong&gt;&lt;br&gt;
They display individual functionality. The new version of Java has numerous functional interfaces which can be employed in large measure in lambda expressions.&lt;/p&gt;

&lt;p&gt;7.&lt;strong&gt;Default Methods&lt;/strong&gt;&lt;br&gt;
In Java 8, there is a fresh paradigm of interfaces having default method implementation. The feature is included for the purpose of backward compatibility. It is now possible to use old interfaces to harness the lambda expression capability of the new version of Java.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For instance:&lt;/strong&gt; ‘Collection’ and ‘List’ interfaces lack ‘forEach’ method declaration. If such a method is added the collection framework implementations would be broken. Now, the default method is introduced such that List/Collection contains default implementations of the forEach method. Now the respective methods do not have to be implemented by the class which is implementing these particular interfaces.&lt;br&gt;
8.&lt;strong&gt;Method References&lt;/strong&gt;&lt;br&gt;
This major feature of Java 8 use is to point to relevant methods using their respective names. A “::” symbol describes method references. The latter can be employed to point to the below-mentioned kinds of methods-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Constructors Employing the New Operator&lt;/li&gt;
&lt;li&gt;Instance Methods&lt;/li&gt;
&lt;li&gt;Static Methods&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;9.&lt;strong&gt;Lambda Expressions&lt;/strong&gt;&lt;br&gt;
These are claimed to be the most important as well as the most significant feature of the new version of Java. The former makes functional programming easy and convenient. Moreover, Lambda expressions simplify programming to a great extent.&lt;/p&gt;

&lt;p&gt;Below is a typical lambda expression.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;parameter -&amp;gt; body of expression
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We take a look at the major parts of a lambda expression.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Return Keyword:&lt;/strong&gt; The value is returned by the compiler in case the body contains a single expression. Curly braces signify that the expression returns some value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The parenthesis around the parameter:&lt;/strong&gt; If there is only a single parameter parenthesis can be omitted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type Declaration:&lt;/strong&gt; Parameter type declaration is not needed. From the parameter’s value, the compiler determines the necessary action.
&lt;strong&gt;Miscellaneous Features of Java 8:&lt;/strong&gt; JDBC-ODBC Bridge has been taken off. So. has been the PermGen memory space. The ‘jjs’ command invokes the Nashorn engine while the ‘jdeps’ command analyzes the class files.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Now that you have a theoretical knowledge of Java 8’s new features it is necessary to put them in action. In other words, you have to do the coding that exploits the many useful and valuable features of the new version of Java. Only then will you be truly proficient in Java 8.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Servlet Life Cycle</title>
      <dc:creator>ADITYA SASANE</dc:creator>
      <pubDate>Fri, 29 Jul 2022 17:24:42 +0000</pubDate>
      <link>https://dev.to/addysasane77/servlet-life-cycle-5a0h</link>
      <guid>https://dev.to/addysasane77/servlet-life-cycle-5a0h</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction to Servlet Life Cycle&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This tutorial gives you an understanding of Java Servlets and their life cycle. Before getting started with the Servlet Life Cycle, let us first gain some insight on what exactly is a Servlet and its process. Java Servlet is a class in Java programming language which is defined in Java Enterprise Edition, also known as Java EE. It was developed by Sun Microsystems in the year 1997. After the first version, 1.0 of Servlet, was released in the year 1997, many new versions were released, the latest being Servlet 4.0.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Servlet?
&lt;/h2&gt;

&lt;p&gt;Servlet Technology is very useful in creating web applications as it generates dynamic web pages while residing at the server-side. Java servlets replaced CGI or Common Gateway Interface, which was a scripting language commonly being used as a server-side programming language.&lt;/p&gt;

&lt;p&gt;Servlet is platform-independent, robust and it can access all the Java APIs like the JDBC (Java Database Connectivity) API for accessing the databases of any enterprise.&lt;/p&gt;

&lt;p&gt;Java Servlet is used to create web applications that are dynamic in nature. In order to do so, it extends the server capability. It is capable of running on any web server which has a Servlet container integrated with it.&lt;/p&gt;

&lt;p&gt;The Java Servlet process can be easily understood from the steps mentioned below :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The client sends a request to a servlet container. The client here refers to any browser such as Chrome, IE, Mozilla, etc., in use.&lt;/li&gt;
&lt;li&gt;The container or the Web Server looks for the servlet. As soon as the server finds the servlet, it initiates the servlet.&lt;/li&gt;
&lt;li&gt;Now, the servlet processes the client request, and then a response is sent back to the server.&lt;/li&gt;
&lt;li&gt;This response is sent to the client by the server.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Before we move to the life cycle of a servlet, you should be clear with terminologies used until now in this article. This will be helpful while understanding the Servlet Life Cycle.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Web Server&lt;/strong&gt;: The Web Server or HTTP Server handles HTTP Requests and HTTP Responses. The requests sent by clients are handled, and a response is sent based on the request made by this server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Web Container&lt;/strong&gt; : Web Container or Servlet Container or Servlet Engine interacts with the Servlets. It is an important component of a web server as it manages a servlet’s life cycle.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Life Cycle of Servlet
&lt;/h2&gt;

&lt;p&gt;You can understand the life cycle of a Servlet as a sequence of steps that a servlet undergoes in its span of life, beginning from its initiation to getting destroyed. Servlet Engine manages the life cycle of the servlet, as told earlier.&lt;/p&gt;

&lt;p&gt;The life cycle of a servlet can be summed up in below mentioned five points:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Servlet class is loaded.&lt;/li&gt;
&lt;li&gt;The Servlet instance is created.&lt;/li&gt;
&lt;li&gt;The init() method is invoked in order to initialize the servlet.&lt;/li&gt;
&lt;li&gt;The service() method is invoked repeatedly for each client request placed.&lt;/li&gt;
&lt;li&gt;The servlet is destroyed using destroy() method.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. Loading of the Servlet Class
&lt;/h2&gt;

&lt;p&gt;The servlet class is loaded with the help of the classloader. As soon as the request for a servlet is received by the web container, the servlet class gets loaded.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Creating a Servlet Class Instance
&lt;/h2&gt;

&lt;p&gt;An instance of the servlet is created by the web container as soon as the servlet class gets loaded. Do keep in mind that the creation of a servlet instance is once in a lifetime process for every servlet, which means that it will be instantiated only once in the life cycle of the servlet.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Invoking the Init() Method
&lt;/h2&gt;

&lt;p&gt;After the servlet instance is created, the task of the web container is to invoke the init() method. The init() method is invoked only once and initializes the servlet. If a servlet is invoked by any user, only one instance of this servlet will be created. Every single request results in a generation of a new thread. The data created or loaded by invoking the init() method remains throughout the servlet’s life.&lt;/p&gt;

&lt;p&gt;Please make a note that the init() method will be called only once during the entire life of the servlet.&lt;/p&gt;

&lt;p&gt;The syntax given below will invoke the init method –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void init()throws ServletException
{
/​/code
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Invoking the Service() Method
&lt;/h2&gt;

&lt;p&gt;The service () method is an important method that will be called every time when a request is received for the servlet. The web container is the component that will call the service() method to perform some real work, i.e., to receive requests sent by the client browser and to handle it by sending an appropriate response to the client. As the servlet is initialized, the service method is invoked, and all the HTTP request types are analyzed, such as GET, POST, PUT, DELETE, etc.  After the request types are known, the service() method will dispatch the request to its handler method based on that.&lt;/p&gt;

&lt;p&gt;Consider the case of a POST request made by the client. The job of the service() method is to call the doPost() method and dispatch the request to it. All the requests have their own handler method to which a call will be made by the service() method based on the type of request made. For example, for getting an exception, there is the doGet() handler method; for Put, there is a doPut() method, and so on.&lt;/p&gt;

&lt;p&gt;Please note that the service() method is invoked every time a client request happens. This means that unlike the init() and destroy() method, the service() method can be invoked innumerable times during the servlet life cycle.&lt;/p&gt;

&lt;p&gt;The syntax that you find below will invoke the service() method for Servlet :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void service(ServletRequest req, ServletResponse response) throws IOException,ServletException
{
//code
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Invoking a Destroy() Method
&lt;/h2&gt;

&lt;p&gt;On the occasion of shutting down the web server, the servlet is given a chance to unload all the servlets created. The destroy() method will remove all the initialized servlets, thus cleaning up the memory.&lt;/p&gt;

&lt;p&gt;The syntax for it is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void destroy()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
