java - Constructor in Abstract method -
this question has answer here:
- can abstract class have constructor? 19 answers
why there constructor in abstract method when abstract method cant me instantiated?it confusing me..
code tutorialspoint.com
public abstract class employee { private string name; private string address; private int number; public employee(string name, string address, int number) { system.out.println("constructing employee"); this.name = name; this.address = address; this.number = number; } public double computepay() { system.out.println("inside employee computepay"); return 0.0; } public void mailcheck() { system.out.println("mailing check " + this.name + " " + this.address); } public string tostring() { return name + " " + address + " " + number; } public string getname() { return name; } public string getaddress() { return address; } public void setaddress(string newaddress) { address = newaddress; } public int getnumber() { return number; } } now can try instantiate employee class shown below:
* file name : abstractdemo.java */ public class abstractdemo { public static void main(string [] args) { /* following not allowed , raise error */ employee e = new employee("george w.", "houston, tx", 43); system.out.println("\n call mailcheck using employee reference--"); e.mailcheck(); } } when compile above class, gives following error:
employee.java:46: employee abstract; cannot instantiated employee e = new employee("george w.", "houston, tx", 43); ^ 1 error what m getting trouble if has show error why constructor in abstract classes reason?
it's there concrete subclasses use.
class subemployee extends employee { public subemployee(string name, string address, int number) { super(name, address, number); } ... }
Comments
Post a Comment