1. Constructor
- Constructor 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/provides data for the object, which is why it is known as constructor.
Rules & Purpose
- It is a special kind of method.
- It has the same name as the class name.
- It does not have any return type, even
void. - It can be used for initialization purpose.
- Constructor can be overloaded.
- Constructor can't be overridden.
Types of Constructors
Default Constructor
No-argument constructor.
Parameterized Constructor
Constructor that accepts parameters.
Main Purpose
- To initialize variables.
- To create an object.
2. Java Default Constructor
- A constructor that has no parameter is known as a default constructor.
- Default constructor is automatically generated by the compiler if the class does not have one.
- Default constructor provides default values to the object like
0,null, etc., depending on the type.
Syntax
<class_name>() {}
Constructor Provided by the JVM/Compiler
public class ConstructorDemoExample extends java.lang.Object {
/**
* Default Constructor
*/
public ConstructorDemoExample() {
super();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
3. Constructor Examples
Example 3, Example 4 and Example 5 are present in the source PDF. The PDF text extraction does not expose the complete code/content for Examples 3 and 4, so no additional code has been invented here.
4. this Keyword
The this keyword holds the current class reference variable and is used to represent:
- Current class variables.
- Current class methods.
- Current class constructors.
Current Class Method Calling
To call the current class methods, the this keyword is optional. Both forms are equivalent.
class Test {
void m1() {
m2();
System.out.println("m1 method");
m2();
}
void m2() {
System.out.println("m2 method");
}
public static void main(String[] args) {
Test t = new Test();
t.m1();
}
}
class Test {
void m1() {
this.m2();
System.out.println("m1 method");
this.m2();
}
void m2() {
System.out.println("m2 method");
}
}
5. super Keyword
The super keyword refers to superclass (parent) objects. It is used to call superclass methods and to access the superclass constructor.
The most common use of the super keyword is to eliminate confusion between superclasses and subclasses that have methods with the same name.
Uses of super
supervariable refers to the immediate parent class instance.supervariable can invoke the immediate parent class method.super()acts as the immediate parent class constructor and should be the first line in the child class constructor.
Example
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
super.move(); // invokes the super class method
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String args[]) {
Animal b = new Dog(); // Animal reference but Dog object
b.move(); // runs the method in Dog class
}
}
6. super() vs this()
| Point | super() | this() |
|---|---|---|
| Definition | Refers to the immediate parent class instance. | Refers to the current class instance. |
| Invoke | Can be used to invoke the immediate parent class method. | Can be used to invoke the current class method. |
| Constructor | Acts as the immediate parent class constructor and should be the first line in the child class constructor. | Acts as the current class constructor and can be used in parameterized constructors. |
| Override | When invoking a superclass version of an overridden method, the super keyword is used. | When invoking a current version of an overridden method, the this keyword is used. |
Quick Revision
| Topic | Key Point |
|---|---|
| Constructor | Special type of method used to initialize an object; same name as class and has no return type. |
| Default Constructor | No-argument constructor; automatically generated when a class does not define one. |
| Parameterized Constructor | Constructor that accepts parameters. |
| this | Represents the current class reference and can represent current variables, methods and constructors. |
| super | Refers to the immediate parent class instance and can invoke parent methods/constructors. |
| super() vs this() | super() refers to the parent constructor; this() refers to the current class constructor. |