Polymorphism is the feature that allows one interface to be used for general class actions.
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area (void) =0;
void printarea (void)
{ cout << this->area() << endl; }
};
class CRectangle: public CPolygon {
public:
int area (void)
{ return (width * height); }
};
class CTriangle: public CPolygon {
public:
int area (void)
{ return (width * height / 2); }
};
int main () {
CPolygon * ppoly1 = new CRectangle;
CPolygon * ppoly2 = new CTriangle;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly1->printarea();
ppoly2->printarea();
delete ppoly1;
delete ppoly2;
return 0;
}
By
MUTHU
Polymorphism is the association between a generalized reference and a more specific object. For instance, consider the following code:
Animal a = new Dog(); //right
Dog d = new Animal(); //wrong
In the first example, the reference ais referencing a Dog object. So when a method call is made, such as:
a.eat();
the eat method is carried out by the Dog object that areferences.
The second example is incorrect, because not all Animals can perform Dog actions, so a method call of d.bark() does not make sense, because not all Animals can bark.
same action can act differently on different object
ex- operation move can be different for Chess and a person
1. Single Inheritance
A Scenario where one class is inheriting/extending the behavior of just one super class.
Ex: public class Ferrari extends Car {…}
2. Multilevel Inheritance
A Scenario where one class is inheriting/extending the bahavior of another class which in turn is inheriting behavior from yet another class.
Ex: public class Automobile {…}
Public class Car extends Automobile {…}
Public class Ferrari extends Car {…}
This multilevel inheritance actually has no limitations on the number of levels it can go. So as far as java goes, it is limitless. But for maintenance and ease of use sakes it is better to keep the inheritance levels to a single digit number.
Polymorphism can be considered as the ability of one thing being multiple other things (though partially or fully). Am I confusing you? I believe yes. To put it in simpler words, any java object that can pass more than one Is-A test can be considered polymorphic
There are two important concepts in polymorphism
• Method Overloading and
• Method Overriding
Overridden Methods
Any time you have a class that inherits a method from a superclass, you have the opportunity to override the method (unless, as you learned in the earlier chapters, the method is marked final). The key benefit of overriding is the ability to define behavior that's specific to a particular subclass type. The following example demonstrates a Porsche subclass of Car overriding the Car version of the drive() method:
public class Car {
public void drive() {
System.out.println("Generic Car Driving Generically");
}
}
class Porsche extends Car {
public void drive() {
System.out.println("Porsche driving Full Throttle");
}
}
Overloaded Methods
Let us say we have the Ferrari class with a method drive() as below:
Public class Ferrari extends Car {
Public void drive() {…}
}
Below are the legal overload method declarations:
1. public void drive(String destination, String roadToTake) {…}
2. public void drive(String destination, int timeToTake) {…}
3. public void drive(String destination) throws RouteNotFoundException {…}
Assuming that the Ferrari class has the above 3 methods, below are is an illegal overload:
1. public String drive (String destination, String roadToTake) {…} - You cannot have two methods which differ just in the return type. This is not allowed
Polymorphism is used in Java when we want multiple things to represent the same entity. The usage definition might be misleading but, powerful features like Method Overloading and Method Overriding can be used to tap the potential of polymorphism in Java and implement complex functionalities with ease.
polymorphism is more than one name with different forms
By implementing polymorphism we need to use overloading and overriding Technics in program..
Polymorphism means The ability to take more than on form
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.
Actually java is not purely object oriented.because we can use the primitive data types in Java.In java all those things or considered as classes and objects .So we are called java is an object oriented programming language...
Abstraction in Java or Object oriented programming is a way to segregate implementation from interface and one of the five fundamentals along with Encapsulation, Inheritance, Polymorphism, Class and Object. Abstraction in Java is achieved by using interface and abstract class in Java.
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.
Polymorphism, is an object-oriented programming concept, which relates to the ability to create a variable, function or an object that has more than one form. This allows the object to invoke the correct instance of the variable, function or other object based upon the object type. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Here are some links to examples: C++: http://www.cplusplus.com/forum/beginner/10884/ c#: http://msdn.microsoft.com/en-us/library/ms173152.aspx Python: http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Java: http://www.tutorialspoint.com/java/java_polymorphism.htm
Yes.
No. Interfaces in Java are a construct to get polymorphism ( subtype polymorphism ) working in Java, but they are not a "kind" of polymorphism. In polymorphism happens when two objects respond to the same message ( method call ) in different way ( hence poly -> many, morphism -> way or shape : polymorphism -> many ways). In Java to be able to send the same message to two different objects you have to either inherit the same parent, or implement the same interface.
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.
Yes. Java is an Object Oriented Programming Language and it supports the OOPS concepts like Inheritance, Polymorphism etc
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.
Actually java is not purely object oriented.because we can use the primitive data types in Java.In java all those things or considered as classes and objects .So we are called java is an object oriented programming language...
Abstraction in Java or Object oriented programming is a way to segregate implementation from interface and one of the five fundamentals along with Encapsulation, Inheritance, Polymorphism, Class and Object. Abstraction in Java is achieved by using interface and abstract class in Java.
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.
Polymorphism, is an object-oriented programming concept, which relates to the ability to create a variable, function or an object that has more than one form. This allows the object to invoke the correct instance of the variable, function or other object based upon the object type. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Here are some links to examples: C++: http://www.cplusplus.com/forum/beginner/10884/ c#: http://msdn.microsoft.com/en-us/library/ms173152.aspx Python: http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Java: http://www.tutorialspoint.com/java/java_polymorphism.htm
features are: 1. Inheritance 2. Polymorphism 3. Data Encapsulation 4. Data Abstraction etc..
These are follows: 1-It is a platform independency. 2-Multithreding. 3-polymorphism. 4-encapsulation. 5-inheritence. 6-multitasking.
The important features of Java are the ones that relate to the object oriented concepts like: a. Inheritance b. Polymorphism c. Encapsulation d. Data Hiding e. Data Abstraction etc