Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Extends will  invoke constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Extends will  invoke constructor?" Watch "Extends will  invoke constructor?" New topic
Author

Extends will invoke constructor?

Ankith suresh
Ranch Hand

Joined: Jun 07, 2007
Posts: 42
package pk1;
public class SCJP11
{
SCJP11()
{
}
}
---------------------------------

package pk2;
import pk1.SCJP11;
class SCJP11Test extends SCJP11 //1
{

}

When i compile the SCJP11Test.java
i am getting following error

SCJP11Test.java:3: SCJP11() is not public in pk1.SCJP11; cannot be accessed from outside package
class SCJP11Test extends SCJP11
^
1 error

Why the error is displaying while extending..?
Compiler trying to access the constructor while extending the class?
I thought error will display only when trying to create the object.


(edit: no bangs in topic title look much better)
[ June 12, 2007: Message edited by: Barry Gaunt ]
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

suresh


In the package pk1 the class is public but the constructor not public,as i explained already in your previous post.

In pk2 you are extending ,so here the compiler checks the constructor of the super class ,it could not found that one,it will give compiler error.


Put the constructor in the package pk1 as public and try again.


AND ALSO TRY BY DECLARING THE CONSTRUCTOR IN THE CLASS SCJP11 AS protected

Thanks

Anil Kumar

[ June 12, 2007: Message edited by: anil kumar ]
[ June 12, 2007: Message edited by: anil kumar ]
Ankith suresh
Ranch Hand

Joined: Jun 07, 2007
Posts: 42
Hi anil
I got it.
So what i understood is ..............

Whenever we extending a class, compiler will check the super class's default constructor accessability . right?
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

suresh

Not the default constructor ,what it checks it depends on your program

say for example


class A
{
A(int a)
{
}
}

class B extends A
{
}


In this program if you try to compile class B you will get compile time erro,like there is no constructor of this type A(){}.

Because in the B class the compiler puts these for you

B()
{
super();
}


Means here the compiler checks wheather default constructor is there or not.Because super() calls default constructor.

B() //line 1
{
super(10);
}


If you right like line 1 this time the compiler checks wheather is there any constructor with single parameter of type int or float or long or double there or not.Because it knows it has to call a constructor of single parameter type.


Thanks

Anil Kumar
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Thats correct Anil kumar!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Extends will invoke constructor?
 
Similar Threads
private constructors
Output of the Program
what is right....and why???
Head first Servlets first example
why this doesn't work on java 1.4?