Polymorphism
Polymorphism is the capacity of a protest go up against numerous structures. The most widely recognized utilization of polymorphism in OOP happens when a parent class reference is utilized to allude to a youngster class protest.Any Java protest that can pass more than one IS-A test is thought to be polymorphic. In Java, all Java objects are polymorphic since any question will finish the IS-A test for their own sort and for the class Object.
Know that the main conceivable approach to get to a protest is through a reference variable. A reference variable can be of just a single kind. Once pronounced, the sort of a reference variable can't be changed.
The reference variable can be reassigned to different items gave that it isn't pronounced last. The sort of the reference variable would decide the techniques that it can summon on the protest.
A reference variable can allude to any question of its announced kind or any subtype of its proclaimed sort. A reference variable can be pronounced as a class or interface write.
Types
- Ad hoc polymorphism
- Parametric polymorphism
- Subtyping
- Row polymorphism
- Polytypism
Ad hoc polymorphism
Christopher Strachey picked the term specially appointed polymorphism to allude to polymorphic capacities that can be connected to contentions of various sorts, yet that act diversely relying upon the kind of the contention to which they are connected (otherwise called work over-burdening or administrator over-burdening).
Parametric polymorphism permits a capacity or an information compose to be composed non exclusively, so it can deal with values consistently without relying upon their type.[6] Parametric polymorphism is an approach to make a dialect more expressive while as yet keeping up full static write security.
Subtyping
A few dialects utilize the possibility of subtyping (additionally called subtype polymorphism or consideration polymorphism) to confine the scope of sorts that can be utilized as a part of a specific instance of polymorphism. In these dialects, subtyping enables a capacity to be composed to take a protest of a specific sort T, yet additionally work effectively, if passed a question that has a place with a sort S that is a subtype of T (as indicated by the Liskov substitution rule). This compose connection is now and again composed S <: T. On the other hand, T is said to be a supertype of S—composed T :> S. Subtype polymorphism is generally settled powerfully (see underneath).
Row polymorphism
Column polymorphism is a comparative, yet unmistakable idea from subtyping. It manages auxiliary composes. It permits the utilization of all qualities whose composes have certain properties, without losing the rest of the sort data.
polytypism
A related idea is polytypism (or information compose genericity). A polytypic work is more broad than polymorphic, and in such a capacity, "however one can give settled impromptu cases to particular information composes, a specially appointed combinator is missing".
Example
_______________________________________________________________________________
public class Point
{
public int id;
public Point() {
this.id = 0;
}
public Point(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void display()
{
System.out.println("I am Point class");
}
}
public class Circle extends Point
{
public String name;
public Circle() {
super();
this.name = "";
}
public Circle(String name, int id) {
super(id);
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void display()
{
System.out.println("I am Circle class");
}
public static void main(String args[])
{
Point obj=new Circle();
obj.display();
}
}
_________________________________________________________________________________
0 Comments
Your Comment is Submitted