Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Copy class in java

Tags:

java

android

My project consists of two classes: type 1 and type 2 is that each has its own functions and fields I'm gonna use them as follows:

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

What type of X variable must be ?

<Update 1>=============================

I use superclass and interface but I do not have access to variables and methods of type1 or type2 and only have access to variables and methods of the superclass

<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;
  }

In this case can not be used to cast : ((Type1)x).var

like image 813
Delay Avatar asked Aug 27 '26 06:08

Delay


1 Answers

Use an interface which will be implemented by both 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();
        }
      }

Complete example for 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 method which will be used in both the classes you can define them in interface and they can be accessed directly according to their implementation in classes. If there are some other methods or variables then you have to check the instance and then you have cast into the same.

like image 50
Mandeep Singh Avatar answered Aug 28 '26 20:08

Mandeep Singh