| Author |
Bacis constructor doubt
|
Satish Kumar
Greenhorn
Joined: Feb 18, 2005
Posts: 23
|
|
hello friends , i m unable to understand the reason behind this error i get with construtor's . Well this is what i did 1.I first wrote the following code and the execution was fine. ---------------------------------------------------- class A { A() { System.out.println("CLASS A defualt CONSTRUCTOR"); } A(int x) { System.out.println("CLASS A PARAMETRISED CONSTRUCTOR"); } }; class B extends A { B() { super(5); System.out.println("CLASS B defualt CONSTRUCTOR"); } B(int x) { System.out.println("CLASS b PARAMETRISED CONSTRUCTOR"); } }; class C { public static void main(String[] arg) { B b= new B(); } }; O/ P : CLASS A PARAMETRISED CONSTRUCTOR CLASS B defualt CONSTRUCTOR ------------------------------------------------------------------------------- In above example since in B's defualt constructor , i was explicitly calling super(5) , it forced the A'S parametrized constructor to be executed ignoring A's default constructor . Now if i remove the A'S default constructor and executed the code , i get this eror (here is the resultant code) ------------------------------------------ class A { /*A() { System.out.println("CLASS A defualt CONSTRUCTOR"); }*/ A(int x) { System.out.println("CLASS A PARAMETRISED CONSTRUCTOR"); } }; class B extends A { B() { super(5); System.out.println("CLASS B defualt CONSTRUCTOR"); } B(int x) { System.out.println("CLASS b PARAMETRISED CONSTRUCTOR"); } }; class C { public static void main(String[] arg) { B b= new B(); } }; o/ p : test.java:20: A(int) in A cannot be applied to () { ^ 1 error -------------------------------------------------------------- Now in the first programe ( with default constructor) the prog was running fine , now if i remove this defualt constructor (which i m not using in any part of my programe ) y do i get above error. Some 1 plzz clarify .
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
The B(int x) constructor is implicitely calling the A() constructor. If you don't want it to, you need to explicitely call a different one using super(some parameters);
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Satish Kumar
Greenhorn
Joined: Feb 18, 2005
Posts: 23
|
|
|
thanks to all those replies , i m now confident with constructors
|
 |
 |
|
|
subject: Bacis constructor doubt
|
|
|