Path: csiph.com!news.mixmin.net!aioe.org!.POSTED!not-for-mail From: Xue Feng Newsgroups: comp.lang.java.programmer Subject: How to write constructor for an abstarct class? Date: Mon, 28 Dec 2015 20:44:44 +0800 Organization: Aioe.org NNTP Server Lines: 34 Message-ID: NNTP-Posting-Host: qXPm09R4iHeByFUky4mqcQ.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0 X-Notice: Filtered by postfilter v. 0.8.2 X-Mozilla-News-Host: news://nntp.aioe.org:119 Xref: csiph.com comp.lang.java.programmer:33399 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.