Tricky Interview Question | Difference Between POJO and JavaBean class | POJO vs Java Bean

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this video we are going to discuss  about the differences between pojo and java bean   both pojo and java bean are the classes used  to define java objects for more readability   let us understand with an example let me  create a pojo class by the way pojo is   plain old java object i am going  to name my class as student and this student can have  name attribute can have marks you can have roll number as  well right so and roll number   and we wanted to initialize the values to  this for that i am going to have a constructor using the constructor we are going  to pass the values to this attributes in roll number and marks we will initialize the passed parameters  to these attributes roll number equal to passed roll number and this marks is equal to the   parameter marks which is passed to the constructor  right now how to read the attributes so for   that we will generate the getters so let's  generate getters for these three attributes now let me create a new class where  i'll name my class as pojo main where we are going to have the main method here and i am going to create a new object  student object and we need to pass   parameter values to this  right the first one being name i'll pass it as mahesh and second attribute is  roll number right yeah roll number let's say   for example 101 marks say for example 75 okay  and intentionally i am going to modify the name so if you observe name is declared as default  right so i am able to modify the name here right   and then now let's actually  call the getters get name get marks sorry s dot get roll number and then  we'll have the marks s dot get marks   now let us execute this program and we can see the output here so we passed mahesh  and we modified the name to Mahesh Babu right   so we can see the name roll number and marks  get printed here so this is what we call it as a   pojo class pojo is plain old java object and we  can see the structure here so it need not be in a   conventional format so it can be  uh it we can have any attributes   uh meaning like it can be of any data type it can  have any access modifier we can either have the   constructor or not this is not mandatory and we  can have getters setters or only getters as well   this is the structure of the pojo now let  us understand what java bean is so let   beans are used for the enterprise java application  where users access the data from the server via   network right so the data has to be streamed into  serial format into and out of the user's machine   that's the reason we definitely need to implement  the serializable interface for a java bean class and the second thing is we whatever the  data member inside java bean they has to be   private so all the attributes of the java  bean class has to be private so employee can   have employee id which has to be private  as we are going to define this class as a   javabean class right in our  string we can have employee name maybe we can have salary and another thing is java bean class  has to have a no argument constructor   so let's have no argument constructor and a java bean class should always have   both getters and setters so let's generate  both getters and setters for all the attributes this is our java bin class so what  we have serializable interface   that has to be implemented by the javabean  class all the data attributes should be   private a java bin class has to have an  argument constructor and it has to have a   getters and setters for the attributes  and all these has to be public to access   the private attributes now let us create a  new class which we'll call it as bean main where we are going to create the main method  create an object for the employee class and we are going to set the attributes here  employee which can be something like this   e dot set name e dot set salary some 10 000 like that and how do we actually uh  read these values using the getters e dot get name get employee id get salary and we can see here is output right so the name   employee id and salary and now let's actually see  the differences between pojo class and java bean so if you observe the first difference is like  java bean class has to implement the serializable   interface and pojo need not actually extend  any class or it's not mandatory to definitely   implement any interface and here if you observe  pojo class can have any any access modifier for   its attributes but all the attributes in java bean  class has to be private and pojo class can have a   constructor it can be no arg constructor it can be  parameterized constructor or it need not have the   constructor but java bean class has to have the  no argument constructor it's mandatory right and   you can either have getters and setters or only  getters in the pojo class but in java bin we   should definitely have the getters and setters  and that too with the access modifier as public so these are the main differences between pojo and  java bin class so simply we can say all java bean   classes are pojos but all pojos need not be the  java beans so these are the main differences and   in this video we discussed about pojo what's pojo  class what java bin class and what are the main   differences between pojo class and java bean class  that's all for this video thanks for watching
Info
Channel: TechStack9
Views: 8,772
Rating: undefined out of 5
Keywords: Java, Tricky, Interview, Questions, interview, javaprogramming, coding, java, javadeveloper, programming, programmer, programminglife, programminglanguage, backenddeveloper, backend, developer, interviewchallenge, interviewquestions, interviewpreparation, codinginterview, interviewskills, interviews, POJO, Java Bean, Plain Old Java Object, Serializable
Id: T3z9Rwbz-c8
Channel Id: undefined
Length: 8min 58sec (538 seconds)
Published: Sun Jul 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.