Article View: borland.public.jbuilder.java.language
Article #8053Re: beginning java: constructor problem with subclass
From: Torsten Welches
Date: Sun, 22 Apr 2001 07:44
Date: Sun, 22 Apr 2001 07:44
61 lines
2526 bytes
2526 bytes
the error is due to the fact that the compiler add a default constructor if
(and only if) you haven't defined any constructors yourself. TestPrivate2
has no constructor, so the compiler does his job and adds the following code
(behind the curtain)
public TestPrivate2 {
super();
}
Recall that the very first statement of *every* constructor (except
Object's) is always a call to a parent constructor (or an overloaded
constructor) - if you don't do it yourself, the compiler assumes "super();"
for you.
You now can easily see why the compiler is complaining, TestPrivate1a has no
matching constructor for (no args).
You would either need to add a no-args constructor to TestPrivate1a or write
TestPrivate2 default constructor so that it properly calls TestPrivate1a's
sole constructor, e.g.
public TestPrivate2 {
super(0);
}
Torsten
hi schrieb in Nachricht <3ae27b56$1_1@dnews>...
>Hi,
>
>I am using some simple classes to test the different access specifiers, but
>I am getting the following error when I try to compile a subclass:
>
>TestPrivate2.java:1: cannot resolve symbol
>symbol : constructor TestPrivate1a ()
>location: class TestPrivate1a
>public class TestPrivate2 extends TestPrivate1a
> ^
>1 error
>
>
>Here are my classes:
>
>public class TestPrivate1a
>{
> protected int iAmPro;
> public TestPrivate1a(int x)
> {
> iAmPro = x;
> }
>}
>
>public class TestPrivate2 extends TestPrivate1a
>{
> public static void main(String[] args)
> {
> TestPrivate1a aTestPrivate1a = new TestPrivate1a(10);
> System.out.println(aTestPrivate1a.iAmPro);
> }
>}
>
>
Technical Details
Message-ID:
<3ae28b34_2@dnews>Path:
arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!newsswitch.lcs.mit.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnewsReferences:
<3ae27b56$1_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,69cce75b48e20b78 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-04-22 00:48:04 PST From: "Torsten Welches" <torsten.welches@who.cares.netcologne.de> Newsgroups: borland.public.jbuilder.java.language References: <3ae27b56$1_1@dnews> Subject: Re: beginning java: constructor problem with subclass Date: Sun, 22 Apr 2001 09:44:21 +0200 Lines: 63 X-Newsreader: Microsoft Outlook Express 4.72.3110.5 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 NNTP-Posting-Host: 213.168.95.242 Message-ID: <3ae28b34_2@dnews> X-Trace: dnews 987925300 213.168.95.242 (22 Apr 2001 00:41:40 -0700) Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!newsswitch.lcs.mit.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews