Logo video2dn
  • Сохранить видео с ютуба
  • Категории
    • Музыка
    • Кино и Анимация
    • Автомобили
    • Животные
    • Спорт
    • Путешествия
    • Игры
    • Люди и Блоги
    • Юмор
    • Развлечения
    • Новости и Политика
    • Howto и Стиль
    • Diy своими руками
    • Образование
    • Наука и Технологии
    • Некоммерческие Организации
  • О сайте

Скачать или смотреть Constructor Overloading and Constructor Chaining in Java complete explanation.

  • Learn Java
  • 2024-04-01
  • 97
Constructor Overloading and  Constructor Chaining in Java complete explanation.
#shorts#javaShorts#balaji m#java#computers
  • ok logo

Скачать Constructor Overloading and Constructor Chaining in Java complete explanation. бесплатно в качестве 4к (2к / 1080p)

У нас вы можете скачать бесплатно Constructor Overloading and Constructor Chaining in Java complete explanation. или посмотреть видео с ютуба в максимальном доступном качестве.

Для скачивания выберите вариант из формы ниже:

  • Информация по загрузке:

Cкачать музыку Constructor Overloading and Constructor Chaining in Java complete explanation. бесплатно в формате MP3:

Если иконки загрузки не отобразились, ПОЖАЛУЙСТА, НАЖМИТЕ ЗДЕСЬ или обновите страницу
Если у вас возникли трудности с загрузкой, пожалуйста, свяжитесь с нами по контактам, указанным в нижней части страницы.
Спасибо за использование сервиса video2dn.com

Описание к видео Constructor Overloading and Constructor Chaining in Java complete explanation.

You can learn Java with me as the Java Programming language is being made easily. It would take a period of minimum three months and maximum 6 months to master Java. I would recommend you to watch all my videos to crack any software interviews that would require Java Programming language as a primary skill.

Constructor Overloading:-
=========================
Whenever you keep constructors with the same name but with different parameters ,we call this process as constructor overloading. And, the process of constructor overloading is basically achieved to give a specific task to the constructors.
Example:-

class A
{
A() // no arg Constructor
{
System.out.println("No arg");
}
A(int a)// int arg Constructor
{
System.out.println("int arg");
}
A(double b)//double arg Constructor
{
System.out.println("double arg");
}
public static void main(String[] args)
{
A obj1=new A();
A obj2=new A(10);
A obj3=new A(10.20);
}
Output:-
=========
No arg
int arg
double arg

Constructor chaining:-
=====================
A constructor chaining in Java is a sequence of invocations or constructor calls by just creating one Object. And, all the constructors will get executed upon this object.

Example:-
class A
{
A()
{
this(10);
System.out.println("No arg");
}
A(int a)
{
this(10.34);
System.out.println("int arg");
}
A(double b)
{
System.out.println("double arg");
}
public static void main(String[] args)
{
A obj1=new A();// All the constructors of the same class gets executed using this //constructor chaining Mechanism
}
}

output:-
=========
double arg
int arg
No arg

We can achieve constructor chaining with the following mechanisms;-
=====================================================================

When a constructor is called from another constructor with in the same class using the this() constructor call is called as constructor chaining.

When a constructor is called from another constructor in a different class using the super() constructor call is called as constructor chaining.

Example:-

class B{
B()
{
System.out.println("B class");
}
}
class A extends B
{
A()
{
this(10);
System.out.println("No arg");
}
A(int a)
{
this(10.34);
System.out.println("int arg");
}
A(double b)
{
super();
System.out.println("double arg");
}
public static void main(String[] args)
{
A obj1=new A();
}
}

Output:-
========
B class
double arg
int arg
No arg

Recursive invocation call error:-
==================================
Please be informed that, If a constructor calls itself, then the error message “recursive constructor invocation” occurs. The following program is not allowed by the compiler because inside the constructor there is a call to same constructor. The compiler finds it instantly and throws an error.

Example 1:-

class A
{
A()
{
this(10);
System.out.println("No arg");
}
A(int a)
{
this(10.34);
System.out.println("int arg");
}
A(double b)
{
this();
System.out.println("double arg");
}
public static void main(String[] args)
{
A obj1=new A();
}
}

Error:-
A.java:3: error: recursive constructor invocation
A()

Example 2:-
class A
{
A()
{
this();
System.out.println("No arg");
}
A(int i)
{
this(10);
System.out.println("No arg");
}

public static void main(String[] args)
{
A obj1=new A();
}
}

Error:-
A.java:8: error: recursive constructor invocation
A(int i)
^
A.java:3: error: recursive constructor invocation
A()
^
2 errors

Rules for Constructor chaining :-
==================================
An expression that uses this() constructor call must be first line of the constructor.

An expression that uses super() constructor call must be first line of the constructor.

The order does not matter in constructor chaining.

Please ensure that there must be at least one constructor that does not use this() constructor call and when this rule is violated, you get recursive constructor invocation. But the same case is not with super() constructor call.

Please ensure that there must not be a this() constructor call which is calling the same constructor as follows.

1. A(){
this() } INVALID recursive invocation
2. A(int i){
this(10)} INVALID recursive invocation

3. A(doube i){
this(10.234)} INVALID recursive invocation

Problem statement:-
==================

class B{
int b;
int c;
B(int b)
{
this();
this.b=b+100;
}
B()
{
this.c=300;

}
public String toString()
{
return b+" "+c;
}
}

class A extends B
{

int a;
A(int a)
{
super(a);
this.a=a;

}
public String toString()
{
return a+" "+b+" "+c;

}

public static void main(String[] args)
{
A obj2=new A(100);

System.out.println(obj2);

}
}

Find the output?

Комментарии

Информация по комментариям в разработке

Похожие видео

  • О нас
  • Контакты
  • Отказ от ответственности - Disclaimer
  • Условия использования сайта - TOS
  • Политика конфиденциальности

video2dn Copyright © 2023 - 2025

Контакты для правообладателей [email protected]