Results 1 to 10 of 10

Thread: Abstract Classes v/s Interface

  1. #1
    Junior Member
    Join Date
    Nov 2008
    Answers
    1

    Abstract Classes v/s Interface

    Hi,

    Can I get a brief idea as to which to prefer - abstract class or Interface?

    Also, in wat scenario i would prefer abstract class and in what I woud prefer interface????

    Regards
    Prathima


  2. #2
    Junior Member
    Join Date
    Nov 2008
    Answers
    1

    Re: Abstract Classes v/s Interface

    interfaces are better ....


  3. #3
    Junior Member
    Join Date
    May 2008
    Answers
    8

    Re: Abstract Classes v/s Interface

    interfaces are good to use because we can use multiple inhertiance in it.
    we cannot instantiate Abstract classes until all the methods are implemented.


  4. #4
    Junior Member
    Join Date
    Nov 2008
    Answers
    1

    Re: Abstract Classes v/s Interface

    You can use Abstract Class when you want to provide partial implementation of methods and leave the rest for the extending class. Multiple inheritance is not possible in Java, so you can extend only one Abstract Class and implement its methods. However, you can implement as many interfaces as possible. So, Interfaces come in handy. Also, I have seen that if you are defining many constants, they are defined in interface and that interface is implemented. Hope this helps.


  5. #5
    Junior Member
    Join Date
    Nov 2008
    Answers
    1

    Re: Abstract Classes v/s Interface

    Hi All,

    all the methods declared in abstract and interface, In Abstract class have atleast one abstract method , others may concrete.
    Abstract method have atleast one subclass,Interface can't have any sub classes.
    Abstract class we must declare the abstract keyword before the method.In interface we need not to use any keyword for methods.
    An Abstract can extend only one class, where as Interface can extend more than one.

    regards,
    rajeshwar reddy.


  6. #6
    Junior Member
    Join Date
    Nov 2008
    Answers
    1

    Thumbs up Re: Abstract Classes v/s Interface

    Quote Originally Posted by Prathima001 View Post
    Hi,

    Can I get a brief idea as to which to prefer - abstract class or Interface?

    Also, in wat scenario i would prefer abstract class and in what I woud prefer interface????

    Regards
    Prathima
    Hi Prathima,
    Generally,abstract class is written when there are some common features shared by all the objects as they are.

    Let i explain with example!

    Let us take a class WholeSaler which represents a wholesale shop with textbooks and stationery like pens,papers,notebooks etc.,

    class WholeSaler
    {
    void text_books()
    {
    //Text books
    }
    void stationery()
    {
    // this can be Pen,Paper,Notebooks
    }
    }

    Explanation:-

    Let us take Person 1 & person 2

    Person 1 needs J2ee book and Pens.

    Person 2 Needs J2ee book and Notebook.

    Here, the text_book() is common feature shared by both the persons.

    So in this case,The programmer designs the WholeSaler class as an abstract class.person1,person2 are subclasses.


    Suppose, person1 needs j2ee book and papers
    person 2 needs Dotnet book and pens means,

    the text_books() and stationery() needs different implementations depending on the persons.

    So in this case, the programmer desings the WholeSaler class as an interface and person 1 ,person 2 become implementation classes.

    The programmer prefers to write an interface when he/she wants to leave the implementations part to the third party vendors.

    The programmer prefers to write an abstract class when he/she ready to provide the subclasses .


  7. #7
    Junior Member
    Join Date
    Sep 2008
    Answers
    4

    Re: Abstract Classes v/s Interface

    first of all I would like to explain what is abstract method.
    the method which have no body is called as abstract method, and the class which contains at least one abstract method is called abstract class. there may be another concrete classes, but incase of Interface all the methods inside it must be declared as abstract.


  8. #8
    Junior Member
    Join Date
    Oct 2006
    Answers
    2

    Thumbs up Re: Abstract Classes v/s Interface

    Hi
    interface is use when you want to define the peripheral abilities of a class where as An abstract class defines the core identity of a class.


  9. #9
    Junior Member
    Join Date
    Jan 2009
    Answers
    2

    Thumbs up Re: Abstract Classes v/s Interface

    hi
    good question

    Case i abstract:
    when u want to create new method for ur class which can return more than one function(means Abstraction) but you want one of those function, then u go and create abs method which belongs to abs class.Atlast you should override that method in your class which extends the abs class

    For example

    abstract class vehicle
    {
    abstract int wheels();
    }

    class sumo extends vehicle
    {
    public int wheels()
    {
    return 4;
    }
    }


    Case ii interface:

    when u want to create more than one new method for ur class , All of which can return more than one function(means Abstraction) but you want one of those function and all metods might be in different classess or different abs classes in that case you can't extents all of which from your class So, then u go and creat more than one method which belongs to one interface or more than one interface .Atlast you should override that methods into your class which implements the more than one interface.The overriden method implicitely a abstraction feature.


    For example

    interface vehicle
    {
    public static int i=5;
    //Implicitely a absmethod
    public int wheels();
    }

    interface typeHorn
    {
    public static int i=5;
    //Implicitely a absmethod
    public String Horn();
    }


    class sumo implements vehicle,typeHorn
    {

    public int wheels()
    {
    return 4;
    }
    public String Horn()
    {
    return "BEEP";
    }
    }


  10. #10
    Junior Member
    Join Date
    Jan 2009
    Answers
    2

    Re: Abstract Classes v/s Interface

    hi anybody tell me the diference between == and equals() in all variable


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact