how to call a constructor in java calling a constructor in java types of calling a constructor in java super at constructor level this at constructor level how to call a constructor in java from another class how to call a constructor in java from another class with example how to call a constructor in java with example how to call parent class constructor in java how to call base class constructor in java how to call another constructor in java calling constructor from another constructorjava calling default constructor from another constructorjava calling overloaded constructor in java how to call a constructor from another class constructor how to call a constructor from another class constructor with example calling super constructor in java can we call a constructor from another constructor in java call a constructor from another constructor in java super at constructor level at constructor level this at constructor level at constructor level
In java calling a constructor from another constructor in same class or different class
can be divided into two types.
They are
1) this function
2) Super function
1) this() and this(…) functions:
These are two implicit functions used for the
establishing the communication between
current class constructors.
1) this()
2) this(….)
1) this(): It is used for calling current class default
constructor from current class parameterized constructor’s
2) this(…): It is used for calling current class
parameterized from other category constructor of same class.
Rules for this() and this(…) implicit
functions:
1) While we are using this() and
this(..) in the current class constructor, they must be used always as first
executable statement (otherwise we get compile time error).
2) While we are using this() and this(…)
in current class constructors, it is highly recommended not to form cycles leads to bad recursion and recommended to
from only path.
Following Example illustrate the concept of this() and this(…)
Class Basedemo1
{
int a;
Basedemo1()
{
System.out.println(“this is default constructor base demo”);
this.a=a;
System.out.println(“value of base demo is”+a);
}
Basedemo2(int a)
{
this( ); //calling default constructor from single paremterized constructor in same class
System.out.println(“Derived class demo parameterized constructor”);
b=200;
System.out.println(“value of b from Derived class=”+b);
}
Basedemo3(int a,int b)
{
this(a); //calling the single parameter constructor in same class
System.out.println(“Derived class demo parameterized constructor”);
b=200;
System.out.println(“value of b from Derived class=”+b);
}
}
Class maindemo1
{
Public static void main(String args[])
{
Basedemo3 bd=new Basedemo3(10,20);
}
}
2) Super At
Constructor level:
1) Whenever we develop any inheritance
application it is always recommended to create an object of bottom most derived
class.
2) Because
it contains features of both top most base class and intermediate base
class.
3) Whenever
we create an object of bottom most derived class first we get memory space top
most base class data members, second we get memory space for intermediate base
class data members, at last we get the memory space for bottom most derived
class data members.
4) In
whatever the order memory space is created for data members, In the same order
default values will be initialized by making use of system defined default
constructor i.e; In the hierarchy of inheritance application development top
most based class data members must be initialized first second intermediate
base class data members and at last bottom most derived class data members.
5) we know that initializing the data members of class
will be done by constructors
Hence the above points
makes us to understand the way of calling constructors are executing from top
to bottom.
The above rule makes us to understand to
establish the communication between base class constructor and derived class
constructor.
In order to establish
the communication between base class constructor and derived class constructor we use the following two implicit
functions.
1)
Super()
2)
Super(…)
1) Super():
It is used for call the super class
default constructor from the context of derived class constructors.
The usage of super() in
the context of derived class constructor is optional because base class
contains single default constructor.
2) Super(…):
It is used
for call the super class parameter constructor from the context of derived
class constructors.
The usage of super(…)
in the context of derived class constructor is mandatory because base class
contains multiple parameter constructors.
Rules For Super() Constructor:
Rule 1: Whenever we use either super() or super(..) in the
context of derived class
constructor, their must used always as first
executable statements otherwise we get
compile time error.
Rule 2: Whenever derived class constructor wants to call base
class default constructor
from context of derived class constructor. We need to
write/use super().
The usage of super() in the context of
derived class constructor for base class default
constructor is optionally.
Rule 3: Whenever derived class constructor wants to call base
class parameterized
constructor from context of derived class constructor. We
need to write/use super().
The usage of super(…) in the context of
derived class constructor for base class default
constructor is mandatory.
Following Example illustrate the
concept of super() Constructor:
Class Basedemo
{
int a;
Basedemo()
{
System.out.println(“this is default
constructor base demo”);
a=100;
System.out.println(“value of base demo is”+a);
}
Basedemo(int a)
{
System.out.println(“this is parameterized
constructor base demo”);
this.a=a;
System.out.println(“value of base demo
is”+a);
}
}
Following Example illustrate the
concept of super(…) Constructor:
Class
Basedemo
{
int a;
Basedemo(int a)
{
System.out.println(“this is parameterized
constructor base demo”);
this.a=a;
System.out.println(“value of base demo
is”+a);
}
}
Class
Deriveddemo extends Basedemo
{
int b;
Deriveddemo(int a,int b)
{
super(a);
System.out.println(“Derived class demo
parameterized constructor”);
b=200;
System.out.println(“value of b from Derived
class=”+b);
}
}
Class
maindemo
{
Public
static void main(String args[])
{
Deriveddemo dd=new Deriveddemo(10,20);
}
}
No comments:
Post a Comment