Inheritance classes (Java), explicit constructor error message -
so trying learn inheritance classes.
first created class called box calculate area of box.
then created testbox class in have created box object called fedex.
box class:
public class box { private string boxname; public void calculatearea(int length, int width) { system.out.println("area of " + getboxinfo() + (length * width)); } public box(string boxname) { this.boxname = boxname; } public string getboxinfo() { return boxname; } }
testbox class:
public class testbox { public static void main(string[] args) { box fedex = new box("fedex"); fedex.calculatearea(23, 2); } }
so far if run code works out fine , print screen shows area of fedex 46
so went create new class called newbox , used "extends" inherit methods class box, class used calculate volume
newbox class:
public class newbox extends box { public void calculatevolume(int length, int width, int height) { system.out.println("volume = " + (length * width * height)); } }
now test created new object in testbox class called ups, testbox class looks this:
public class testbox { public static void main(string[] args) { box fedex = new box("fedex"); fedex.calculatearea(23, 2); newbox ups = new newbox("ups"); ups.calculatearea(3, 2); ups.calculatevolume(3, 2, 2); } }
when try run program following error message:
exception in thread "main" java.lang.error: unresolved compilation problem: constructor newbox(string) undefined @ day3.inheritence.testbox.main(testbox.java:10)
i using eclipse ide.
what can fix code, , error message mean?
newbox has have constructor forwards parent class's constructor. try this:
public class newbox extends box{ public newbox(string name) { super(name); } public void calculatevolume(int length, int width, int height){ system.out.println("volume = " + (length*width*height)); } }
Comments
Post a Comment