Java Polymorphism

  1. Same Length Attributes Method Overloading
  2. Different Length Attributes Method Overloading
  3. Same Length Attributes Constructor Overloading
  4. Different Length Attributes Constructor Overloading

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Same Length Attributes Method Overloading


Poly_Ex1.java

public class Poly_Ex1 {
	
	int a;
	float b;
	String str;
	
	Poly_Ex1() {
		
		a = 11;
		b = (float)11.11;
		str = "Method ";
	}
	
	void display(int x) {
		
		a = a+x;
		System.out.println("The Integer Value is : "+ a);
	}
	
	void display(float y) {
		
		b = b+y;
		System.out.println("The Float Value is : "+ b);
	}
	
	void display(String z) {
		
		str = str.concat(z);
		System.out.println("The String Value is : "+ str);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Poly_Ex1 obj = new Poly_Ex1();
		
		obj.display(100);
		obj.display((float)23.56);
		obj.display("Overloading !!!");
	}
}

Sample Output

The Integer Value is : 111
The Float Value is : 34.67
The String Value is : Method Overloading !!!




B. Different Length Attributes Method Overloading


Poly_Ex2.java

public class Poly_Ex2 {
	
	void display() {
		
		System.out.println("Welcome");
	}
	
	void display(int x, int y) {
		
		int a = x + y;
		System.out.println("int, int Combination : "+a);
	}
	
	void display(int x, float y) {
		
		float a = x + y;
		System.out.println("int, float Combination : "+a);
	}
	
	void display(String x, String y) {
		
		String a = x.concat(y);
		System.out.println("string, string Combination : "+a);
	}
}

class MainClass {
	
	public static void main(String args[]) {
		
		Poly_Ex2 obj = new Poly_Ex2();
		
		obj.display();
		obj.display(10, 20);
		obj.display(20, (float)50.45);
		obj.display("Method ", "Overloading !!!");
	}
}

Sample Output

Welcome
int, int Combination : 30
int, float Combination : 70.45
string, string Combination : Method Overloading !!!




C. Same Length Attributes Constructor Overloading


Poly_Ex3.java

public class Poly_Ex3 {
 
	Poly_Ex3(String x) {
		
		System.out.println(x);
	}
	
	Poly_Ex3(int x) {
		
		System.out.println("Int Value : "+x);
	}
	
	Poly_Ex3(float x) {
		
		System.out.println("Float Value : "+x);
	}
}

class MainClass {
	
	public static void main(String args[]) {
		
		Poly_Ex3 obj1 = new Poly_Ex3("Constructor Overloading !!!");
		Poly_Ex3 obj2 = new Poly_Ex3(11);
		Poly_Ex3 obj3 = new Poly_Ex3((float)32.65);
	}
}

Sample Output

Constructor Overloading !!!
Int Value : 11
Float Value : 32.65




D. Different Length Attributes Constructor Overloading


Poly_Ex4.java

public class Poly_Ex4 {
 
	Poly_Ex4() {
		
		System.out.println("Welcome !!!");
	}
	
	Poly_Ex4(String x, String y) {
		
		x = x.concat(y);
		System.out.println(x);
	}
	
	Poly_Ex4(int x, int y) {
		
		x = x+y;
		System.out.println("int, int Combination : "+x);
	}
	
	Poly_Ex4(int x, float y) {
		
		y = x+y;
		System.out.println("int, float Combination : "+y);
	}
}

class MainClass {
	
	public static void main(String args[]) {
		
		Poly_Ex4 obj1 = new Poly_Ex4();
		Poly_Ex4 obj2 = new Poly_Ex4("Constructor ", "Overloading");
		Poly_Ex4 obj3 = new Poly_Ex4(5,10);
		Poly_Ex4 obj4 = new Poly_Ex4(23,(float) 56.23);
	}
}

Sample Output

Welcome !!!
Constructor Overloading
int, int Combination : 15
int, float Combination : 79.229996







SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com