Java – How to copy classes in Java

How to copy classes in Java… here is a solution to the problem.

How to copy classes in Java

My project consists of two classes: type 1 and type 2, each with its own functions and fields
I will use them as follows:

private void initialize(String type) {
    if (type == "Type1") {
      x = new Type1;
    } else if (type == "Type2") {
      x = new Type2;
    }
}

What type does the X variable have to be?

< Update 1>==

==================================
I

use the super class and interface, but I can’t access the variables and methods of type1 or type2, only the variables and methods of the parent class

< update 2>======

================================

  public class Type1 extends SuperClass{
    public int var = 1;
  }

public class Type2 extends SuperClass{
    public int var = 2;
  }

private void initialize(String type) {
    switch (type) {
      case "Type1":
        x = new Type1();
        break;
      case "Type2":
        x = new Type2();
        break;
    }
  }

void main(){
    int num = x.var;
  }

Not available for conversion in this case: ((Type1)x).var

Solution

Use an interface that will be implemented by two classes

public interface Typeimplemntor{}
public class Type1 implements Typeimplemntor{}
public class Type2 implements Typeimplemntor{}

After that in your function

 private void initialize(String type) {
Typeimplemntor typImp;
        if (type == "Type1") {
          typImp = new Type1();
        } else if (type == "Type2") {
          typImp = new Type2();
        }
      }

A complete example of the update

    public class Test {
        public static void main (String argd[])
        {
            String type= "Type2";
            Typeimplemntor typeimplemntor = null;
            if (type.equals("Type1")) {
                typeimplemntor = new Type1();
            }else if (type.equals("Type2")) {
                typeimplemntor = new Type2();
            }

typeimplemntor.print();
            if (typeimplemntor instanceof Type1) {
                int y = ((Type1)typeimplemntor).x;
                System.out.println(y);
                ((Type1)typeimplemntor).notInInterfaceType1();
            }else if (typeimplemntor instanceof Type2){
                int y = ((Type2)typeimplemntor).x;
                System.out.println(y);
                ((Type2)typeimplemntor).notInInterfaceType2();
            }

}
    }

public class Type1 implements Typeimplemntor {
    int x = 5;
    public void print () {
        System.out.println("Printed from Type1");
    }
   public void notInInterfaceType1 () {
    System.out.println("Not in interface but in Type1");
   }
}

public class Type2 implements Typeimplemntor {
    int x = 15;
    public void print () {
        System.out.println("Printed from Type2");
    }
    public void notInInterfaceType2 () {
       System.out.println("Not in interface but in Type2");
    }
}

public interface Typeimplemntor {
    void print();
}

If you have methods that will be used in both classes, you can define them in an interface and access them directly based on their implementation in the class. If there are some other methods or variables, then you have to check the instance and then convert it to the same.

Related Problems and Solutions