Results 1 to 7 of 7

Thread: abstract

  1. #1
    maheshpwr is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default abstract

    What is difference between abstract class and interface ???
    and when the used ???

  2. #2
    vitthal is offline Member
    Join Date
    Mar 2011
    Location
    Pune, India
    Posts
    5
    Rep Power
    0

    Default

    Abstract class can contain abstract methods, abstract property as well as other members (just like normal class).

    Interface can only contain abstract methods, properties but we don’t need to put abstract and public keyword. All the methods and properties defined in Interface are by default public and abstract.

    //Abstarct Class
    public abstract class Vehicles
    {
    private int noOfWheel;
    private string color;
    public abstract string Engine
    {
    get;
    set;
    }
    public abstract void Accelerator();
    }
    //Interface
    public interface Vehicles
    {
    string Engine
    {
    get;
    set;
    }
    void Accelerator();
    }

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by vitthal View Post
    Abstract class can contain abstract methods, abstract property as well as other members (just like normal class).
    What's an abstract property?
    Or do you mean simply abstract getters and setters (which would be abstract methods anyway)?

    Oh, and if you're going to give sample code at least make sure it's vaguely correct.

  4. #4
    vitthal is offline Member
    Join Date
    Mar 2011
    Location
    Pune, India
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    What's an abstract property?
    Or do you mean simply abstract getters and setters (which would be abstract methods anyway)?

    Oh, and if you're going to give sample code at least make sure it's vaguely correct.
    I am not that much senior to answer you but if you are asking then I will try,
    An abstract method/property is a method that is declared without an implementation.
    When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.

    Can you explain what is wrong in that code?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Yes, but the term "abstract property"?
    That makes no sense to me.
    Surely that's just an abstract method? Since you can't have abstract attributes...

    As for the code:
    Java Code:
    public abstract [B]string [/B]Engine
    {
    [B]    get;
        set;
    [/B]}
    
    public interface Vehicles
    {
    [B]    string Engine
        {
            get;
            set;
        }[/B]
        void Accelerator();
    }
    The bits in bold are all wrong.

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by maheshpwr View Post
    What is difference between abstract class and interface ???
    and when the used ???
    an other difference is when a class want to use an interface the class must be declared with the keyword implements and when the class want to use an abstract class the class must be declared with the keyword extends. in both cases the class must implement all abstract methods.

    @vitthal: Real-world objects contain state and behavior, java objects contain fields and methods.
    Last edited by j2me64; 03-08-2011 at 10:00 AM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by j2me64 View Post
    an other difference is when a class want to use an interface the class must be declared with the keyword implements and when the class want to use an abstract class the class must be declared with the keyword extends. in both cases the class must implement all abstract methods.
    Unless the class itself is declared abstract.
    :)

Similar Threads

  1. Replies: 4
    Last Post: 02-19-2011, 04:32 AM
  2. Replies: 3
    Last Post: 08-13-2010, 04:57 AM
  3. Replies: 3
    Last Post: 09-16-2009, 09:27 PM
  4. Replies: 2
    Last Post: 11-21-2008, 12:20 AM
  5. Replies: 6
    Last Post: 10-27-2008, 12:25 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
  •