Thread: Abstract class

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    204
    Rep Power
    13

    Abstract class


    Hi,

    I have an abstract Employee class as shown below.

    Code:
    public abstract class Employee
    {
       public abstract double earnings();
    }
    if I create the below statement, it will work.

    Code:
    Employee[] employees = new Employee[2];
    But the below statement will not work due to Employee is abstract. It cannot be instantitated.

    Code:
    Employee employee = new Employee();
    Why does it work for the employees array but not work for the employee object? Is an array the same an object?

    Thanks,
    Last edited by soap; October 1st, 2013 at 03:01 PM.
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    What you are creating is an array of references to Employee objects. Initially each reference will be null.

    In order to instantiate an element, you would have to subclass Employee so that the subclass is not abstract and create an instance of that subclass.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    204
    Rep Power
    13
    Thanks! I got it.

IMN logo majestic logo threadwatch logo seochat tools logo