Tuesday, January 1, 2019

What is java pojo class, java bean, normal class?

  1. Normal Class: A Java class that is normally create.
  2. Java Beans:
    • All properties private (use getters/setters).
    • A public no-argument constructor.
    • Implements Serializable.
    • Java Beans are reusable software components for Java.
    • They are used to encapsulate many objects into a single object (the bean), so that they can be passed around as a single bean object instead of as multiple individual objects.
    • // Java program to illustrate JavaBeans
      class Bean
      {
          // private field property
          private Integer property;
          Bean()
          {
              // No-arg constructor
          }
        
          // setter method for property
          public void setProperty(Integer property)
          {
              if (property == 0)
              {
                  // if property is 0 return
                  return;
              }
              this.property=property;
          }
        
          // getter method for property
          public int getProperty()
          {
              if (property == 0)
              {
                  // if property is 0 return null
                  return null;
              }
              return property;
          }
      }
        
      // Class to test above bean
      public class GFG
      {
          public static void main(String[] args)
          {
              Bean bean = new Bean();
        
              bean.setProperty(0);
              System.out.println("After setting to 0: " +
                                       bean.getProperty());
        
              bean.setProperty(5);
              System.out.println("After setting to valid" +
                            " value: " + bean.getProperty());
          }
      Output:-
      After setting to 0: null
      After setting to valid value: 5
       
  3. Pojo: Plain Old Java Object is a Java object not bound by any restriction . 
  • Example of Pojo : 
  • // Person POJO class to represent entity Person
    public class Person
    {
        // default field
        String name;
      
        // public field
        public String id;
      
        // private salary
        private double salary;
      
        //arg-constructor to initialize fields
        public person(String name, String id, 
                                 double salary)
        {
            this.name = name;
            this.id = id;
            this.salary = salary;
        }
      
        // getter method for name
        public String getName()
        {
            return name;
        }
      
        // getter method for id
        public String getId()
        {
            return id;
        }
      
        // getter method for salary
        public Double getSalary()
        {
            return salary;
        }
    • Comparison between Java Bean Vs Pojo?
      POJO Java Bean
      It doesn’t have special restrictions other than those forced by Java language. It is a special POJO which have some restrictions.
      It doesn’t provide much control on members. It provides complete control on members.
      It can implement Serializable interface. It should implement serializable interface.
      Fields can be accessed by their names. Fields are accessed only by getters and setters.
      Fields can have any visiblity. Fields have only private visiblity.
      There can be a no-arg constructor. It must have a no-arg constructor.
      It is used when you don’t want to give restriction on your members and give user complete access of your entity It is used when you want to provide user your entity but only some part of your entity.

 

No comments:

Post a Comment

Spring Annotations