• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Inheritance

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test{

public static void main(String []kk){
int y=9;
long b=9;
A obj=new B();
obj.meth(y);
new B().meth(b);
}
}

class A{
public void meth(long u){
System.out.println("parent");
}
}


class B extends A{
public void meth(int aa)
{
System.out.println("child");

}


}

Result is:
parent
parent


why in both cases the parent version of meth gets called?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amarkirt,

In Class B we are overloading the method meth() and not overriding it.
When you are overloading, the reference type is considered and hence in both the calls to meth() "parent" was getting printed.

Let me know if you need any more explanation.

Thanks,
Jaimesh.
 
amarkirt saroay
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jammy ! got it


Originally posted by jammy ponkia:
Hi Amarkirt,

In Class B we are overloading the method meth() and not overriding it.
When you are overloading, the reference type is considered and hence in both the calls to meth() "parent" was getting printed.

Let me know if you need any more explanation.

Thanks,
Jaimesh.

 
Straws are for suckers. Now suck on this tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic