Results 1 to 7 of 7
- 07-15-2009, 08:59 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 1
- Rep Power
- 0
Questions about interface and abstract class
Hi,
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?
Thanks
- 07-16-2009, 01:05 AM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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.
- 07-17-2009, 10:34 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 19
- Rep Power
- 0
Quote form the guide to SCJP exam:
Interfaces
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 10:42 AM.
- 07-19-2009, 01:59 PM #4
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 itMak
(Living @ Virtual World)
- 07-23-2009, 08:50 AM #5
Member
- Join Date
- Jul 2009
- Posts
- 10
- Rep Power
- 0
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);
- 08-24-2009, 07:53 PM #6
Member
- Join Date
- Aug 2009
- Posts
- 9
- Rep Power
- 0
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.
- 08-25-2009, 01:05 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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
-
difference between Abstract and interface
By mrark in forum New To JavaReplies: 3Last Post: 11-07-2009, 04:20 AM -
Difference between Abstract class having only abstract method and a Interface class
By Santoshbk in forum New To JavaReplies: 6Last Post: 02-11-2009, 10:51 AM -
Interface and Abstract Class
By kian_hong2000 in forum New To JavaReplies: 1Last Post: 08-27-2008, 02:22 PM -
Interface Vs Abstract Class
By javarishi in forum New To JavaReplies: 5Last Post: 06-15-2008, 05:43 AM -
what is the Priority for execution of Interface class and a Abstract class
By Santoshbk in forum Advanced JavaReplies: 0Last Post: 04-02-2008, 07:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks