Blog

Filter posts by Category Or Tag of the Blog section!

Polymorphic Methods in java

Tuesday, 06 May 2014

Polymorphism is an object-oriented programming concept that literally is being in many shapes. polymorphism allows you to define one interface and have multiple implementations. Polymorphism in Java has two types: Compile time and Runtime. When you are overloading the methods it's static polymorphism and when you are overriding methods it's dynamic polymorphism.

 

1- static polymorphism


public class Calculator
{
    public int DoIt(int a, int b )
    {
        return a+b;
    }

    public int DoIt(int a, int b, int c)
    {
        return a+b+c;
    }

    public int DoIt(int a, double b)
    {
        return a+(int)b;
    }
}

 

 

2 - dynamic polymorphism



public class Machine{

    public void Action(){
        System.out.println("it moves!");
    }
}

public class Car extends Machine{

    @Override
    public void Action(){
        System.out.println("it moves and can carry 4 person");
    }
}

public class Bus extends Machine{

    @Override
    public void Action(){
        System.out.println("it moves and can carry 40 person");
    }
}

Category: Software

Tags: Java

comments powered by Disqus