• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Back to basics - Polymorphism ......

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
Can anyone explain to me about the following issue i am having

Its seems in Polymorphism

"Reference variable type determines the methos to be called " and
"Polymorphic method invocation apply only to overidden instance methods."

In overriding it says "Object type not the reference variable type determines which overridden method is used at runtime."

Do you know how this works?


Cheers,
Johns

 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check out this Javaranch Polymorphism

Say,

A a = new B(); // assuming B is subclass of A

Here A is called refernce type for a and B() is called object type. And with thsi info check out teh above article.
 
Jones Iraland
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
My question is how come if polymorphism invokes methods according to reference variable type and only do overriding ,but in overiding methods invocation happens according to object type and not reference type.

Cheers,
Johns
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jones

Jones Iraland wrote:Hi,
polymorphism invokes methods according to reference variable type and only do overriding ,but in overiding methods invocation happens according to object type and not reference type.

Cheers,
Johns



If i understood your question correctly.. this is polymorphism..


In overriding it says "Object type not the reference variable type determines which overridden method is used at runtime."



During compile time , compiler checks for the reference type and if it has called methods in it.. only during runtime , the object that gets created will call its own inherited method ( if method is not overridden) or overriden method (if overrideen) . wats your doubt ?
 
Jones Iraland
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Balu,
I think i got it.Thanks for explanation.

I wrote this program to get it more clear in my head.

public class Poly extends PolyParent{
public void go(){
System.out.println("Inside Poly");
}
public static void main(String args[]){
PolyParent p=new Poly();

p.go();
}
}

class PolyParent{
public void go(){
System.out.println("Inside PolyParent");
}
}


If I commented PolyParent class go() method it will not compile.when I run this method it realy invokes Poly class go() method.

So Polymorphism happens at compile time actual method will be called depends on run time.

Have I got it correct?

 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, you are right !!

try changing access specifers (coderanch, private) to understand overriden rules..
 
Jones Iraland
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mate cheers
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jones Iraland wrote:
So Polymorphism happens at compile time actual method will be called depends on run time.



Polymorphims happens at compile time?? Does the action of invoking methods dynamically has anything to do with Polymorphim?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic