4.40 ..i think i am confused and contradicts wat u say..we can call childs method with parents reference.can u explain what happens here in below program? Parent class: public class JavaParent { int x=10; public void disp() { System.out.println("This is parent display and value of x is:"+x); } } Child class: public class JavaChild extends JavaParent { int x=20; public void disp() { System.out.println("this is child method and value of x is:" +x); } public static void main(String[] args) { JavaChild j = new JavaChild(); j.disp(); System.out.println(j.x); JavaParent p = new JavaChild(); p.disp(); //how does this call child method ? System.out.println(p.x); } } O/P: this is child method and value of x is:20 20 this is child method and value of x is:20 10
hi sagar, When you have two methods with same name in two different classes and you want to called child class method but with the reference of other class you can use dynamic binding which is explained in above video in your example you are asking you are asking your parent class to call the method of child class. Your parent can call his method only and child can call his method and parent method also parent class cannot call child methods so here we use dynamic binding JavaParent p = new JavaChild(); your parent class is calling the method of child class
What a explanation... Super handwriting.. 😲WOW
Sir servlet ka bhi lecture uploads kar de please.
Showing AdMob Interstitial ad even after closing the app ..please soluation
learn to write clean
Ok sorry
4.40 ..i think i am confused and contradicts wat u say..we can call childs method with parents reference.can u explain what happens here in below program?
Parent class:
public class JavaParent {
int x=10;
public void disp()
{
System.out.println("This is parent display and value of x is:"+x);
}
}
Child class:
public class JavaChild extends JavaParent {
int x=20;
public void disp()
{
System.out.println("this is child method and value of x is:" +x);
}
public static void main(String[] args) {
JavaChild j = new JavaChild();
j.disp();
System.out.println(j.x);
JavaParent p = new JavaChild();
p.disp(); //how does this call child method ?
System.out.println(p.x);
}
}
O/P:
this is child method and value of x is:20
20
this is child method and value of x is:20
10
hi sagar,
When you have two methods with same name in two different classes and you want to called child class method but with the reference of other class you can use dynamic binding which is explained in above video
in your example you are asking you are asking your parent class to call the method of child class.
Your parent can call his method only and child can call his method and parent method also parent class cannot call child methods so here we use dynamic binding
JavaParent p = new JavaChild();
your parent class is calling the method of child class