📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Overloading Constructors Example
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
Box ob = new Box();
package com.javaguides.corejava.basics.polymorphism;
public class OverloadingConstructors {
public static void main(String[] args) {
// create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
// get volume of cube
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
}
}
/*
* Here, Box defines three constructors to initialize the dimensions of a box
* various ways.
*/
class Box {
double width;
double height;
double depth;
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
Volume of mybox1 is 3000.0
Volume of mybox2 is -1.0
Volume of mycube is 343.0
Using this keyword - invoke one constructor from another constructor
Use the explicit this qualifier when accessing fields inside instance methods or constructors to avoid ambiguity in referring to variable names.
package com.javaguides.corejava.basics.polymorphism;
public class OverloadingConstructors {
public static void main(String[] s) {
System.out.println(new Circle());
System.out.println(new Circle(50, 100));
System.out.println(new Circle(25, 50, 5));
}
}
class Circle {
private int xPos;
private int yPos;
private int radius;
// three overloaded constructors for Circle
public Circle(int x, int y, int r) {
xPos = x;
yPos = y;
radius = r;
}
public Circle(int x, int y) {
xPos = x;
yPos = y;
radius = 10; // default radius
}
public Circle() {
xPos = 20; // assume some default values for xPos and yPos
yPos = 20;
radius = 10; // default radius
}
public String toString() {
return "center = (" + xPos + "," + yPos + ") and radius = " + radius;
}
}
center = (20,20) and radius = 10
center = (50,100) and radius = 10
center = (25,50) and radius = 5
package com.javaguides.corejava.basics.polymorphism;
public class OverloadingConstructors {
public static void main(String[] s) {
System.out.println(new Circle());
System.out.println(new Circle(50, 100));
System.out.println(new Circle(25, 50, 5));
}
}
class Circle {
private int xPos;
private int yPos;
private int radius;
public Circle(int x, int y, int r) {
xPos = x;
yPos = y;
radius = r;
}
public Circle(int x, int y) {
this(x, y, 10); // passing default radius 10
}
public Circle() {
this(20, 20, 10);
// assume some default values for xPos, yPos and radius
}
public String toString() {
return "center = (" + xPos + "," + yPos + ") and radius = " + radius;
}
}
center = (20,20) and radius = 10
center = (50,100) and radius = 10
center = (25,50) and radius = 5
Comments
Post a Comment
Leave Comment