java - Abstract class cannot be instantiated -
i had create point program year ago, year ago worked fine. have revisit , upon compiling , trying run ran error of fact abstract class cannot instantiated. have done looking around online , figured out update or sort java has made method of using pointclass point1 = new pointclass();
no longer valid , through error.
i have yet find answer fixing error when trying instantiate class using driver program. saw in order use abstract class now, subclass must present. thing is due instructions of program cannot use subclass. driver , point class.
the program simple, declare points , call them abstract class in order print screen. need on figuring out updated method make work again without instantiated error.
the pointclass
public abstract class pointclass { private int pointx; private int pointy; //set instance variables public pointclass() { this.pointx = 10; this.pointy = 10; } public pointclass(int x, int y){ this.pointx = x; this.pointy = y; } //make getters , setters public void setpointx(int x) { this.pointx = x; } public void setpointy(int y) { this.pointy = y; } public int getpointx() { return this.pointx; } public int getpointy() { return this.pointy; } //make string format driver public string tostring() { return "x = " + this.pointx + " y = " + this.pointy; } }
the driver
public class pointtest { public static void main(string[] args){ system.out.println(); pointclass point1 = new pointclass(); //set point1 no argument pointclass point2 = new pointclass(11, 24); // set point2 argument x , y system.out.println("point1: " + point1); //display point1 tostring method system.out.println(); system.out.println("point2: " + point2); //display point2 tostring method system.out.println("---------------------"); } }
the best thing remove abstract
keyword. there's no need it. point
has no abstract methods.
if can't whatever reason, can create inline anonymous classes adding curly braces after each instantiation:
pointclass point1 = new pointclass() { }; pointclass point2 = new pointclass(11, 24) { };
by way, claim used work incorrect. has never been possible directly instantiate abstract class. in fact entire point of keyword, prevent class being instantiated.
Comments
Post a Comment