Constructor
is a special method given in OOP language for creating and initializing object.
In java constructor role is only initializing object, and new keyword’s role is creating an object.
Rules in defining constructor
·
Constructors name should be same as the class
name.
·
It should not contain return type.
·
It should not contain modifiers.
·
In its logic, return statement with value is not
allowed.
Syntax –
class Employee
{
Employee()
{
}
}
Rule for calling a constructor
Constructor
must be called along with “new” keyword
else it leads to compile time error.
Types of constructors – Java supports
3 types of constructors. They are-
1. Default constructor – The compiler
generated constructor is called default constructor. It does not have
parameters and logic except super() call.
2. No-argument or non-parameterized
constructor – The developer generated constructor without
parameters is called no-arg/non-parameterized.
3. Parameterized constructor – The developer
generated constructor with parameters is called parameterized constructor.
Logic can also be provided.
Difference between default and no-argument
constructor
Default Constructor
|
No-argument Constructor
|
It is
generated by compiler.
|
It is
defined by developer.
|
It’s
accessibility modifier is same as class accessibility modifier, so the only
allowed accessibility modifiers are default or public.
|
It can
have all four accessibility modifiers as it is defined by developers, so the
allowed accessibility modifiers are private, protected, default, public.
|
It does
not have logic except super() call.
|
It can
have logic including super() call.
|
Constructor Overloading – Defining
multiple constructor in a class with different parameters type list/order is
called constructor overloading. Like methods, constructors can also be
overloaded, but they cannot be overridden because constructors are not
inherited, and we cannot create constructor in subclass with super class name
Example –
public class
Cons1 {
public
Cons1()
{
System.out.println("No argument constructor");
}
public
Cons1(int a)
{
System.out.println("Int argument constructor");
}
public
Cons1(String a)
{
System.out.println("String argument constructor");
}
public static void
main(String[] args) {
Cons1 c1
= new Cons1();
Cons1 c2
= new Cons1(10);
Cons1 c3
= new Cons1("Rakesh");
}
}
Output –
No argument constructor
Int argument constructor
String argument constructor
Example –
class
Example{
int x;
public
Example()
{
x=10;
}
public
Example(int a)
{
x=a;
System.out.println("Int argument constructor = "+x);
}
}
class
Cons2 extends Example {
String name;
Cons2()
{
System.out.println("No argument constructor");
}
Cons2(String a)
{
super(10);
name=a;
System.out.println("String argument constructor = "+name);
}
public static void
main(String[] args) {
Cons2 c1
= new Cons2();
Cons2 c2
= new Cons2("Rakesh");
}
}
Output –
No argument constructor
Int argument constructor = 10
String argument constructor =
Rakesh
Constructors chaining – Calling
one constructor from other constructor by using super() and this() is called
constructor chaining. Subclass constructors are chained with super class
constructors by using super() and subclass overloaded constructors are chained
by using this().
Example –
class
Superclass {
public
Superclass()
{
this(10);
System.out.println("No argument");
}
public
Superclass(int a)
{
this("Rakesh");
System.out.println("int argument");
}
public
Superclass(String s)
{
System.out.println("String argument");
}
}
class
Subclass extends Superclass{
public
Subclass()
{
this(10);
System.out.println("No argument");
}
public
Subclass(int a)
{
this("Rakesh");
System.out.println("int argument");
}
public
Subclass(String s)
{
System.out.println("String argument");
}
}
class
Test {
public static void
main(String[] args) {
new
Subclass();
new
Subclass(10);
new
Subclass("Rakesh");
}
}
Output –
String argument
int argument
String argument
int argument
No argument
String argument
No comments:
Post a Comment