Inheritance
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi,
in the following code while constructing A, why the call to the method m refers to the one in class B? What is the rule that governs this? ( I understand m is overrided)
- Thanks
class A {
int x=10;
void m(){System.out.println("A"+ x);}
A(){
System.out.println("constructing A");
m();}
}
class B extends A {
int x=20;
void m(){System.out.println("B"+ x);}
B(){
System.out.println("constructing B"); }
}
public class Try{
public static void main (String args[]) {
A newa = new B();
}}
in the following code while constructing A, why the call to the method m refers to the one in class B? What is the rule that governs this? ( I understand m is overrided)
- Thanks
class A {
int x=10;
void m(){System.out.println("A"+ x);}
A(){
System.out.println("constructing A");
m();}
}
class B extends A {
int x=20;
void m(){System.out.println("B"+ x);}
B(){
System.out.println("constructing B"); }
}
public class Try{
public static void main (String args[]) {
A newa = new B();
}}
Doit
Ranch Hand
Posts: 169
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Help me and help others.
- Thanks.
- Thanks.
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
First of all if a method is overriden, then the actual object determines which method is called. In this case the actual object is B.
Note there are cases where m() in B will NOT be called:
1) m() is not overriden (this is obvious)
2) m() is declared as private in class A
Note there are cases where m() in B will NOT be called:
1) m() is not overriden (this is obvious)
2) m() is declared as private in class A
Doit
Ranch Hand
Posts: 169
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Ken,
Thanks for the explanation. But i am confused again with this...
In this case at the end when a1.giveBirth(a) is called, a1 was
refering to object B. So according to u method in B object should be called. Which is not the case here.
Could u please explain..
- Thanks
class A
{
public A(){};
void giveBirth(A a)
{System.out.println("X");
}
}
class B extends A
{
public B(){}
void giveBirth(B b)
{System.out.println("Y");
}
public static void main(String args[])
{
B a = newB();
A a1 = new A();
a1 = a;
a1.giveBirth(a);
}
}
Thanks for the explanation. But i am confused again with this...
In this case at the end when a1.giveBirth(a) is called, a1 was
refering to object B. So according to u method in B object should be called. Which is not the case here.
Could u please explain..
- Thanks
class A
{
public A(){};
void giveBirth(A a)
{System.out.println("X");
}
}
class B extends A
{
public B(){}
void giveBirth(B b)
{System.out.println("Y");
}
public static void main(String args[])
{
B a = newB();
A a1 = new A();
a1 = a;
a1.giveBirth(a);
}
}
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The method names are always Resolved at the Runtime ,This is called as Dynamic Binding,
In the Following Example
class A
{
public A(){};
void giveBirth(A a)
{System.out.println("X");
}
}
public class B extends A
{
public B(){}
void giveBirth(B b)
{System.out.println("Y");
}
public static void main(String args[])
{
B a = new B();
A a1 = new A();
a1 = a;
a1.giveBirth(a);
}
}
The Method giveBirth() in class B is not Overridden Rather overloaded with the Referece argument b which holds reference to Class B.Note that in methods argument evalaution is not promoted automatically.
so when a1.giveBirth(a) is called The method with the RIght argument is called that is the method which accepts class A as argument.This is available in class B also by virtue of inheritance,otherwise there could have been RUntime Error.
In the Following Example
class A
{
public A(){};
void giveBirth(A a)
{System.out.println("X");
}
}
public class B extends A
{
public B(){}
void giveBirth(B b)
{System.out.println("Y");
}
public static void main(String args[])
{
B a = new B();
A a1 = new A();
a1 = a;
a1.giveBirth(a);
}
}
The Method giveBirth() in class B is not Overridden Rather overloaded with the Referece argument b which holds reference to Class B.Note that in methods argument evalaution is not promoted automatically.
so when a1.giveBirth(a) is called The method with the RIght argument is called that is the method which accepts class A as argument.This is available in class B also by virtue of inheritance,otherwise there could have been RUntime Error.
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hey
Can anyone tell me in the first example why the following is printed.
B0
B20
I understand that variable declaration & intialization are done after the constructors are executed, then it should only print B0. But why is it printing B20 ?
class A {
int x=10;
void m(){
System.out.println("A"+ x);
}
A(){
System.out.println("constructing A");
m();
}
}
class B extends A {
int x=20;
void m(){
System.out.println("B"+ x);
}
B(){
System.out.println("constructing B"); }
}
public class Try{
public static void main (String args[]) {
A newa = new B();
}
}
Can anyone tell me in the first example why the following is printed.
B0
B20
I understand that variable declaration & intialization are done after the constructors are executed, then it should only print B0. But why is it printing B20 ?
class A {
int x=10;
void m(){
System.out.println("A"+ x);
}
A(){
System.out.println("constructing A");
m();
}
}
class B extends A {
int x=20;
void m(){
System.out.println("B"+ x);
}
B(){
System.out.println("constructing B"); }
}
public class Try{
public static void main (String args[]) {
A newa = new B();
}
}
aaaa aaaa
Greenhorn
Posts: 6
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Can anyone help me understand.
Thx in advance.
Thx in advance.
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi
When main method of class try is instantiated, the instance of class is constructed but it refers to the class b not a, so when class be is loaded and constructed u get those values...
hope it clears u little
rajan
When main method of class try is instantiated, the instance of class is constructed but it refers to the class b not a, so when class be is loaded and constructed u get those values...
hope it clears u little
rajan
Doit
Ranch Hand
Posts: 169
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Let me rephrase my question....
Look at the question. I know methods of actual object class are called and variables of object reference class.
But while constructing B, it is calling constructor A, which is correct. In constructor A, the method m is causing me a problem.
Why should it refer to m method in class B , while i am constructing class A constructor???
- Thanks in advance
Look at the question. I know methods of actual object class are called and variables of object reference class.
But while constructing B, it is calling constructor A, which is correct. In constructor A, the method m is causing me a problem.
Why should it refer to m method in class B , while i am constructing class A constructor???

- Thanks in advance
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
To answer aaaa's question, the code given in your post only prints
constructing A
B0
constructing B
But if the method m() were called in B's constructor also, THEN it would have printed
B20
also.
constructing A
B0
constructing B
But if the method m() were called in B's constructor also, THEN it would have printed
B20
also.
| You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |






