as far as i can tell, both define methods with no bodies. only interfaces can declare variables. only interfaces can be instansiated. what else? i don't understand the point.
Rex Rock
Ranch Hand
Joined: Jun 25, 2001
Posts: 82
posted
0
1. Abstract class CAN contain methods with method bodies. Interfaces CANNOT. 2. you CANNOT extend from more than one class... but you can implement more than one interface..(kinda multiple inheritance).
Kaspar Dahlqvist
Ranch Hand
Joined: Jun 18, 2001
Posts: 128
posted
0
Howdy! What about variables? This is true: Variables in interfaces are implicitly public final static. I THINK this is true: Variables in abstract classes have no such restrictions and can have any access modifier etc... Correct me! /Kaspar
/Kaspar
Scott Appleton
Ranch Hand
Joined: May 07, 2001
Posts: 195
posted
0
An abstract class can declare variables, too. It can also provide implementations of some methods; it is abstract only because at least one method is declared but not implemented. Interfaces do not provide implementations of any methods. Therefore it is necessary for an implementing class to define all methods in an interface (even if such implementations are merely stubs). A class extending an abstract class, on the other hand, need only implement the abstract methods of its superclass. Implementing an interface allows a class to extend any other class. Conversely, a class which extends an abstract class cannot extend any other class. An interface is more "general" in a sense because all implementing classes must separately define the functionality of all methods, which means that 2 different classes implementing the same interface may be very different in many ways. Classes that extend abstract classes, on the other hand, are usually quite similar and usually differ only in the specific ways in which they implement a very limited number of methods.
Rex Rock
Ranch Hand
Joined: Jun 25, 2001
Posts: 82
posted
0
More..... When you implement an interface..the method should have 'public' access in the class you implement even if the method signature in the interface is not mentioned as public, the reason being all methods in an interface are considered to be public by default and when u implement a interface you can't lower/reduce its access modifier
INDU, BALA
Ranch Hand
Joined: Jun 07, 2001
Posts: 48
posted
0
Hi Donny, I have noted some points on interfaces when I referred Java2 The compete reference and the certification study guide. hope this is useful . INTERFACES: 1. Interfaces are similar to classes ,but they lack instance variables , and their methods are declared without Body. 2. The access modifier is public or not used 3. Variables can be declared inside interface declarations.They are implicitly final and static ( ie they can not be changed by the implementing class) IMPLEMENTING INTERFACES 1. One or more classes can implement that interface by including implements clause in its class definition 2. Access modifier is either public or not used 3. If a class implements more than one interface ,then interfaces are separated by comma 4. If a class implements more than one interface that declare the same method then the same method will be Used by clients of either interface 5. The method that implements that interface must be declared public.Also the type signature of the implementing method must match exactly the type signature specified in the interface definition.
ACCESSING IMPLEMENTATIONS THROUGH INTERFACE REFERENCE 1 Variables can be declared as object references that use an interface rather than a class type For eg interface name variablename = new class name which implements that interface () 2 Any instance of any class that implements that declared interface can be stored in such a variable 3 When the method is called thru one of these referencess , the correct version will be called based on the actual instance of the interface being referred to. 4 An interface variable only has the knowledge of the methods declared by its inter face declaration
PARTIAL IMPLEMENTATION If a class includes an interface but does not fully implement the methods defined by that interface then that class must be declared abstract. VARIABLES IN INTERFACES Variables can be declared in interface and they can be initialised with the desired value. By implementing the interface that variables can be used as constants
INTERFACE CAN BE EXTENDED One interface can extend another interface by using the keyword extends When a class implements an interface which inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain.
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
Originally posted by Scott Appleton: An abstract class can declare variables, too. It can also provide implementations of some methods; it is abstract only because at least one method is declared but not implemented.
This isn't quite true. An abstract class does not have to have any abstract methods.
------------------ Co-Moderator of the Programmer Certification Forums
Indu, It means that the following is a legal class declaration:
Notice that while Foo is declared as abstract, it does not contain any abstract methods. The only thing that abstract does in this case is to prevent instantiating a new Foo. The JLS 8.1.1.1 specifically discourages use of abstract for this purpose alone and suggests using a private constructor instead. See http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#34944
[This message has been edited by JUNILU LACAR (edited July 13, 2001).]
Hi, You can declare a class as absract class, it does not have to have an abstract method. It can't be instantiated. public abstract class A{ void aMethod(){ } } But if the class has any abstract method it has to be declared abstract. Vanitha.
INDU, BALA
Ranch Hand
Joined: Jun 07, 2001
Posts: 48
posted
0
Thankyou all. I got it.
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
From the JLS:
A class type should be declared abstract only if the intent is that subclasses can be created to complete the implementation. If the intent is simply to prevent instantiation of a class, the proper way to express this is to declare a constructor (�8.8.8) of no arguments, make it private, never invoke it, and declare no other constructors.
There is another reason. It may be that each method has a default implementation but that no class that extends the abstract class would leave all the methods as their default. A class like MouseAdapter could have been made abstract because no one would ever instantiate a MouseAdapter because it doesn't do anything (all the methods are empty). In fact, it is abstract although it has no abstract methods!!!
srinivas bolloju
Ranch Hand
Joined: Jan 23, 2001
Posts: 112
posted
0
hi indu, 1. if a class is declared as abstract ,it is not required that methods of that class are to be declared abstract, 2. but if a class contains an abstract method ,the class must be declared abstract. thomas is trying to explain my point 1 above.
please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details
INDU, BALA
Ranch Hand
Joined: Jun 07, 2001
Posts: 48
posted
0
Thankyou Thomas & Srinivas for your further clarification on the subject discussed above.