Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: How to write constructor for an abstarct class? Date: Mon, 28 Dec 2015 08:21:08 -0500 Organization: A noiseless patient Spider Lines: 47 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 28 Dec 2015 13:18:34 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="2d081e2f7155c28005d4472f67c2f43d"; logging-data="30155"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/YIH4KzM/9+ecNRlnFM3qL" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0 In-Reply-To: Cancel-Lock: sha1:oVEVRiE5QfMLYcovkRxvssg6EBA= Xref: csiph.com comp.lang.java.programmer:33400 On 12/28/2015 7:44 AM, Xue Feng wrote: > I have written a Shape class to test abstract class. My plan is to > include two fields x and y to represent the coordinate of a shape, and > add two constructors to initialize the coordinate. In addition, two > abstract methods, namely getArea() and getPerimeter(), is included to > calculate the area and perimeter of a shape respectively. The two > methods will be overrided by subclass of Shape. The code is as follows: > > class Shape { > int x, y; > > public Shape(int x, int y) { > this.x = x; > this.y = y; > } > > public Shape() { > this(0, 0); > } > > public abstract double getArea(); > public abstract double getPerimeter(); > } > > While compiling, the second constructor > > public Shape() { > this(0, 0); > } > > will cause error. I am aware of that Shape is an abstract class, hence > can not be instantiated. But, how can I write the constructor > alternately? Thanks. I see no problem with the constructor. What is "error" you get? However, the code you posted has a problem: The class itself must be declared `abstract' if it has abstract methods, as in abstract class Shape { ... Are you sure that the code you posted is the same code that gives you trouble? -- esosman@comcast-dot-net.invalid "Don't be afraid of work. Make work afraid of you." -- TLM