-
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();}
}
-
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.
-
The field "ArrayList A" must be initialized in the interface I. Otherwise, you will get a compiler error...
Interface I
{
ArrayList A;
}
Similar Threads
-
By Steve Oliver in forum VB Classic
Replies: 0
Last Post: 07-26-2002, 12:30 PM
-
By Trey Hutcheson in forum .NET
Replies: 6
Last Post: 03-27-2002, 08:13 PM
-
Replies: 66
Last Post: 08-14-2001, 12:22 PM
-
Replies: 0
Last Post: 04-19-2001, 05:29 AM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks