Module 3 - Java Constructor and Inheritance
3.1. Rules in creating Java Constructor
3.2. Types of Java Constructor
3.3. Constructor Overloading
3.4. Difference between Constructor and Method in Java
3.5. Java Copy Constructor
Rules in creating Java Constructor
Following are some rules for writing constructors in Java:
• The name of the constructor must be the same as the name of its class.
• A constructor must have no return type. It can not have not even void as its return type.
• We can use the access modifiers with a constructor to control its access so that other classes
can call the constructor.
• We can not declare a constructor as final, abstract, abstract and synchronized.
Example
The following Java program demonstrates the creation of constructors.
Output
Name of the Student: Raju
Age of the Student: 20
Types of Java constructors
There are two types of constructors in Java:
• Default constructor (no-arg constructor)
• Parameterized constructor
Module 3 - Java Constructor and Inheritance
Java Default Constructor
A constructor is called "Default Constructor" when it doesn't have any parameter.
Syntax of default constructor:
Example of default constructor
In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time
of object creation.
Output:
Bike is created
Note:
• if there is no constructor in a class, compiler automatically creates a default constructor.
• The default constructor is used to provide the default value to the object like 0, null, etc…,
depending on the type.
Java Parameterized Constructor
A constructor which has a specific number of parameters is called a parameterized constructor.
Why use the parameterized constructor?
The parameterized constructor is used to provide different values to distinct objects. However, you can
provide the same values also.
Example of parameterized constructor
In this example, we have created the constructor of Student class that have two parameters. We can
have any number of parameters in the constructor.
Module 3 - Java Constructor and Inheritance
Output
111 Karan
222 Aryan
Constructor Overloading
In Java, a constructor is just like a method but without return type. It can also be overloaded like Java
methods.
Constructor overloading in Java is a technique of having more than one constructor with different
parameter lists. They are arranged in a way that each constructor performs a different task. They are
differentiated by the compiler by the number of parameters in the list and their types.
Example of Constructor Overloading
Module 3 - Java Constructor and Inheritance
Output
111 Karan 0
222 Aryan 25
Module 3 - Java Constructor and Inheritance
Difference between constructor and method in Java
There are many differences between constructors and methods. They are given below.
Java Constructor Java Method
A constructor is used to initialize the state of an object. A method is used to expose the
behavior of an object.
A constructor must not have a return type. A method must have a return type.
The constructor is invoked implicitly. The method is invoked explicitly.
The Java compiler provides a default constructor if you The method is not provided by the
don't have any constructor in a class. compiler in any case.
The constructor name must be same as the class name. The method name may or may not be
same as the class name.
Module 3 - Java Constructor and Inheritance
Java Copy Constructor
There is no copy constructor in Java. However, we can copy the values from one object to another like
copy constructor in C++.
There are many ways to copy the values of one object into another in Java. They are:
• By constructor
• By assigning the values of one object into another
• By clone() method of Object class
In this example, we are going to copy the values of one object into another using Java constructor.
Module 3 - Java Constructor and Inheritance
Output
111 Karan
111 Karan
Java Inheritance
In Java, it is possible to inherit attributes and methods from one class to another. We group the
"inheritance concept" into two categories:
• subclass (child) - the class that inherits from another class
• superclass (parent) - the class being inherited from
To inherit from a class, use the extends keyword.
In the example below, the Car class (subclass) inherits the attributes and methods from
the Vehicle class (superclass):
Resources
• https://techvidvan.com/tutorials/java-
constructor/#:~:text=Rules%20for%20Writing%20Constructors%20in%20Java,-
Following%20are%20some&text=The%20name%20of%20the%20constructor%20must%20be
%20the%20same%20as,classes%20can%20call%20the%20constructor.
• https://www.tutorialspoint.com/what-are-the-rules-to-create-a-constructor-in-java
• https://www.javatpoint.com/java-constructor
Tutorial:
Inheritance: https://www.youtube.com/watch?v=ptyqpfyB6oA
Constructor: https://www.youtube.com/watch?v=zoZ8uzpDiBA