java - Constructor error in my sub class constructor -
here assignment:
design , implement class representing person along 3 subclasses using following guidelines:
a.create class named person , 3 subclasses named employee, student, retired.
b.the person has following data fields: name, year_of_birth, isstudying, , isemployed. has methods setting , getting values of each of fields along method calculate current age , display status of person. constructor sets isstudying , isemployed fields false included in person class. welcome add additional data fields , methods if like.
1.finally, create java test class simulates using person class. in test class should @ minimum: a) construct 4 instances of person, b) print names of instances c) print status of instances based on values of age, isstudying , isemployed attributes.
public class person2 {//begin class //declare variables string name; int year_of_birth; boolean isstudying; boolean isemployed; int age; public person2(string name, int year_of_birth, boolean isemployed, boolean isstudying, int age){//begin constructor this.name = name; this.year_of_birth = year_of_birth; this.isemployed = false; this.isstudying = false; this.age = age; }//end constructor public int getyear(){//get year method return year_of_birth; }//end method public string getname(){//get name method return name; }//end method public boolean getemployed(){//get employed method return isemployed; }//end method public boolean getstudying(){//get employed method return isstudying; }//end method public int getage(){//get year method age = 2014 - year_of_birth; return age; }//end method public void setname(string name){//set name method this.name = name; }//end method public void setyear (int year){//set year method this.year_of_birth = year; }//end method public void setemployed(boolean employed){//set employed method this.isemployed = employed; }//end method public void setage (int age){//set year method this.age = age; }//end method public static void main(string[] args) { // todo code application logic here } } class student extends person2 {//begin class public student(string name, int year_of_birth, boolean isemployed, boolean isstudying, int age){//begin constructor this.name = name; this.year_of_birth = year_of_birth; this.isemployed = isemployed; this.isstudying = isstudying; this.age = age; }//end constructor) @override public int getyear(){//get year method return year_of_birth; }//end method @override public string getname(){//get name method return name; }//end method @override public boolean getemployed(){//get employed method return isemployed = false; }//end method @override public boolean getstudying(){//get employed method return isstudying = true; }//end method @override public int getage(){//get year method age = 2014 - year_of_birth; if (age > 30){ system.out.println("person not student"); } return age; }//end method }
this code isn't complete getting hung on constructor error. says "actual , formal arguments differ in length".
you problem not invoking super constructor person2
. constructor in student
trying invoke default constructor (with no arguments) of person2
, doesn't exist.
you have 1 constructor in person2
, , should call constructor constructor of student
:
public student(string name, int year_of_birth, boolean isemployed, boolean isstudying, int age) { super(name, year_of_birth, isemployed, isstudying, age); }
this means don't need repeat of initialisation code twice.
you shouldn't repeating methods in student
in person2
. if take them out, student
inherit them anyway. that's whole point in extending class in first place. should override method if don't want inherited behaviour, want student
specific behaviour instead.
Comments
Post a Comment