Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: An array from abstract parent?

  1. #1
    Member
    Join Date
    Jul 2012
    Location
    Dallas area
    Posts
    34
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default An array from abstract parent?

    Please help me clarify an unclear point in my textbook.

    The text says an object cannot be constructed from an abstract parent. But seemingly contradicting this, the example program shows an abstract parent with a constructor, and it also shows an array that was instantiated from that abstract parent class. I thought an array was an object. So if an object cannot be constructed there...

    What am I missing? Is an array an exception to this rule (that an object cannot be constructed in an abstract parent class?

    Do I need to show the code, or is the above explanation enough?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: An array from abstract parent?

    Could you post the example code?

  3. #3
    Member
    Join Date
    Jul 2012
    Location
    Dallas area
    Posts
    34
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: An array from abstract parent?

    First up is the code from the driver creating an array from the abstract parent class. Below that is the abstract parent class' heading and constructor.

     
    public class Payroll
    {
        public static void main(String[] args)
        {
            Employee[] employees = new Employee[100];
        }

    Then the parent abstract class (fragment):

     
    public abstract class Employee
    {
        public abstract double getPay();
        private String name;
     
        //***********************************
     
        public Employee(String name)
        {
            this.name = name;
        }
     
        ..................
     
    } end class Employee


    Please let me know if you need to see more code than I've included here.

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: An array from abstract parent?

    Here's the deal:

    When you create an array with a statement like the following, it creates an array of reference variables.
            Employee [] employees = new Employee[100];

    It hasn't actually tried to create any new Employee objects yet. To some of us this may seem a little strange, but it's important to know stuff like this.

    Next...

    If you try to create an Employee object and set array element zero to refer to that object, you will get the error that you expected:
    public class Payroll
    {
        public static void main(String [] args)
        {
            Employee[] employees = new Employee[100];    //<--- This is OK (Well, generates no errors, but not particularly useful)
            employees[0] = new Employee("Richard Dent"); //<--- This is not OK, since Employee is an abstract class
        }
    }

    What I got, specifically, was
    Payroll.java:6: Employee is abstract; cannot be instantiated
            employees[0] = new Employee("Richard Dent");


    Now for people with experience with C++ (and certain other object-oriented languages), this is really quirky. For Java guys and Java wannabes (like me), it's fundamental.

    Bottom line(s):

    A declaration and definition of an array of fundamental data types (like ints) creates storage for the ints. Each element can be assigned an int value. (With an assignment statement, for example.)

    A declaration and definition of an array of objects creates an storage for reference variables. Each element does not hold an object, but can be set to refer to an object of that type. (With an assignment statement with an existing object of that class or by using new to create a new object of that class.)


    Cheers!

    Z
    Last edited by Zaphod_b; September 26th, 2012 at 04:45 PM.

  5. #5
    Member
    Join Date
    Jul 2012
    Location
    Dallas area
    Posts
    34
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: An array from abstract parent?

    Here's a quote from the textbook in question. It might shed some light on my problem.

    "Adding an abstract modifier to a class heading makes it impossible to instantiate an object from that class. If a program attempts to instantiate an abstract class, the compiler generates an error. For example, since Employee (my code example above) is an abstract class, we'd get a compilation error if we had a main method like this:

     
    public static void main(String[] args)
    {
        Employee emp = new Employee("Bob");
    }

    I hope that helps. The above shows that an object cannot be created in an abstract class. But my previous code sample above shows that this textbook created an array in that same abstract class. That seems contradictory. What's up with that?

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: An array from abstract parent?

    Quote Originally Posted by pict3000 View Post
    ...What's up with that?
    I guess my reply got posted just as you were preparing your response to a previous question. See if my explanation sheds any light...

    Cheers!

    Z
    Last edited by Zaphod_b; September 26th, 2012 at 06:52 PM.

  7. #7
    Member
    Join Date
    Jul 2012
    Location
    Dallas area
    Posts
    34
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: An array from abstract parent?

    Yes, that helps, Zaphod_b. Thank you. This book, although impressive in general, has been known to be ambiguous from time to time. Here again they have dangled a participle or two and let me wander away from their point. Regardless of your explanation, which I understand, they still seem be using an array of instantiated objects from an abstract class. So I suppose I MUST have misread something somewhere. I'll go back and re-read a fourth time.

  8. #8
    Member
    Join Date
    Jul 2012
    Location
    Dallas area
    Posts
    34
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: An array from abstract parent?

    I've got it now! (he says with an echo to an empty auditorium)

    Just as Zaphod_b said (above), the creation of the array was NOT an illegal instantiation of an object in the abstract class Employee - as much as it looks that way, it's not. I was misled by the existence of a constructor. It's in an abstract class (Employee) and it's illegal for an object to be instantiated in an abstract class. So the constructor, along with the array-creation statement, seemed contradictory to the claim that an object cannot be instantiated there.

    In the end, the array was created to hold references to objects instantiated in other classes, descendant classes, for the sake of illustrating polymorphism and the subsequent use of abstract classes and methods.

    If this were not my own post, I would surely be asleep by now.

    Thanks again for your in-depth explanation, Zaphod_b! And thank you to all the empty seats out there, for listening.

Similar Threads

  1. GUI error: is not abstract and does not override abstract method
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 21st, 2011, 01:26 PM
  2. Access and set variable in parent class through child
    By java_newbie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 19th, 2011, 11:44 PM
  3. saving the data of the child jsp into parent jsp
    By nrao in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 15th, 2011, 11:05 AM
  4. GUI does not pop up in child process before parent is killed
    By Antipeko2 in forum Java Theory & Questions
    Replies: 0
    Last Post: February 26th, 2010, 06:32 AM
  5. setting location of a dialog relative to the parent
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 1st, 2009, 05:42 PM

Tags for this Thread