Advertisements
Home > Java > Java Program to show Overloading Constructors – Q21

Java Program to show Overloading Constructors – Q21


Q21: Java Program to show overloading constructors

Constructors are used to create instances of an object and can be overloaded by creating more than one Constructor in a class with same name but having different signatures.

class Emp_class{
	private String name;
	private int salary;
	
	public Emp_class(){
		name = "Manoj Pandey";
		salary = 10500;
	}
	
	public Emp_class(String n, int s){
		name = n;
		salary = s;
	}
	
	public Emp_class(Emp_class Oemp){
		name = Oemp.name;
		salary = Oemp.salary;
	}
	
	public void show(){
		System.out.println("\n Name: " + name);
		System.out.println(" Salary: " + salary);
	}
}

class ctor_ovld{
	public static void main(String args[]){
		Emp_class Oe1 = new Emp_class();
		Oe1.show();
		
		Emp_class Oe2 = new Emp_class("Nitin", 25050);
		Oe2.show();
		
		Emp_class Oe3 = new Emp_class("Bhuwanesh", 30050);
		Oe3.show();

		Emp_class Oe4 = Oe2;
		Oe4.show();
	}
}

… from College notes (BCA/MCA assignments):


Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: