public class Main {
public static void main(String[] args) {
Parent obj=new Child(1,2,3); //child constructor accessed during object creation
System.out.println(obj.p);
obj.fun()//error->how can I access constructor of child but not function of child
}
}
public class Parent {
int p;
Parent(int p){
this.p=p;
System.out.println("in parent");
}
}
public class Child extends Parent {
int c1,c2;
Child(int c1,int c2,int p){
super(p);
this.c1=c1;
this.c2=c2;
System.out.println("in child");
}
public void fun(){
System.out.println("fun");
}
}
Q> I know that when variable type is parent object and object type is child, we can access the variables and methods of parent only and not that of child. My question is then how are we being able to access constructor of child?