Constructor in java with some best example suitable to understand by the student. Constructors relation with inheritance also briefly discussed in this slide. How multiple constructor can be invoked are discussed very well in this slide. The use of this and super keyword in Java are discussed here. How can we invoke a parameter from derived class and pass parameters to the constructor are discussed here. Constructor chaining also discussed here. It is a great slide if you want to learn constructor and there chaining in Java. Best of luck to students.
2. What is constructor ?
Structure of constructor
Types of constructor
Java constructor and inheritance
Constructor chaining
This() keyword
Super() keyword
2/17/2024 2
naeem_mia
3. A constructor is just a Java method with the same name as the class name. The
constructor method does not have any return type to it.
It is mainly used to initialize the object. When creating an object of a class, the
constructor gets invoked at that time.
Compared to other programming languages, such as C++, Java doesn’t have explicit
destructors. The responsibility of managing memory in Java is assigned to the
garbage collector.
The garbage collector automatically identifies and deallocates objects that are no
longer referenced. Hence, there is no need for explicit destructor methods in Java
compared to other languages.
2/17/2024 3
naeem_mia
4. How to invoke this constructor?
2/17/2024 4
naeem_mia
11. In constructor chain, a constructor is called from another constructor in the same class
this process is known as constructor chaining. It occurs through inheritance. When
we create an instance of a derived class, all the constructors of the inherited class
(base class) are first invoked, after that the constructor of the calling class (derived
class) is invoked.
We can achieve constructor chaining in two ways:
By using this() keyword: It is used when we want to call the current class
constructor within the same class.
By using super() keyword: It is used when we want to call the superclass
constructor from the base class.
2/17/2024 11
naeem_mia