Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How does java implements runtime polymorphism.?

807607Nov 23 2006 — edited Nov 23 2006
Hi all.

we know how does runtime polymorphism take place in java.

But my quesions is , how does java implement it internally.

in C++, with the use of keyword virtual , complier decides to make a call at runtime using virtual table and Vptr.

like in c++,

class base {


public:
virtual void show(){
cout<<"I am in base class"<<endl;
}
};

class child : public base {

public:
void show(){
cout<<" I am in child class"<<endl;
};

int main(){
base*p = new child();
p->show();

return 0;
}

out put - I am in child class.

but if we remove the virtual keyword then output

I am in base class..

We know how it happens

but in java

class base {
void show(){
System.out.println("I am in base class");
}
}

class child extends base {

void show(){
System.out.println(" I am in child class");
}
}
class demo {

public static void main(string args[ ]){
base b;
child c = new child();
b = c;
b.show();
}
}
output is - I am in child class

but how can i bring the output as

I am in base class ---

complier knows that b is base reference so y doesnt it just use base version.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 21 2006
Added on Nov 23 2006
2 comments
558 views