Ehsan Ghanbari

Experience, DotNet, Solutions

Polymorphic Methods in java

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");
    }
}

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns. I spend a lot of time on software architecture. Since 2008, I've been as a developer for different companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the past years, Read More

Post Tags
Pending Blog Posts
using Elmah in asp.net MVC4
Using FluentSecurity in MVC
Strategic design
Factory Pattern
time out pattern in ajax
Redis as a cache server
How to use PagedList In asp.net MVC
Multiple submit buttons in asp.net MVC
Domain driven design VS model driven architecture
What's the DDD-lite?
comments powered by Disqus