This week's book giveaway is in the Spring forum.
We're giving away four copies of Modern frontends with htmx and have Wim Deblauwe on-line!
See this thread for details.
Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

which method is invoked and why?

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

In the above code i typecasted the "c" reference to A type. When i invoke the method meth() on "a" reference method meth() of class B is invoked rather than Class A method..why is that so..
[ September 29, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abhishek Reddy Chepyala:
class A
{
protected void meth()
{
System.out.println("meth() of A called");
}
}
class B extends A
{
protected void meth()
{
System.out.println("meth() of B called");
}
}
class C extends B
{
public static void main(String args[])
{
C c=new C();
A a=(A)c;
a.meth();//------which method is invoked and why-
System.out.println("exited main");
}
}

In the above code i typecasted the "c" reference to A type. When i invoke the method meth() on "a" reference method meth() of class B is invoked rather than Class A method..why is that so..



Ok Let me explain,

Please correct me other ranchers If I am wrong,

Prior to explain this I would like to introduce one example



What do you think the output is?

Correct Output is: in child

Anyways now as you see in the your code that C is direct subclass of Class B
So it will search the called method in it body itself but if it failed then it goes into the direct superclass that is B

There it found the method so it executes the method given in Class B

It is the exmple of run Time Binding.
Check this out PolyMorphism-Run Time Binding
 
Abhishek Reddy
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Sample extends ABC {
public void add() {
System.out.println(" in child");
}
public static void main(String arg[]) {
ABC a = new Sample();
a.add();
}
}
class ABC {
public void add() {
System.out.println(" in Parent");
}
}

At runtime reference "a" is pointing to SampleObject so, the behaviour..its fine....now tell me what happens when we typecast the refrence "a" to ABC type ie ABC a1=(ABC)a;...will it still point to Sample object..
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abhishek Reddy Chepyala:
public class Sample extends ABC {
public void add() {
System.out.println(" in child");
}
public static void main(String arg[]) {
ABC a = new Sample();
a.add();
}
}
class ABC {
public void add() {
System.out.println(" in Parent");
}
}

At runtime reference "a" is pointing to SampleObject so, the behaviour..its fine....now tell me what happens when we typecast the refrence "a" to ABC type ie ABC a1=(ABC)a;...will it still point to Sample object..



Yes ofcourse it will still the same Sample Object.
Run this code and then check.



Again the same output is there: in child.

Please let me know if there are any issues.
[ September 29, 2006: Message edited by: Ankur Sharma ]
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankur,

So casting is applicable only to references and not for actual objects...am I right??Please clarify.

Regards,
Jothi Shankar Kumar. S
 
reply
    Bookmark Topic Watch Topic
  • New Topic