Results 1 to 7 of 7
  1. #1
    gangelo is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default Properties in Interfaces?

    Is it possible to define a property in a Java Interface? I can do this in C#.Net, is there a way to do it in Java?

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    what's a property's equivalent in java? you can define methods and public static final fields, hopefully one of those is a 'property'.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You can define "getter" and "setter" methods in an interface which I believe are the Java equivalents of a C# property.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Furbarable is correct. Simply create get/set methods (accessor methods). Here is an example:
    Java Code:
    private int someNumber;
    
    public void setSomeNumber(int someNumber){
    	this.someNumber = someNumber;
    }
    
    public int getSomeNumber(){
    	return someNumber;
    }

  5. #5
    pushdown is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default

    In Java Interface, only static data member can be declared. Others are not allowed. But in abstract classes defining data members allowed. May be it can be useful for you. Both of them cannot be instantiated by the way.

  6. #6
    gangelo is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by quad64bit View Post
    Furbarable is correct. Simply create get/set methods (accessor methods). Here is an example:
    Java Code:
    private int someNumber;
    
    public void setSomeNumber(int someNumber){
    	this.someNumber = someNumber;
    }
    
    public int getSomeNumber(){
    	return someNumber;
    }
    So, if I understand, there is nothing really different between "getter" and "setter" members and any other members? So the answer is really "no". But than answers my question, than you.

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Right, if your question was "is there special syntax" then the answer is no.

Similar Threads

  1. Interfaces
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 05-03-2008, 10:57 PM
  2. interfaces..
    By sireesha in forum New To Java
    Replies: 5
    Last Post: 01-16-2008, 06:52 PM
  3. Interfaces
    By Kavana Krishnappa in forum New To Java
    Replies: 7
    Last Post: 12-11-2007, 05:28 PM
  4. Interfaces
    By imran_khan in forum New To Java
    Replies: 5
    Last Post: 07-30-2007, 09:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •