11.1 Constructor Overloading
Friday, August 21, 2009
In addition to overloading normal methods, we can also overload constructors. In fact, for most real-world classes that you create, overloaded constructors will be the norm.
Have a look at the fallowing example
Example I
When we compile this program we can see the fallowing output
Values of I, J and K :10 20 30
Values of I, J :10 20
Value of K :10
THIS Constructor
Have a look at the fallowing example to use THIS CONSTRUCTOR
If we compile the above program we can see the fallowing output
Employee Name :Hrithik
Department : Direction
Employee Salary : 20000.0
Employee Name :Hrithik
Department : null
Employee Salary : 2000.0
Here you can observe that department name becomes null in the second constructor call.
Have a look at the fallowing example to know more about constructor overloading
If we compile this program we can see the below output
Volume of the Cuboid is: 10.0
Volume of the Cube is :27.0
Have a look at the fallowing example
Example I
// Demonstartion of Constructor Overloading /** * * @author Sudarsan */ class Sample { int i,j,k; //Constructor with three Parameters public Sample(int i,int j,int k) { this.i=i; this.j=j; this.k=k; System.out.println("Values of I, J and K :"+i+" "+j+" "+k); } //Constructor with two parameters public Sample(int i,int j) { this.i=i; this.j=j; System.out.println("Values of I, J :"+i+" "+j); } //Constructor with only one Parameter public Sample(int k) { this.k=k; System.out.println("Value of K :"+k); } }
// Constructing three different Object for the Class Sample /** * * @author Sudarsan */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Sample s1=new Sample(10,20,30); Sample s2=new Sample(10,20); Sample s3=new Sample(10); } }
When we compile this program we can see the fallowing output
Values of I, J and K :10 20 30
Values of I, J :10 20
Value of K :10
THIS Constructor
Have a look at the fallowing example to use THIS CONSTRUCTOR
// Using THIS Constructor /** * * @author Sudarsan */ class Employee { private String dept; private String name; private double salary=20000.00; public Employee(String name,double salary,String dept ) { this.name=name; this.salary=salary; this.dept=dept; } public Employee(String name,double salary) { // Passing values to the above constructor by using "this" this(name,salary,null); } public void display() { System.out.println("Employee Name :"+name); System.out.println("Department : "+dept); System.out.println("Employee Salary : "+salary); } }
// Creating Objects for Class Employee /** * * @author Sudarsan */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here //Passing parameters to the Employee Constructor Employee e=new Employee("Hrithik",20000.00,"Direction"); e.display(); Employee e1=new Employee("Hrithik",2000.00); e1.display(); } }
If we compile the above program we can see the fallowing output
Employee Name :Hrithik
Department : Direction
Employee Salary : 20000.0
Employee Name :Hrithik
Department : null
Employee Salary : 2000.0
Here you can observe that department name becomes null in the second constructor call.
Have a look at the fallowing example to know more about constructor overloading
// To fine the Volume of the Cuboid and Cube /** * * @author Sudarsan */ public class Cuboid { double length; double width; double height; // Constructor declared which accepts three arguments Cuboid(double l, double w, double h) { length = l; width = w; height = h; } // Overloaded constructor declared which accepts one argument Cuboid(double side) { length = width = height = side; } double volume() { return length*width*height; } }
// Constructing Cuboid Objects and passing Parameters /** * * @author Sudarsan */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Cuboid cub1 = new Cuboid(1, 2, 5); Cuboid cub2 = new Cuboid(3); double vol; vol = cub1.volume(); System.out.println("Volume of the Cuboid is: "+ vol); vol = cub2.volume(); System.out.println("Volume of the Cube is :" + vol); } }
If we compile this program we can see the below output
Volume of the Cuboid is: 10.0
Volume of the Cube is :27.0
Labels:
Polymorphism