Java Beans - Notes by Shariq SP

Understanding Java Bean Class

A Java Bean is a reusable software component that follows specific conventions for its properties, methods, and events. Java Beans are commonly used in Java application development for encapsulating data and functionality.

To create a Java Bean class, you typically follow these conventions:

  • Private member variables with public getter and setter methods for each variable.
  • A public no-argument constructor.
  • Implementing Serializable interface for supporting serialization.

Example 1: Java Bean Class

In this example, we have a simple Java Bean class Person:


            import java.io.Serializable;
        
            public class Person implements Serializable {
                private String name;
                private int age;
                
                public Person() {
                    // Default constructor
                }
                
                public String getName() {
                    return name;
                }
                
                public void setName(String name) {
                    this.name = name;
                }
                
                public int getAge() {
                    return age;
                }
                
                public void setAge(int age) {
                    this.age = age;
                }
            }
            

Example 2: Java Bean Class

In this example, we have another Java Bean class Product with more properties:


            import java.io.Serializable;
        
            public class Product implements Serializable {
                private String name;
                private double price;
                private int quantity;
                
                public Product() {
                    // Default constructor
                }
                
                public String getName() {
                    return name;
                }
                
                public void setName(String name) {
                    this.name = name;
                }
                
                public double getPrice() {
                    return price;
                }
                
                public void setPrice(double price) {
                    this.price = price;
                }
                
                public int getQuantity() {
                    return quantity;
                }
                
                public void setQuantity(int quantity) {
                    this.quantity = quantity;
                }
            }
            

Interview Questions on Java Bean

  1. What is a Java Bean?
  2. What are the conventions to create a Java Bean class?
  3. Why are getter and setter methods important in a Java Bean?
  4. What is the purpose of implementing the Serializable interface in a Java Bean?
  5. Can a Java Bean class have methods other than getter and setter methods?

Multiple Choice Questions (MCQs) - Java Bean

  1. Which of the following is a convention for creating a Java Bean class?
    1. Public member variables
    2. Private member variables with public getter and setter methods
    3. No constructor
    4. No methods other than main method
  2. Why are getter and setter methods important in a Java Bean?
    1. To make member variables accessible from outside the class
    2. To encapsulate the behavior of the class
    3. To provide a default constructor
    4. To ensure thread safety
  3. What is the purpose of implementing the Serializable interface in a Java Bean?
    1. To make the class immutable
    2. To allow the class instances to be serialized
    3. To ensure type safety
    4. To provide inheritance
  4. Can a Java Bean class have private getter and setter methods?
    1. Yes
    2. No
    3. Only if the class is final
    4. Only if the class is abstract
  5. What is the purpose of the default constructor in a Java Bean class?
    1. To initialize member variables with default values
    2. To ensure thread safety
    3. To restrict instantiation of the class
    4. To provide type safety