A class without method

in java, Can a class be without any methods?we can have interface without methods...is that possible for a class?
[127 byte] By [Caliguardoa] at [2007-10-2 20:16:48]
# 1
If a class doesnt have any methods, aka behavior, what good would it be?At a bare minimum, your empty class will inherit from the Object class, thus inheriting some methods. See the API for the Object class for the specifics.JJ
Java_Jaya at 2007-7-13 22:59:12 > top of Java-index,Java Essentials,New To Java...
# 2
Then what s the use of an interface without methods?
Caliguardoa at 2007-7-13 22:59:12 > top of Java-index,Java Essentials,New To Java...
# 3

> Then what s the use of an interface without

> methods?

Interfaces are never instantiated. They are used to specify guaranteed behavior by the class that implements the interface.

When you write a class that implements an interface, in essence, your promising to the compiler that your class behaves like the interface. Further you also promise to the compiler you will define all the methods in the interface, thus guaranteeing behavior, and fulfilling the contract.

Interfaces have method declarations but no method bodies. When you implement a interface with your class, you are saying that your class will behave like the interface. It is up to you to implement the method bodies for the method declarations in the interface. Furthermore, you must implement all the methods specified in the interface, you cannot leave any out.

Hope that sheds some light on the subject..

JJ

Java_Jaya at 2007-7-13 22:59:12 > top of Java-index,Java Essentials,New To Java...
# 4

> Then what s the use of an interface without

> methods?

They are often called marker interfaces, and you usually use them to "tag" classes for different purposes. You can e.g. let a class implement the marker interface Serializable to allow the class to be serialized.

Kaj

kajbja at 2007-7-13 22:59:12 > top of Java-index,Java Essentials,New To Java...
# 5
> At a bare minimum, your empty class will inherit from the Object class, > thus inheriting some methods. If you do not declare any constructor, there will be a no-arg one generated.
BIJ001a at 2007-7-13 22:59:12 > top of Java-index,Java Essentials,New To Java...