DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2007

    Can we create instance variables of an interface

    What will happen in the following code

    Interface I
    ArrayList A;
    class B implements I
    {A al=new A();}

  2. #2
    Join Date
    Dec 2004
    San Bernardino County, California

    An interface has one or more methods which are not defined [this is similar to an "Abstract Class"]. You must define it/them in the class which implements the interface.

    Your example, however, does not "create an instance variable of an Interface" as your title suggests. Your "B" constructor will not compile because there is no datatype "A" - you have defined A as an instance of ArrayList. An instance of a class is not its own class; you cannot make an instance of an instance.

    Because B implements I, it already has an ArrayList A as a field (as a result of its "hierarchy/inheritance"). You don't need to recreate it.

  3. #3
    Join Date
    Aug 2007
    The field "ArrayList A" must be initialized in the interface I. Otherwise, you will get a compiler error...

    Interface I
    ArrayList A;

Similar Threads

  1. Can't create a PARADOX file and insert records using ODBC
    By Steve Oliver in forum VB Classic
    Replies: 0
    Last Post: 07-26-2002, 12:30 PM
  2. Replies: 6
    Last Post: 03-27-2002, 08:13 PM
  3. to VB or to C#
    By jason in forum .NET
    Replies: 66
    Last Post: 08-14-2001, 12:22 PM
  4. Replies: 0
    Last Post: 04-19-2001, 05:29 AM
  5. Help needed before 25/1/01!!!!
    By Chris reid in forum Java
    Replies: 3
    Last Post: 02-11-2001, 08:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.