21 How to create object using User Define Factory Method in Java ?

In this I created one class "FMDemo" in that creating object of classes "Employee" and "ITCompany". But the "ITCompany" class constructor is private and "Employee" class has only one non-static data member. So how to solve this problem shown in below:

FMDemo.java

class Employee{
String empName = "Atharva Agrawal";
 
}

class ITCompany{

private ITCompany() {

}

String itcName = "Coding Atharva";

public static ITCompany getObjectForITCompany(){
return new ITCompany();
}

Employee getObjectForEmployee(){
return new Employee();
}

}

class FMDemo{
public static void main(String[] s){

ITCompany itc = ITCompany.getObjectForITCompany();
System.out.println("Company Name: "+itc.itcName );

Employee e = itc.getObjectForEmployee();
System.out.println("Employee Name: "+e.empName);
}

}

Output:  

.
Previous
Next Post »