Results 1 to 8 of 8

Thread: Abstract Class Situation

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Geek_Guest
    Guest

    Abstract Class Situation

    When to use the Abstract Class? (We have interface and Abstract class, both used for declarations of common methods and subclasses will give the implementations. Please give em the situation when to use the abstract class)

    Question asked by visitor gskumar81


  2. #2
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    Re: Abstract Class Situation

    Go through the following URL. We already discuss with this. Hopefully this will help you.

    When we use Abstract Class?

    ---------------------
    suresh


  3. #3
    Junior Member
    Join Date
    Oct 2007
    Answers
    5

    Re: Abstract Class Situation

    Abstract class may or may not have implementation.They may or may not have abstract methods.The purpose of class declaring abstract is to prevent the user from instantiating.

    For example if we have a abstract class human:
    public abstract class human{

    public abstact void reproduce()
    {

    }
    public void eatfood()
    {
    //some implementation here
    }
    }

    public class man extends human
    {
    public void reproduce()
    {
    ///implementation
    }
    }
    public class woman extends human
    {
    public void reproduce()
    {
    ///implementation
    }
    }


    Please note that even in absence of abstrart function reproduce it would be vague to instantiate class human(coz human is either a man or a woman).hence some classes are declared abstarct even though they dont have abstract methods.

    On the other hand some methods are declared abstract to force the implementor of class to define them.For example creator of class man and woman has to implement method reproduce


  4. #4
    Junior Member
    Join Date
    Jan 2007
    Answers
    2

    Re: Abstract Class Situation

    well my friend, In Abstarct Classes u can also implement the normal methods while it is not possible in case of Interface.


  5. #5
    Junior Member
    Join Date
    Sep 2007
    Answers
    3

    Re: Abstract Class Situation

    Quote Originally Posted by Geek_Guest View Post
    When to use the Abstract Class? (We have interface and Abstract class, both used for declarations of common methods and subclasses will give the implementations. Please give em the situation when to use the abstract class)

    Question asked by visitor gskumar81

    In case of Interface you must have to have redefine all the methods declared in an interface at the time of implementing it, you can't skip any method of it. If you don't need or wish to redefine all of them then you have to make a blank body of those unnecessary methods but it's mandatory to give all the methods a blank body or purposeful definition according to your needs.

    In case of Abstract class you may or may not redefine all of the methods of that class at the time of extending it. You can only redefine the methods which you are in need. Its not mandatory to redefine all the methods of that abstract class.

    So when you are going to inherit a class or an interface you've to decide if you need to redine all of the methods of an interface or only redefine some of the methods of a class. It depends upon you totally. And to make the decision you should know the signatures of an interface or a class (may be an abstracr also).

    BTW, there is another impotant situation, if you're in need of a multile inheritance then you must have to take the help of an interface. Because Java doesn't support multiple inheritance with multiple classes. You can only inherit a single class as its Super but to make it multiple inheritance you can include some interfaces as its parent also. Java restricts to inherit more than one class at a time for the security purpose.

    Is it clear to you, now?

    Thanks,

    kuntal Chandra



  6. #6
    Junior Member
    Join Date
    Aug 2007
    Answers
    1

    Re: Abstract Class Situation

    Quote Originally Posted by Geek_Guest View Post
    When to use the Abstract Class? (We have interface and Abstract class, both used for declarations of common methods and subclasses will give the implementations. Please give em the situation when to use the abstract class)

    Question asked by visitor gskumar81
    If you have situation like part of the implementation(functionality) is same, then you'll need to use Abstract Class rather than Interface.


  7. #7
    Junior Member
    Join Date
    Sep 2007
    Answers
    7

    Thumbs up Re: Abstract Class Situation

    Hi,
    Both abstract classes and interfaces cannot be instantiated. It serve as superclasses to concrete subclasses. In abstract class you can have both abstract methods and nonabstract methods (You can also have an abstract class with no abstract methods). But in interfaces you can have only abstract methods and constants.

    Now about your question, When you want to give common functionality to Related clases, you can use abstract class. The related classes will be providing some funcionality exactly in same way (here you can use nonabstract methods of abstract classes). so you dont have to implement it in all the subclasses. But these classes may be providing some functionality in its own unique way (here you can make use of abstract methods of abstract class). So each and every subclass must implement these functions in its own way.
    eg: Shape as abstract class and Triangle, Rectangle, Circle etc as its sub classes. Think about a function 'calcArea()'.

    Since interfaces supports only abstract methods, you can make use of this for providing common functionality to unrelated classes.
    eg: Tyre and Ball, create a interface called Rollable. declare the function roll(). and make both Tyre and Ball implement this interface

    Hopes explanation is clear to you.

    Bye.....

    Last edited by admin; 10-23-2007 at 04:34 AM.

  8. #8
    Junior Member
    Join Date
    Jan 2007
    Answers
    22

    Re: Abstract Class Situation

    okay well Lets' see ,,,
    if i ask you to create an Animal , (mind it i m asking an Animal not Dog ,Cat, Lion , Monkey , Cow , Goat ,Whale, Fish , cockroach or like) how wud to create an Animal??? You can't because Animal is a just a blue print , we cant actually have it physically (ie. its Abstract)
    So my friend Animal is Abstract , where as Dog , Cat etc wud be concrete classes which extend Animal class.. Animal will obviously have some default behaviour which is common to all and exactly same for example .. all of them can have a move() method .

    Interface on the other hand tells what a 'THING" can do but does not how it does it ,, interface is more or less like an adverb/adjective not a noun ..
    like like Runnable, Seriliazable , it usually tells what an object can do .

    hope it helps.


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