AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
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 ?.



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
profile Saksham Kumar - anyforum.in Saksham Kumar
73433936114
up-rate
5
down-rate

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
profile Garima Gupta - anyforum.in Garima Gupta
596129554552
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in