Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

DRY (Don’t Repeat Yourself) Principle in Java with Examples

  • Last Updated : 08 May, 2021

DRY is simply an approach, or we can say a different perceptive to programmers. DRY stands for Don’t Repeat Yourself. In Java, it means don’t write the same code repeatedly. Suppose you are having the same code at many places in your program, then It is known as DRY, You are repeating the same code repeatedly at different places. Hence, the solution is obtained using the DRY concept by placing the methods in place of all repeated codes and define the code in one method. So By calling methods, we will reach the principle DRY.

Applications:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  • Online marketing applications
  • education
  • financial applications

Illustration:

Consider the scenario of the college student system. The college contains many departments. So each department has different people, but the college name is the same. So no need to specify the college name for each department by writing the code for the display of the college name.



Implementation: Without DRY approach

Example:

Java




// Java Program without DRY approach
  
// Main class
public class GFG {
  
    // Method 1
    // For cse department
    public void CSE()
    {
        System.out.println("This is computer science");
    }
  
    // Method 2
    // For cse dept. college
    public void college()
    {
        System.out.println("IIT - Madras");
    }
    // Method 3
    // ece dept method
    public void ECE()
    {
        System.out.println("This is electronics");
    }
  
    // Method 4
    // For ece dept college 1
    public void college1()
    {
        System.out.println("IIT - Madras");
    }
    // Method 5
    // For IT dept
    public void IT()
    {
        System.out.println(
            "This is Information Technology");
    }
  
    // Method 6
    // For IT dept college 2
    public void college2()
    {
        System.out.println("IIT - Madras");
    }
  
    // Method 7
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating object of class in main() method
        GFG s = new GFG();
  
        // Calling above methods one by one
        // as created above
        s.CSE();
        s.college();
        s.ECE();
        s.college1();
        s.IT();
        s.college2();
    }
}
Output
This is computer science
IIT - Madras
This is electronics
IIT - Madras
This is Information Technology
IIT - Madras

Implementation: Applying the DRY principle

  • Here we create only one method named college and then call the method in all the departments.

Example 1:

Java




// Java Program with Use of DRY Concept
  
// Importing input output classes
import java.util.*;
  
// Main class
public class GFG {
  
    // Method 1
    // For cse department
    public void CSE()
    {
  
        // Print statement
        System.out.println("This is computer science");
  
        // Calling method
        college();
    }
  
    // Method 2
    // For ece dept method
    public void ECE()
    {
        System.out.println("This is electronics");
  
        // Calling method
        college();
    }
  
    // Method 3
    // For IT dept
    public void IT()
    {
  
        // Print statement
        System.out.println(
            "This is Information Technology");
  
        // Callling method
        college();
    }
  
    // Method 4
    // For college dept
    public void college()
    {
  
        // Print statement
        System.out.println("IIT - Madras");
    }
  
    // Method 5
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating object of class in main() method
        GFG s = new GFG();
  
        // Calling the methods one by one
        // as created above
        s.CSE();
        s.ECE();
        s.IT();
    }
}
Output
This is computer science
IIT - Madras
This is electronics
IIT - Madras
This is Information Technology
IIT - Madras

Implementation: Bank names of a person related to one bank after applying the DRY approach 

Example 2:

Java




// Java Program with Use of DRY Concept
  
// Importing input output classes
import java.util.*;
  
// Main class
public class GFG {
  
    // Method 1
    // For person1
    public void person1()
    {
  
        // Print statement
        System.out.println("sravan");
  
        // Calling Method 3
        bank();
    }
  
    // Method 2
    // For person 2
    public void person2()
    {
  
        // Print statement
        System.out.println("ramya");
  
        // Calling method 3
        bank();
    }
  
    // Method 3
    // For bank
    public void bank()
    {
  
        // Print statement
        System.out.println("SBI");
    }
  
    // Method 4
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating objectof class in main() method
        GFG s = new GFG();
  
        // Calling the methods one by one
        s.person1();
        s.person2();
    }
}
Output
sravan
SBI
ramya
SBI



My Personal Notes arrow_drop_up
Recommended Articles
Page :