Results 1 to 4 of 4
  1. #1
    pannix is offline Member
    Join Date
    Oct 2014
    Posts
    5
    Rep Power
    0

    Default Abstract class questions and answers

    The Java course I am doing (distance education) has exercises and questions, but no solutions.
    So I am going to ask some of the questions here, but I am going to try and give an answer first.

    Can an abstract class have concrete (non-abstract) methods?

    Yes. If a class has at least one abstract method, regardless of the number of concrete methods, it is an abstract class.

    Can a concrete class have an abstract method?
    No. The moment a class has an abstract method it is an abstract class and it cannot be instantiated. If not all abstract methods of an abstract superclass have an implementation in a subclass, than the subclass is also an abstract class and cannot be instantiated.

    Is it possible to have an abstract class without abstract methods?
    This is the tricky one. I believe it is possible, but why? Unless it is to prevent the class from being instantiated, forcing to use a concrete subclass.

    Thanks for your time.
    PS: My native tongue and the course are in Dutch, so I had to translate on the fly. I hope I used to word instantiated correctly.

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

    Default Re: Abstract class questions and answers

    One use of an abstract class without abstract methods is to serve as a base class of pre-implemented, common behaviors. Then other classes may extend that class as appropriate.

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

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Abstract class questions and answers

    Quote Originally Posted by pannix View Post
    Yes. If a class has at least one abstract method, regardless of the number of concrete methods, it is an abstract class.
    Even if a class doesn't have any abstract method at all, it is an abstract class if it is marked so.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Abstract class questions and answers

    Quote Originally Posted by jim829 View Post
    One use of an abstract class without abstract methods is to serve as a base class of pre-implemented, common behaviors. Then other classes may extend that class as appropriate.
    ... and you can't accidentally make an instance of the abstract base class.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 3
    Last Post: 05-25-2013, 02:01 PM
  2. Questions about interface and abstract class
    By Sswordx in forum New To Java
    Replies: 6
    Last Post: 08-25-2009, 01:05 PM

Posting Permissions

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