You are on page 1of 1

Polymorphism: In Core Java Polymorphism is one of easy concept to understand.

Polymorphism def inition is that Poly means Many morphos means froms . Its refer to the objects ability to active Polymorphism depends on its type. There are two types of Polymorphism available in Java. 1)Static Polymorphism 2) Dynamic Polymorphism Let s we discuss about Static Polymorphism, Its Compile time Polymorphism. Method overloading is the concept of two or more methods in a Java Class can have same name and it their arguments lists are different. We also have another two import ant concepts in Polymorphism, Method Overloading and Method Overridding. The fol lowing example program will make you understand the Method Overloading. Example for Method overloading class Subjects { void add(int tamil, int english){ System.out.println( The total of tamil and english is +(tamil+english)); } void add(int tamil,int english,int maths){ System.out.println( The total of tamil english and maths is +(tamil+english+maths)) ; } } public class MethodOverloadingDemo{ public static void main(String arg[]){ //create Subjects class object Subjects sb=new Subjects(); // we have to call add() method by passing 2 values sb.add(90, 80); //here also we are calling add() method by passing 3 values, So the 3 arguments (parameters) method will get execute. sb.add(95,85,100); } } Output for the above program is : The total of tamil and english is 170 The total of tamil english and maths is 280 Now we will discuss about what is dynamic polymorphism, Its run time polymorphis m. We can also call it as Method overridding. Method overridding is the concept of two or more method, constructor have a same name in super and sub class with same signature. This feature is called method overridding. Method Overloading Example Program: class MathsSquareDemo1 { void calculate(double price){ System.out.println( Sqare value +(price*price)); } } class MathsSquareDemo2 extends MathsSquareDemo1 { void calculate(double price){ System.out.println( Sqare value +(Math.sqrt(price))); } } public class MethodOverriddingDemo{ public static void main(String arg[]){ MathsSquareDemo2 msd=new MathsSquareDemo2(); msd.calculate(25); } }

You might also like