Selenium automation testing course with Java- constructor chaining in java- class 12

preview_player
Показать описание
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 Chaining:-
======================
What is constructor chaining ?
Constructor Chaining in Java refers to the practice of calling one constructor from another constructor within the same class or from the superclass. This helps in avoiding code duplication and ensures a consistent object initialization process.
Types of Constructor Chaining:-
==========================
1. Within the Same Class – using this()
You can call another constructor from the same class using this().
Example:-
=========
public class Student {
String name;
int age;
Student() {
this("Unknown", 18); }

Student(String name, int age) {
}
}
2.From Subclass to Superclass – using super()
You can call a constructor from the immediate parent class using super().
Example:-
=========
class Person {
Person() {
}
}

class Employee extends Person {
Employee() {
super(); // calls the constructor of Person class
}
}
Rules for Constructor Chaining
==============================
1. this() or super() must be the first statement in the constructor.
2. You cannot call both this() and super() in the same constructor.
3. Constructor chaining helps reduce redundancy and improves maintainability.
instance variables vs static variables
========================================
Instance Variables
====================
-Belong to individual objects of a class.
-Every object gets its own copy of the variable.
-Created when an object is instantiated using new.
-Accessed using object reference.
- Memory is allocated in the heap for each object.

public class Car {
String color; // instance variable

public Car(String color) {
}
}
Car c1 = new Car("Red");
Car c2 = new Car("Blue");

Static Variables (Class Variables)
===================================
1. Belong to the class, not to any one object.
2. Shared by all instances of the class.
3. Created once when the class is loaded into memory.
5. Memory is allocated in the method area (or meta-space in JVM).

public class Car {
static int wheels = 4; // static variable
}
Car c1 = new Car();
Car c2 = new Car();
Use static variables when the data is common to all instances, like a counter or constant

this keyword in java
=======================
In Java, the this keyword is a special reference variable that refers to the current object — the object on which the method or constructor is being invoked.

Use Cases of this Keyword
=========================
1. Refer to current class instance variables
Helps distinguish between instance variables and parameters with the same name.

public class Student {
String name;

public Student(String name) {
}
}
2. Call another constructor in the same class
Used for constructor chaining.

public class Student {
String name;
int age;

Student() {
this("Unknown", 0); // calls parameterized constructor
}

Student(String name, int age) {
}
}
Рекомендации по теме
visit shbcf.ru