Results 1 to 9 of 9
  1. #1
    vivekbarange is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default Interface and abstract class

    If i declare a abstract class without any method and a interface without any method.In this case which one is better to use in our program.

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

    Default

    Quote Originally Posted by vivekbarange View Post
    If i declare a abstract class without any method and a interface without any method.In this case which one is better to use in our program.
    Neither. Both. Either.

    If you need a more informative answer, you may wish to ask a more informative question by providing context regarding the problem you're trying to solve and details of your current program. You can find more suggestions on how to ask a better question in my link below on how to ask smart questions.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Which do you think?

    db

  4. #4
    vivekbarange is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    i think, we can implement more thn one interface but in case of abstract class only one class can extended.Do you have any other idea.

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

    Default

    Quote Originally Posted by vivekbarange View Post
    i think, we can implement more thn one interface but in case of abstract class only one class can extended.Do you have any other idea.
    Again context is important. What are you trying to achieve with this? What is your goal?

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    With so little information it's hard really to say. When you have methods, you should try to use interfaces over abstract classes(since you can implement many, and only inherit once). If you are only planning to have constants you shouldn't really use an interface. If the class is only going to have constants you may want to consider making a concrete uninstantiable class(something like Math), which has static constants and a private constructor.

    Java Code:
    public class X{
      private X(){
        throw new IllegalAccessException("Cannot instantiate class");
      }
      public static final int x = ...;
      //more constants as needed
    }

    It's a bit hard to really give concrete answers though without more information.

  7. #7
    vivekbarange is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    Thank you very much to give these information about class and interface....Can you please tell me which one is faster?

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Which do you think?

    db

  9. #9
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Its not really an answerable question. Deciding whether to use an Abstract class or an Interface is completely situational. Some times you are better off using an Abstract class and other times you aren't. Programming does not accept a one size fits all solution to problems.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Abstract Class & interface
    By suresh.sa in forum New To Java
    Replies: 1
    Last Post: 10-18-2010, 08:25 AM
  2. Replies: 6
    Last Post: 02-11-2009, 10:51 AM
  3. Interface and Abstract Class
    By kian_hong2000 in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 02:22 PM
  4. Interface Vs Abstract Class
    By javarishi in forum New To Java
    Replies: 5
    Last Post: 06-15-2008, 05:43 AM
  5. Replies: 0
    Last Post: 04-02-2008, 07:04 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
  •