Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2016
    Posts
    47
    Rep Power
    0

    Default When to use a superclass vs. an abstract class as a superclass

    In scenario 1, let's say you have a superclass with 3 subclasses and each of those subclasses have instantiated objects. In scenario 2, let's say you have an abstract class with 3 subclasses and each of those subclasses have instantiated objects. In scenario 2, the abstract class would also be a superclass.

    In scenario 1 you would have concrete methods. In scenario 2, you could have abstract methods but you can also have concrete methods. So I don't see the benefit of using one over the other.

    When would you use scenario 1 vs. scenario 2? What is the difference between these two?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: When to use a superclass vs. an abstract class as a superclass

    Can you post some code that demonstrates the problems you are having?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Oct 2016
    Posts
    47
    Rep Power
    0

    Default Re: When to use a superclass vs. an abstract class as a superclass

    Quote Originally Posted by Norm View Post
    Can you post some code that demonstrates the problems you are having?
    I don't have a code example. I was reading about these topics on various websites. I'm trying to understand the idea behind this.

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: When to use a superclass vs. an abstract class as a superclass

    There are no advantages or disadvantages to both solutions, you have to pick one scenario for the given situation. I usually pick scenario 2 if I want to force an instance into one subclass, because an instance of the superclass does not make sense. Let me try an example: abstract superclass FlyingInsect, subclasses Bee, Hornet, Butterfly. You can put all properties and methods of a flying insect in the superclass, but specific methods are in the subclasses (sting() method). This way you have to make up your mind what insect it actually is.
    Last edited by benji2505; 11-09-2016 at 04:37 AM.

  5. #5
    Join Date
    Oct 2016
    Posts
    47
    Rep Power
    0

    Default Re: When to use a superclass vs. an abstract class as a superclass

    Quote Originally Posted by benji2505 View Post
    There are no advantages or disadvantages to both solutions, you have to pick one scenario for the given situation. I usually pick scenario 2 if I want to force an instance into one subclass, because an instance of the superclass does not make sense. Let me try an example: abstract superclass FlyingInsect, subclasses Bee, Hornet, Butterfly. You can put all properties and methods of a flying insect in the superclass, but specific methods are in the subclasses (sting() method). This way you have to make up your mind what insect it actually is.
    This helps. Thank you sir.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: When to use a superclass vs. an abstract class as a superclass

    A completely defined superclass as in scenario 1 is one which can stand on its own without any other modifications.
    One usually only subclasses it if there is need to override some method. Otherwise, use composition. A typical
    example would be JFrame and JPanel. JFrame should rarely be subclassed but JPanel is usually done so and its
    paintComponent method overridden.

    Secenario 2 would be where you have a partial implementation (some methods are abstract and some are concrete).
    The abstract ones are usually those that depend on how the subclass is implemented. You could have some data structure
    that has various support methods for the structure. But the accessor methods would be abstract since the actual backing
    of the structure (array or linked list) is not yet known. Note that if your class is completely abstract and has no fields you
    might be better off using an interface.

    So scenario 2 forces you to implement some methods where scenario 1 allows you to.

    Fully abstract methods are also used only as targets. You can never instantiate an abstract method but
    you can assign it an object which may be provided via some factory class.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 02-24-2016, 07:10 PM
  2. Replies: 11
    Last Post: 03-05-2012, 11:18 AM
  3. LinkedList declared as an abstract superclass
    By box2box in forum New To Java
    Replies: 2
    Last Post: 04-21-2010, 10:32 PM
  4. Class and SuperClass
    By pompeez in forum New To Java
    Replies: 2
    Last Post: 08-05-2009, 10:55 AM
  5. which class is superclass and subclass?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 08:55 PM

Tags for this Thread

Posting Permissions

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