Results 1 to 7 of 7
  1. #1
    Sswordx is offline Member
    Join Date
    Jul 2009
    Rep Power

    Default Questions about interface and abstract class


    I have a question about interface and abstract class. I'm not sure if the following statements are valid or not.

    Interface class is a class that contains only abstract methods. This class is used because another class wants to obtain all the behaviours from it.

    Abstract class is a class that contains both non-abstract and abstract methods. This class is used because another class wants to obtain certain behaviours from it.

    If the above statements are true, then can't we just use inheritance and override the methods instead?


  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Rep Power


    as for your description of interfaces and abstract classes... no. classes implement/extend interfaces/abstract classes primarily to define behavior for them, not get behavior from them. deriving behavior from the super-class is inheriting, which applies to all classes. the same goes for overriding. moreover, you cannot override an abstract method or an interface's methods, since they are not implemented in the first place.

    also, an interface is not a class.

  3. #3
    turanan is offline Member
    Join Date
    Mar 2009
    Rep Power


    Quote form the guide to SCJP exam:

    A powerful companion to inheritance is the use of interfaces. Interfaces are like a
    100-percent abstract superclass that defines the methods a subclass must support, but
    not how they must be supported. In other words, an Animal interface might declare
    that all Animal implementation classes have an eat() method, but the Animal
    interface doesn't supply any logic for the eat() method. That means it's up to the
    classes that implement the Animal interface to define the actual code for how that
    particular Animal type behaves when its eat() method is invoked.
    Class Modifiers (Nonaccess)
    * Classes can also be modified with final, abstract, or strictfp.
    * A class cannot be both final and abstract.
    * A final class cannot be subclassed.
    * An abstract class cannot be instantiated.
    * A single abstract method in a class means the whole class must be abstract.
    * An abstract class can have both abstract and nonabstract methods.
    * The first concrete class to extend an abstract class must implement all of its
    abstract methods.
    Interface Implementation
    * Interfaces are contracts for what a class can do, but they say nothing about
    the way in which the class must do it.
    * Interfaces can be implemented by any class, from any inheritance tree.
    * An interface is like a 100-percent abstract class, and is implicitly abstract
    whether you type the abstract modifier in the declaration or not.
    * An interface can have only abstract methods, no concrete methods allowed.
    * Interface methods are by default public and abstract—explicit declaration
    of these modifiers is optional.
    * Interfaces can have constants, which are always implicitly public,
    static, and final.
    * Interface constant declarations of public, static, and final are optional
    in any combination.
    * A legal nonabstract implementing class has the following properties:
    * It provides concrete implementations for the interface's methods.
    * It must follow all legal override rules for the methods it implements.
    * It must not declare any new checked exceptions for an
    implementation method.
    * It must not declare any checked exceptions that are broader than
    the exceptions declared in the interface method.
    * It may declare runtime exceptions on any interface method
    implementation regardless of the interface declaration.
    * It must maintain the exact signature (allowing for covariant returns)
    and return type of the methods it implements (but does not have to
    declare the exceptions of the interface).
    * A class implementing an interface can itself be abstract.
    * An abstract implementing class does not have to implement the interface
    methods (but the first concrete subclass must).
    * A class can extend only one class (no multiple inheritance), but it can
    implement many interfaces.
    * Interfaces can extend one or more other interfaces.
    * Interfaces cannot extend a class, or implement a class or interface.
    * When taking the exam, verify that interface and class declarations are legal
    before verifying other code logic.
    Last edited by turanan; 07-17-2009 at 11:42 AM.

  4. #4
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Rep Power


    Actually interfaces are loosely coupled one and classes are tightly coupled one.
    The question arise for you also raised for me when i was beginner of java.
    if you use class in your main class,while compiling ,the compiler need to verify the base class as well as main class.It may increase the complexity of your program.
    but if you use interface you dont need to worry about it
    (Living @ Virtual World)

  5. #5
    Join Date
    Jul 2009
    Rep Power


    interface is not a class, it's something describes one or more methods. that's why class DO NOT extends it but implements it.
    abstract class is an incomplete class, it must contains at least one abstract method. class extends it and NOT implements it.
    abstract method is a method with no 'body'.
    abstract void abstractMethod(int _in);

  6. #6
    QkrspCmptPop is offline Member
    Join Date
    Aug 2009
    Rep Power


    The fundamental idea of abstract/final is how you want the subclass use your class. If you want your class to be extended and your method to be overriden, declare it as abstract.

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Rep Power


    Quote Originally Posted by QkrspCmptPop View Post
    The fundamental idea of abstract/final is how you want the subclass use your class. If you want your class to be extended and your method to be overriden, declare it as abstract.
    Declaring it abstract merely says "a concrete class has to provide the body of this method"...for a class it merely says "I may have abstract methods".

    If you want your class to be extendible simply don't declare it final. If you want your method to be overrideable, similarly don't declare it final. Neither of these concepts has anything to do with abstract, really.

    ETA: Just to clarify. Abstract for a class also means "you must have a concrete implementation of me. I cannot be instantiated."

Similar Threads

  1. difference between Abstract and interface
    By mrark in forum New To Java
    Replies: 3
    Last Post: 11-07-2009, 05:20 AM
  2. Replies: 6
    Last Post: 02-11-2009, 11:51 AM
  3. Interface and Abstract Class
    By kian_hong2000 in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 03:22 PM
  4. Interface Vs Abstract Class
    By javarishi in forum New To Java
    Replies: 5
    Last Post: 06-15-2008, 06:43 AM
  5. Replies: 0
    Last Post: 04-02-2008, 08:04 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