Java – How do you create a class when properties in Java, C++, or any object-oriented language are dynamic and mutable?

How do you create a class when properties in Java, C++, or any object-oriented language are dynamic and mutable?… here is a solution to the problem.

How do you create a class when properties in Java, C++, or any object-oriented language are dynamic and mutable?

Okay, in

object-oriented language (OOL), when we create a class, we usually know all its properties in advance. For example, the Item class should have a fixed property (color, model, brand, price). So we just:

   public Class Item{
     private String color;
     private String model;
     etc more attribute here

& set & get method for all attributes
     public String getColor() {
         return color;
     }

public void setColor(String color) {
        this.color = color;
     }

public String getModel() {
         return model;
     }

public void setModel(String model) {
         this.model = model;
     }
    }

But what if all properties are dynamic? For example, in 1 company, their project attributes might be color, brand, but in other companies, they don’t have color and brand attributes, but they have width, height, size….

How do I create a class that accepts dynamic properties in Java, C++, or any OOL?

Solution

How to create a Class that accepts dynamic attributes in Java, C++ or in any OOL?

It really depends on how you want to use it. In many cases, you can modify your class to include some type of dynamically growing collection, such as std::map in C++ or in Map (or Dictionary) In Java.

This allows you to create and add arbitrary data for each instance using the key selected at runtime.

Related Problems and Solutions