answersLogoWhite

0


Best Answer

Polymorphism is Greek for "many forms". There are two types of polymorphism: static and dynamic. Static polymorphism occurs at compile time and is also known as compile time polymorphism. Dynamic polymorphism occurs at runtime and is also known as runtime polymorphism.

Runtime polymorphism is a primary feature of the object-oriented programming paradigm. We achieve runtime polymorphic behaviour by defining virtual methods in a base class which derived classes can then override in order to provide more specialised implementations. Whenever we invoke one of these methods, the most-specialised override is executed automatically. In other words, polymorphic objects will behave according to their runtime type even when the runtime type cannot be determined at compile time.

User Avatar

Wiki User

βˆ™ 8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 14y ago

In run time polymorphism compiler doesn't know about way of execution of program that mean decide way of execution at run time while in compile time polymorphism compiler know about the way of execution of program.

Example : compile time polymorphism -- method overloading

run time time polymorphism -- method overriding

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 13y ago

Run time polymorphism maybe achieved by implementing a particular interface among different class hierarchies, or using the same delegate from different objects. (delegate maybe thought as an interface to invoke methods with different names but with the same parameters. These methods may or may not be of the same object/class)

Compile time polymorphism is achieved by classinheritance or implementing an interface.

Run time way means the actual implementing class is UNKNOWN (uncertain) at compile time, and maybe injected into your application in real time (through reflection is one way to get dynamic classes). Yet because the runtime object implements the predefined interface (at compile time), the "polymorphism" is achieved.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 10y ago

Whenever you declare or inherit a virtual function, you automatically create a virtual table for that class. The virtual table (or v-table) consists of one function pointer per virtual function, such that each pointer points to the most-derived override of the function. If a virtual function is not explicitly overridden by a class, the v-table entry points to the most-derived base class override instead. Whenever you instantiate any object within the hierarchy, its v-table will determine which specific override to invoke whenever a virtual method is called implicitly, regardless of where the call originates.

Consider the following example:

#include<iostream>

using namespace std;

struct A

{

virtual void foo() { cout << "calling A::foo()\n"; }

};

struct B: A

{

virtual void foo() { cout << "calling B::foo()\n"; }

};

void polymorphism(A& a)

{

a.foo();

}

int main()

{

A a; a.foo();

B b; b.foo();

poly(b);

}

Output:

calling A::foo()

calling B::foo()

calling B::foo();

In the above example, class A declares foo() to be virtual so class A effectively has a virtual table with a single pointer to A::foo(). B inherits from A and therefore has its own virtual table. B::foo() overrides A::foo(), so its virtual table entry points to B::foo(), the most-derived override. Had B not overridden this method, the entry would point to A::foo() instead, that being the most-derived base class override.

In the main function we instantiate an A object named 'a' and call the a.foo() method, thus invoking A::foo() as expected. We then instantiate a B object named 'b' and call b.foo(), invoking B::foo(), also as expected.

We than pass b into the polymorphism function. This is where runtime polymorphism comes into play. Note that the function expects a reference to an A object. Although the function knows absolutely nothing about B objects whatsoever, it invokes the B::foo() method when we pass a B object to it. This is entirely expected behaviour and shows runtime polymorphism at work. That is, the function did not need to switch on the runtime type of the object in order to invoke its more specialised behaviour.

The reason it works is because B inherits from A. Objects instantiated from B are really just more specialised instances of A, hence the compiler doesn't complain when we pass a B object into a function that expects a reference to an A object. Although the function doesn't know anything about B object specialisation, the v-table ensures it doesn't have to. The call is simply passed through the v-table pointer and since the object is really a B object, B::foo() is invoked rather than A::foo().

IMPORTANT! It should be noted that if you declare any virtual function, you must also declare the destructor virtual. This ensures that regardless of which object in the hierarchy falls from scope first, the most-derived destructor will always be invoked first, and the hierarchy will be destroyed in the reverse order it was constructed. Failure to declare the destructor virtual could result in leaked memory whenever a base class falls from scope or is explicitly deleted by code.

As a general rule, all destructors should be declared virtual even if there are no virtual methods in the class itself or in any of its base classes. The rule is only general because there will be some isolated cases where it would be undesirable for a class to be used as a base class, hence the default behaviour is non-virtual destruction. In some languages, all methods, including destructors, are declared virtual unless explicitly stated otherwise, but not C++. This is primarily to retain backward compatibility with the C-style struct where the virtual keyword would be meaningless.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 12y ago

we can achieve by the help of overloading and overrding cocepts and this two

methods are help ful in dynamic and static polymorphism.....

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How does java achieve run time polymorphism?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is compile time polymorphism in java?

Compile Time Polymorphism in Java is when you have the several methods with same name and different parameters and compiler has to decide how to select which method has to run based on the arguments hence the name Compile time polymorphism or method overloading.


What is Difference between dynamic polymorphism and static polymorphism with example?

Static polymorphism is used the concept of early binding or we can say compile time binding where as dynamic polymorphism used the concept of late binding or run time binding.


What is dynamic method dispatch in java?

Dynamic Method Dispatch in Java is when a method call is resolved at runtime based on the actual object the reference points to. For example, if a parent class has a method and a child class overrides it, a parent reference pointing to a child object will execute the child’s version of the method. This allows Java to decide which method to run during execution, enabling runtime polymorphism. To dive deeper into this topic, visit the Uncodemy blog for detailed insights.


Which brands of laptops can run JAVA?

Any computer (desktop or laptop) can run Java.


Why run time polymorphism is dynamic and compile time polymorphism is static?

The simple answer is that compile-time polymorphism occurs at compile time while runtime polymorphism occurs at runtime. The actual answer is that compile-time polymorphism results in the compiler generating source code on your behalf while runtime polymorphism relies on function pointers or virtual methods to determine the next instruction at runtime. Compile-time polymorphism therefore applies to template functions and classes since that is the only way the compiler can generate source code on your behalf. To achieve this, the runtime type for the template parameters must be fully-defined at compile time, even if those types have runtime polymorphic characteristics of their own. Runtime polymorphism applies to virtual methods and function pointers, both of which can be used to dynamically alter the execution path of your program. Virtual methods are made possible through virtual tables, which are essentially just arrays of function pointers. Each runtime type that derives from a base class with virtual methods provides its own virtual table, thus the runtime type determines which specific function overrides will be invoked at runtime, even if the runtime type cannot be determined at compile time. In this way you can generalise your code to work with the base type but still get the expected polymorphic behaviour whenever a derived type is passed instead.

Related questions

What is compile time polymorphism in java?

Compile Time Polymorphism in Java is when you have the several methods with same name and different parameters and compiler has to decide how to select which method has to run based on the arguments hence the name Compile time polymorphism or method overloading.


What is the difference between compile time and run time polymorphism?

Runtime prolymorphism means overriding compiletile polymorphism means overloading


What is Difference between dynamic polymorphism and static polymorphism with example?

Static polymorphism is used the concept of early binding or we can say compile time binding where as dynamic polymorphism used the concept of late binding or run time binding.


How is run time polymorphism accomplished?

Through inheritance and virtual functions.


What is Dynamic Polymorphism?

Dynamic polymorphism is a programming method that makes objects with the same name behave differently in different situations. This type of programming is used to allow Java Scripts to run while playing a game on the computer, for example.


What is polymorphism and its types?

Polymorphism means multiple form of a function, variable or object. In Computer Science, polymorphism is a programming language feature that allows values of different data types to be handles using a common interface. There are three types : Ad-Hoc Polymosphism, Parametric Polymorphism, Subtype/Inclusion Polymorphism. Source: Wikipedia.


Difference between a java complier and the Java JIT?

A java compiler takes Java source code and turns it into Java bytecode, which can then be run by the java virtual machine.Using JIT means that the java code will be compiled and executed at the time that you run the program, which will slow down the program because it has to compile the code at the same time that it runs.


I've updated java 3 times and when I try to use java I get an error saying my java is out of date I've tried hitting the run this time button it doesn't work?

Just press the button that says run time, or something like that, unfortunateley you have to press that button every time you use a java thingamajig


How can you pass run-time arguments in java?

Once you have compiled your Java source files: javac MyClass.java You can run the resulting class file and pass arguments: java MyClass arg0 arg1 arg2


What is dynamic method dispatch in java?

Dynamic Method Dispatch in Java is when a method call is resolved at runtime based on the actual object the reference points to. For example, if a parent class has a method and a child class overrides it, a parent reference pointing to a child object will execute the child’s version of the method. This allows Java to decide which method to run during execution, enabling runtime polymorphism. To dive deeper into this topic, visit the Uncodemy blog for detailed insights.


Difference between run time error and run time exception in java?

Runtime Error Cannot be Rectified but Runtime Exception can.


Do you have to have java on your coumpter to run everything on your coumpter?

No; lots of programs run without Java. You only need the Java runtime to run programs specifically designed with Java technology.