Can we create instance variables of an interface


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Can we create instance variables of an interface

  1. #1
    Join Date
    Aug 2007
    Posts
    4

    Can we create instance variables of an interface

    What will happen in the following code

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

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    No.

    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
    Posts
    2
    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, 01:30 PM
  2. Replies: 6
    Last Post: 03-27-2002, 09:13 PM
  3. to VB or to C#
    By jason in forum .NET
    Replies: 66
    Last Post: 08-14-2001, 01:22 PM
  4. Replies: 0
    Last Post: 04-19-2001, 06:29 AM
  5. Help needed before 25/1/01!!!!
    By Chris reid in forum Java
    Replies: 3
    Last Post: 02-11-2001, 09: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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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