group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
253 Members
Join this group to post and comment.
Gaurav Prasad
Gaurav Prasad
Computer Science
15 Jan 2019

How is Polymorphism used in java language?

details also please?

AdhikariHarika Reshma
AdhikariHarika Reshma
Computer Science
2mos ago

Polymorphism is one of the object oriented concept.It is the ability to take on many forms.When one task is done in different ways then it is referred to as polymorphism.

Poly-many, morph-forms.When a parent class reference is used to refer to a child class object then the polymorphism occurs.In java we use method overloading and method overriding to achieve polymorphism.

Static/Method overloading:

It is also called as static method.It means when we have two methods in our program then the two methods should have the same name but their parameters or the number of variables or the data type of the variables should differ.For example:

Public class Poly

{

Public void show(int a)

{

     System.out.println(a);

}

public void show(int a,int b)

{

     int c= a+b;

     System.out.println(c);

}

public  static void main(String[] s)

{

   Poly y=new Poly();

  y.show(5);

 y.show(2,4);

}

}

Run Time/Dynamic/Method overriding:

It is run time method and dynamic.In this when we have two methods then the name of the method should be same,their parameters,data types,number of parameters should also be same but the method functionality should be different.For example:

 public class Test

public void add()

{

    System.out.println("hello");

}

public void add()

{

 int a,b;

 c=a+b;

 System.out.println(c);

}

public static void main(String[] s)

{

   Test x= new Test();

  x.add();

x.add(5,6);

}

}

David Kanthi
David Kanthi
Computer Science
2mos ago

The same prototype or the definition of the method add() in the same class will not give you an error or exception ???


DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
2mos ago

Polymorphism is nothing but used to conduct many behaviour. There are two types of polymorphism techniques such as

1) Method overloading(compile time)

2) Method overriding(runtime)

Polymorphism can be demonstrated with a minor modification to the Bicycle class. For example, a printDescription  method could be added to the class that displays all the data currently stored in an instance.

public void printDescription(){
    System.out.println("\nBike is " + "in gear " + this.gear
        + " with a cadence of " + this.cadence +
        " and travelling at a speed of " + this.speed + ". ");
}

To demonstrate polymorphic features in the Java language, extend the Bicycle class with a MountainBike and a RoadBike class. For MountainBike, add a field for suspension, which is a string  value that indicates if the bike has a front shock absorber, front. Or, the bike has a front and back shock absorber, dual.

Here is the updated class:

public class MountainBike extends Bicycle {
    private String suspension;

    public MountainBike(
               int startCadence,
               int startSpeed,
               int startGear,
               String suspensionType){
        super(startCadence,
              startSpeed,
              startGear);
        this.setSuspension(suspensionType);
    }

    public String getSuspension(){
      return this.suspension;
    }

    public void setSuspension(String suspensionType) {
        this.suspension = suspensionType;
    }

    public void printDescription() {
        super.printDescription();
        System.out.println("The " + "MountainBike has a" +
            getSuspension() + " suspension.");
    }
}