Here, I created 5 classes named as child1, child2 childrens, Mom and HelloWorld. I created a variable pcount in childrens class that is exnted by two class child1 and child2.
This is the code for reference.
import java.util.*;
class Mom{
public void fun(){
child1 ch11=new child1("child1");
child2 ch22=new child2("child2");
for (int i=0;i<HelloWorld.al.size();i++){
if(HelloWorld.al.get(i)==1){
ch11.pcount++;
}else{
ch22.pcount++;
}
}
}
}
class Childrens{
String name;
public int pcount=0;
}
class child1 extends Childrens{
child1(String n){
name=n;
}
}
class child2 extends Childrens{
child2(String n){
name=n;
}
}
class HelloWorld{
static ArrayList<Integer> al=new ArrayList<>();
public static void main(String[] args){
child1 ch1=new child1("child1");
child2 ch2=new child2("child2");
Mom m=new Mom();
al.add(1);
m.fun();
System.out.println(ch1.pcount);
System.out.println(ch2.pcount);
}
}
Output came is: 0 0
but Expected output is: 1 0
So, Can you help me to figure out what is the problem in this code?