AF
Home
Tag
Submit Notes
Ask Anything
Login
Subscribe Us
A
ny
F
orum
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.
Follow @anyforumin
corejava-OOPs: Why Parent class constructor is automatically called in child class constructor?
Why it happens without using super() in child class constructor?
corejava x 353
OOPs x 49
Posted On :
2014-04-03 23:29:53.0
Saksham Kumar
734
339
36114
5
Answers
First of all we should have a look on inheritance concept, whenever we extend the class, the class´s behaviour is automatically comes under the derived class from base class.
Let´s consider an example:
class A
{
A()
{
System.out.println("Hello Constructor A");
}
}
class B extends A
{
B()
{
System.out.println("Hello Constructor B");
}
}
class C
{
public static void main(String args[])
{
B b1 = new B();
}
}
if we create the object of B class in class C, whenever Object initialized, constructor is called.
Class B constructor calls it´s super class constructor. super() is provided by java compiler in each subclass constructor, therefore firstly super class constructor is invoked.
Posted On :
2014-04-03 23:58:23
Satisfied :
1 Yes
0 No
Garima Gupta
596
1295
54552
Reply This Thread
5
Post Answer
Please Login First to Post Answer:
Login
Answer:
anyforum.in