Overloading Concepts in java-polymorphism and Overloading in java programming
Overloading
what is method overloading in java?
The process of defining methods with the same name but different functionalities is called method overloading. In other words, with the same name and different parameters.This is not supported in c language.For example,an overloaded draw() method can be used to draw anything,such as a circle,square,triangle and so on.
how many types of overloading in java?
Types of overloading
In java there are three types of overloading
- constructor overloading ---> Default constructor(without any arguments)Parameterized constructor(with some arguments)
- method overloading ---> Method Without any argument and method with arguments
- operator overloading ---> The + symbol can be implemented in two ways one is addition operator and another one is concatenation operator,same symbol used to perform two purposes.It is called operator overloading
Polymorphism
Implimenting same thing in diffrent way,this is called polymorphism.All these three types of overloading term is called as polymorphism.Polymorphism in java,is achieved by implementing the overloading concepts.Since these three overloading occurs in the compile time, so it is called compile time polymorphism.
Types of polymorphism
In java there are two types of polymorphism
- Compile time polymorphism ---> compile time polymorphism is achieved by implementing method overloading conceptThis is called static binding
- Runtime polymorphism ---> Runtime polymorphism is achieved by implementing method overriding concepts.This is called dynamic binding
Q:Which java class programming technique shows the use of polymorphism?
A: polymorphism in java, is achieved by implementing the overloading concepts
sample program constructor overloading
class Student { Student() { System.out.println(); System.out.println("This is a default consructor"); System.out.println("____________"); } Student(String str,int i) { System.out.println("This is a parametrized constructor"); System.out.println("Name :"+str+" Age :"+i); System.out.println("____________"); } } class Constructor { public static void main(String args[]) { Student std=new Student(); Student std1=new Student("Bilal",10); } }
sample program method overloading
class Adding
{
void add()
{
System.out.println();
System.out.println("Method without arguments");
System.out.println(10+20);
System.out.println("____________");
}
void add(int i,int j)
{
System.out.println("Method with arguments");
System.out.println(i+j);
System.out.println("____________");
}
}
class Method
{
public static void main(String args[])
{
Adding std=new Adding();
std.add();//calling without argument method
std.add(50,50);// calling argument method
}
}
Sample program operator overloading
class Operator
{
public static void main(String arg[])
{
String str ="Welcome";
System.out.println(200+300);//adding twonumber with +
System.out.println(str+" to Java progrmming");//added two string with +
}
}
Observe the programs and its output carefully
Comments
Post a Comment