7.6 Constructor Example in Java Constructor Overloading

preview_player
Показать описание
Constructor in java is a special type of method that is used to initialize the object.

Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.

name of constructor in Java must be exactly same with the class on which you declare constructor,

A class in Java can have as many constructor as it and that is called constructor overloading in Java but signature of two constructor must not be same.

Constructor overloading is similar to method overloading in Java.

You can call overloaded constructor by using this() keyword in Java.

overloaded constructor must be called from another constructor only.

its necessary to add no argument default constructor

advantage of Constructor overloading is flexibility which allows you to create object in different way

Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

For More Queries about Course, WhatsApp or Call: +919008963671

Subscribe to the channel and learn Programming in easy way.

Рекомендации по теме
Комментарии
Автор

In the constructor overloading example with 2 parameters (int, float)..while passing the values ..we used Abc(5, 5.5)..shouldn't it be Abc(5, 5.5f)..is that not required ?

ciruslivingstone
Автор

sir what if: "A class declares a constructor that takes two parameters. How would you create an instance of the class with no parameters?"

lilrana
Автор

Good morning, Sir
What is the difference between method overloading and constructor overloading?

lalatendumohapatra
Автор

How can i call all the constructor with or without parameter as here i can call only one at a time.

nishthaagarwal
Автор

I am writing code same as you on netbeans but my code is not running, please tell me briefly which type of mistake i am doing

neehakumari
Автор

Sir u said constructer name is class name but u taken abc as constructer

santhoshchinna
Автор

package javaapplication129;
public class STATIC_PRACTICE {
public static void main(String[] args) {
exp var= new exp(); //default constructor for class Abc
exp var1= new exp(9, 5.6); //parameterized constructor using two predefines data type
exp var3 = new exp(4); // parameterized constructor using single predefine data type
}
}
class exp
{
// constructor overloading def:-constructor with same name but different paarmenters.
public exp()
{
System.out.println("Default Contructor");
}
public exp(int c)
{
constructure "+c);
}
public exp(int v, double d)
{
constructure "+v+d);
}
}

shuvshaw
welcome to shbcf.ru