Does a class inherit the constructor of its super class?if
it does, how can you hide that constructor? if it doesnot
how can you call it from the sub class?

Answers were Sorted based on User's Feedback



Does a class inherit the constructor of its super class?if it does, how can you hide that construc..

Answer / pandu

Hi ,
constructor is never been inherited from superclass to
subclass.
we can call the Super class constructor in Sub class
by using
Super();
that too this stement should be first line in method.

Best Regards.

Is This Answer Correct ?    10 Yes 1 No

Does a class inherit the constructor of its super class?if it does, how can you hide that construc..

Answer / hanu

there is an exceptional case for this.i.e when you call
derived class parameterised constructor and if you do not
call any of the super class constructor then bydefault the
superclass default constructor gets executed prior to the
derived argument constructor then the derived class
parameterised constructor gets executed.

example code:

class A
{
A()
{
System.out.println("in Class A");
}
A(int c)
{
System.out.println("in Class A"+c);
}
};
class B extends A
{
B()
{
System.out.println("in Class B");
}
B(int z)
{

System.out.println("in Class B"+z);
}
};


class Dead
{
public static void main(String[] args)
{
System.out.println("Hello World!");
//B b=new B();
B d=new B(20);


}
}

output:

---------- java ----------
Hello World!
in Class A
in Class B20

Output completed (0 sec consumed) - Normal Termination

Is This Answer Correct ?    1 Yes 3 No

Post New Answer

More Core Java Interview Questions

How to display all the prime numbers between 1 and n (n is the number, get the input from user)

0 Answers  


Why does my function print none?

0 Answers  


what are the differences between java and .net?..why u choose java?

1 Answers  


Is space a character in java?

0 Answers  


What is substring in java?

0 Answers  






why string constant pool in java

2 Answers   TCS,


What is the effect of keeping a constructor private?

0 Answers  


what are the differences between final,finally,finalize methods?

14 Answers   IBM,


4.1 Supply contracts (in the form of comments specifying pre- and post conditions) for the enqueue() method of the LinkedQueue class given in the Appendix. (2) 4.2 Let Thing be a class which is capable of cloning objects, and consider the code fragment: Thing thing1 = new Thing(); //(1) Thing thing2 = thing1; //(2) Thing thing3 = (Thing) thing1.clone(); //(3) Explain how the objects thing2 and thing3 differ from each other after execution of the statements. (

0 Answers  


What is passing parameters in java?

0 Answers  


5 Coding best practices you learned in java?

0 Answers  


why java main method is given as static method?

2 Answers  


Categories