answersLogoWhite

0


Best Answer

Polymorphism means "having many forms", and is used to mean that two different object types (ex. LinkedList and ArrayList) can be used as if they were the same type.

In Java polymorphism takes the form of subclassing: Make both LinkedList and ArrayList inherit from List (extend a common superclass or implement a common interface), and the code can call List methods without caring about which particular type of list it is working on.

This is polymorphism since two different types (LinkedList and ArrayList) can be treated as one type (List).

In duck-typing, used for example in Ruby and JavaScript, types need not have a common supertype to be treated the same, they need only have methods with the same name. (Hash, String and Array might all have a method "size". If so, code that uses only this method can treat all three types as the same.)

Both of these mechanisms let the programmer treat different types as the same type, so both are examples of polymorphism.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago
  • it can be achieved by using operator and function overloading. some function name can be used for different functions with argument differences
  • default operators works fine for built in data types.however to use them on user defined data types, user needs to implement their own operators to operate on user defined object types.

#include

#include

class poly

{

int x;

double f;

public:

poly(){}

poly(int x1)

{

x = x1;

f = 0.0;

}

poly(double f1)

{

f = f1;

x=0;

}

void display()

{

cout<<"value of x : "<

}

poly operator +(poly);

};

poly poly::operator +(poly obj5)

{

poly p1;

p1.x = x + obj5.x;

p1.f = f + obj5.f;

return p1;

}

main()

{

poly obj1(10), obj2(20), obj;

obj = obj1+obj2;

cout<<"After integer addition :\n";

obj.display();

poly obj3(10.3), obj4(20.47);

obj = obj3 + obj4;

cout<<"After decimal value addtion :\n";

obj.display();

getch();

return 0;

}

NOTE : constructor func is overloaded and + operator is also implemented to add two objects and in both function execution ,bound with the object at compile time.so it is known as compile time polymorphism.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

• Compile time Polymorphism also known as method overloading

• Method overloading means having two or more methods with the same name but with different signatures

LEarn Design Pattern,C#,ASP.NET,LINQ,Sharepoint,WCF,WPF,WWF

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Method overriding is run
time polymorphism, because method of which class will be called is determine at run time.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How is polymorphism achieved at compile time?
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.


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.


How can be different forms of polymorphism achieved?

Implicit Parametric PolymorphismSubtype PolymorphismExplicit Parametric Polymorphism


How does java achieve run time polymorphism?

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.

Related questions

Define compile time polymorphism with short examples?

compiler can decide which form of the object should be invoked during compile time.this type of polymorphism is know as compile time polymorphism


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

Runtime prolymorphism means overriding compiletile polymorphism means overloading


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.


Is late binding and dynamic binding related to polymorphism?

Late binding and dynamic binding are related to runtime polymorphism. By contrast, compile time polymorphism is known as static binding. Template functions and classes are examples of static binding because the exact type can be determined at compile time.


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 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.


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.


How can be different forms of polymorphism achieved?

Implicit Parametric PolymorphismSubtype PolymorphismExplicit Parametric Polymorphism


Is overriding done at compile time or run time?

compile time


How does java achieve run time polymorphism?

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.


What is dynamic dispatching?

Dynamic dispatching, also known as late binding, is a technique used in object-oriented programming where the method to be called is determined at runtime based on the actual type of the object, rather than at compile time. This allows for flexibility in the behavior of objects and enables polymorphism.


Polymorphism in coelentrates?

polymorphism in coelentrates