Last active
September 30, 2021 13:36
-
-
Save Arjun2002tiwari/fe835c40d535b15c46ebd32cb4886de1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//EXAMPLE OF OVERLOADING CONSTRUCTOR | |
class Overloading { | |
double width; | |
double height; | |
double length; | |
//FIRST CONSTRUCTOR IS CREATED | |
Overloading(double w,double h,double l) { | |
width = w; | |
height = h; | |
length = l; | |
} | |
Overloading(){ | |
width = 2; | |
height = 4; | |
length = 6; | |
} | |
//USED WHEN ALL DIMENSIONS ARE EQUAL | |
Overloading(double len){ | |
width = height = length = len; | |
} | |
double volume(){ | |
return width*height*length; | |
} | |
} | |
class OverloadingDemo{ | |
public static void main(String[] args) { | |
Overloading Overloading1 = new Overloading(10,12,14); | |
Overloading Overloading2 = new Overloading(); | |
Overloading Overloading3 = new Overloading(4); | |
double vol; | |
vol = Overloading1.volume(); | |
System.out.println("the volume from first constructor is: "+vol); | |
vol = Overloading2.volume(); | |
System.out.println("the volume from second constructor is: "+vol); | |
vol = Overloading3.volume(); | |
System.out.println("the volume from third constructor is: "+vol); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment