OOPS in JAVA 16
Polymorphism
Polymorphism is the ability of an object to take many forms.It is a concept by which we can perform a single action by different ways.
There are two types of polymorphism in java:
1. Compile time polymorphism
2. Runtime polymorphism
We can perform polymorphism in java by method overloading and method overriding.
Upcasting
When reference variable of Parent class refers to the object of Child class, it is known as upcasting. For example.
class A{}
class B extends A{}
A a=new B(); //upcasting
Runtime Polymorphism in Java
Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time.
In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.
Example.
class Bike{
void run(){System.out.println("running");}
}
class Splender extends Bike{
void run(){System.out.println("running safely with 60km");}
public static void main(String args[]){
Bike b = new Splender(); //upcasting
b.run();
}
}
Output: running safely with 60km.
NOTE: Runtime polymorphism can't be achieved by data members.
Q. Difference between Static binding and Dynamic binding in java ?
Ans)
1. Static binding in Java occurs during compile time while dynamic binding occurs during runtime.
2. Static binding uses type(Class) information for binding while dynamic binding uses instance of class(Object) to resolve calling of method at run-time.
3. Overloaded methods are bonded using static binding while overridden methods are bonded using dynamic binding at runtime.
No comments:
Post a Comment
Hai , Post your comment . (required, Bugs, Errors )