|
|
Java Constructor Overloading Examples
Like method overloading we can overload constructors also. Along with default constructor, we can have constructors with
parameters. The no of parameters can be same, and it can have different datatypes. Below example gives sample code for constructors
overloading.
Java Constructor Overloading Sample Code
Code: |
package com.myjava.constructors;
public class MyOverloading {
public MyOverloading(){
System.out.println("Inside default constructor");
}
public MyOverloading(int i){
System.out.println("Inside single parameter constructor with int value");
}
public MyOverloading(String str){
System.out.println("Inside single parameter constructor with String object");
}
public MyOverloading(int i, int j){
System.out.println("Inside double parameter constructor");
}
public static void main(String a[]){
MyOverloading mco = new MyOverloading();
MyOverloading spmco = new MyOverloading(10);
MyOverloading dpmco = new MyOverloading(10,20);
MyOverloading dpmco = new MyOverloading("java2novice");
}
}
|
Example Output
Inside default constructor
Inside single parameter constructor with int value
Inside double parameter constructor
Inside single parameter constructor with String object
|
|
Other Constructor Examples
|
|
|
Where can we use serialization?
Whenever an object has to sent over the network, those objects should be serialized. Also if the state of an object is to be saved, objects need to be serilazed.
Education is what remains after one has forgotten what one has learned in school.
-- Albert Einstein
|