Example of Constructor Overloading?
The given class contains overloaded versions of the method getVolume(): the first overloaded version calculates the volume of a cube, the second one calculates the volume of a cylinder and the third calculates the volume of a cuboid.
package com.mindclues; class ConstructorOverload { public void getVolume(int s) { System.out.println("Volume of cube is " + (s s s)); } public void getVolume(int r, int h) { System.out.println("Volume of cylinder is " + (3.14 r r * h)); } //And here is a test class package com.mindclues; class Test { public static void main(String[] args) { ConstructorOverload v = new ConstructorOverload(); v.getVolume(3); v.getVolume(3, 4); v.getVolume(3, 4, 7); } }
Volume of cube is 27 Volume of cylinder is 113.03999999999999 Volume of cuboid is 84
post a comment