Polymorphism in Java
Polymorphism (from the Greek meaning "having multiple forms")
Polymorphism means one thing in many form. an object to have more than one form. Polymorphism is capability of one object to behave in multiple ways.
There are following types of polymorphism :
Static polymorphism (compile time) function overloading :-
Method Overloading is a feature that allows more than two methods having same name in a class with different parameters.
Example:
Dynamic polymorphism(runtime time) function overriding :-
Methods with same name and with same signature in super class and sub class.
Example:
Polymorphism means one thing in many form. an object to have more than one form. Polymorphism is capability of one object to behave in multiple ways.
There are following types of polymorphism :
Static polymorphism (compile time) function overloading :-
Method Overloading is a feature that allows more than two methods having same name in a class with different parameters.
Example:
- public class Calculator{
- void add(int a,int b)
- {
- System.out.println(a+b);
- }
- void add(int a,int b,int c)
- {
- System.out.println(a+b+c);
- }
- public static void main(String args[]){
- Calculator cal=new Calculator();
- cal.add(5,10,15);
- cal.add(5,10);
- }
- }
Dynamic polymorphism(runtime time) function overriding :-
Methods with same name and with same signature in super class and sub class.
Example:
- public class Vehicle{
- void Name(){
- System.out.println("Vehicle")
- }
- }
- public class Car extends Vehicle{
- void Name(){
- System.out.println("Car")
- }
- }
- public class Bus extends Vehicle{
- void Name(){
- System.out.println("Bus")
- }
- }
- public class Test{
- public static void main(String args[]){
- Bus b=new Bus();
- b.Name();
- Vehicle v= new Car();
- b.Name();
- }
- }
Comments
Post a Comment