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 and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

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