• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Inheritance

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}}
 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Help me and help others.
- Thanks.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
aaaa aaaa
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone help me understand.
Thx in advance.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic