4 Questions on Java interface class

S

Sem

Hello Please help

Q 1) Is it possible for an interface class to have no method?

Example

public interface One{
}

Q 2) Is it possible for an interface class to declare variables only
no method?

Example

public interface Two {
Final int books = 50;
}

Q 3) What is the main difference between interface and abstract class?

Q 4) Is it possible to have only abstract class without interface
class?


Thank you

-sem
 
A

Alan Krueger

Sem said:
Hello Please help

Q 1) Is it possible for an interface class to have no method?
Q 2) Is it possible for an interface class to declare variables only
no method?
Q 3) What is the main difference between interface and abstract class?
Q 4) Is it possible to have only abstract class without interface
class?

This sounds remarkably like a homework assignment.

Here's some documentation you might want to consult:

http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.1.1
 
L

Lew

Sem said:
Hello Please help

Q 1) Is it possible for an interface class to have no method?

Example

public interface One{
}
Yes.

Q 2) Is it possible for an interface class to declare variables only
no method?

Example

public interface Two {
Final int books = 50;
}

No instance variables allowed in an interface.

"Final" is not a keyword and will cause you grief used that way.
Q 3) What is the main difference between interface and abstract class?

An interface is a contract: it has no implementation code. Method bodies are
forbidden.

An abstract class, like any kind of class, is an implementation: it can (and
usually does) have implementation code (method bodies).
Q 4) Is it possible to have only abstract class without interface
class?

Yes.

A true devoté if Inversion of Control (IOC, see the Swing project) might think
of such a class as ronin.

-- Lew
 
S

Sem

This sounds remarkably like a homework assignment.

Here's some documentation you might want to consult:

http://java.sun.com/docs/books/jls/...s/books/jls/third_edition/html/classes.html#8....

Thank you.

But it is not a homework question. I did my own thought and want to
know others opinion.
Here are myown respond are they right?? my answer is NO
If you extends more than one interface and did not
implement some methtod then for
these classes you have to name them abstract Based on the above definition my answer is NO

Am I right? or wrong?

-sem
 
L

Lew

Sem said:
> Hello Please help
> Q 2) Is it possible for an interface class to declare variables only
> no method?
>
> Example
>
> public interface Two {
> Final int books = 50;
> }

"Final" is not a keyword and will cause you grief used in a declaration that
way, even in places where such a declaration would otherwise be permissible.
> Q 4) Is it possible to have only abstract class without interface
> class?

A true devoté if Inversion of Control ("IOC", see the Swing project) might
think of such a class as ronin.

-- Lew
 
A

Alan Cui

Hi, Sem

Hello Please help

Q 1) Is it possible for an interface class to have no method?
Yes, you can declare a interface without any method.

Example

public interface One{

}

Q 2) Is it possible for an interface class to declare variables only
no method?

Example

public interface Two {
Final int books = 50;

}

Indeed, It works.
Q 3) What is the main difference between interface and abstract class?

All Java books will tell you the difference, some key points:

1. Apparently, a class can implement several interfaces, while it is
not permitted to derive several abstract classes.
2. Usually, interface stands for some kind of attributes, such as
"Serializable", "Accessible". So we could say the object has such
attribute or ability
But abstract class indicates the essential property of the class, such
as "Vehicle", "Computer". So we usually say the object belongs
to ..........
3. Abstract classes can have their concrete methods, but the methods
declared in a interface are all abstract.

Q 4) Is it possible to have only abstract class without interface
class?
I don't known what exactly the question is.
 
L

Lew

Sem said:
But it is not a homework question. I did my own thought and want to
know others opinion.
Here are myown respond are they right??
my answer is NO

The correct answer is yes.
my answer is NO

Interfaces may not have instance variables at all.
If you extends more than one interface and did not
implement some methtod then for
these classes you have to name them abstract

Not correct. If you /implement/ an interface and don't want to implement all
its methods, the class must be abstract.

But that doesn't explain the difference between interfaces and abstract classes.

The difference between an abstract class and an interface is the difference
between any class and an interface.

An interface is a contract; it defines a type to which implementing classes
must conform, but has no implementation. Method bodies are forbidden. Instance
variables are forbidden.

An abstract class, like any class, is an implementation, possibly of an
interface's contract. It may, and usually does, contain method bodies.
Based on the above definition my answer is NO

The correct answer is yes.

-- Lew
 
L

Lew

Sem wrote:
Sem said:
>
> But it is not a homework question. I did my own thought and want to
> know others opinion.
> Here are myown respond are they right??
> my answer is NO

The correct answer is yes.
method?
> my answer is NO

The correct answer is yes.
> If you extends more than one interface and did not
> implement some methtod then for
> these classes you have to name them abstract

Not correct. If you /implement/ an interface and don't want to implement all
its methods, the class must be abstract.

But that doesn't explain the difference between interfaces and abstract classes.

The difference between an abstract class and an interface is the difference
between any class and an interface.

An interface is a contract; it defines a type to which implementing classes
must conform, but has no implementation. Method bodies are forbidden. Instance
variables are forbidden.

An abstract class, like any class, is an implementation, possibly of an
interface's contract. It may, and usually does, contain method bodies.
> Based on the above definition my answer is NO

The correct answer is yes.

-- Lew
 
V

vishist

Yes, you can declare a interface without any method.




Indeed, It works.




All Java books will tell you the difference, some key points:

1. Apparently, a class can implement several interfaces, while it is
not permitted to derive several abstract classes.
2. Usually, interface stands for some kind of attributes, such as
"Serializable", "Accessible". So we could say the object has such
attribute or ability
But abstract class indicates the essential property of the class, such
as "Vehicle", "Computer". So we usually say the object belongs
to ..........
3. Abstract classes can have their concrete methods, but the methods
declared in a interface are all abstract.


I don't known what exactly the question is.

For interfaces, Variable values declared will be "final" by default.
private/protected parameters are not allowed. But (public, package
scope) static final variables can be defined.

public interface Two {
final int books = 50;
}
== public interface Two {
int books = 50;
}

Q 1) Is it possible for an interface class to have no method?
The correct answer is yes: please refer to java.io.Serializable,
Cloneable interface which are marker interfaces
Q 2) Is it possible for an interface class to declare variables only
no method?
Yes: Sometimes, application wide configuration information can be
stored in interfaces and just implement the interface wherever the
information is needed.

Q 3) What is the main difference between interface and abstract
class?
Interfaces, as defined by everyone, is a contract that
implementing classes need to adhere to. As for difference between
Abstract Class/Interface, Abstract Class forms part of Object
Hierarchy (Super Class-sub class), Abstract class attributes can be
instance specific. Please refer to java.util.AbstractCollection,
java.util.AbstractList... for examples.

Q 4) Is it possible to have only abstract class without interface
Yes. You don't want to mix abstract class concepts with interfaces.
Interfaces are contract to implementing that they adhere to the method
signature, scoping, and exception handling.
 
A

Alex Hunsley

Sem said:
Hello Please help

Q 1) Is it possible for an interface class to have no method?

Example

public interface One{
}

I know this is a really wild idea but...
Did you consider
a) trying to run such code through a java compiler?
b) reading the Java API docs/language specification?

lex
 
L

Lew

vishist said:
Yes: Sometimes, application wide configuration information can be
stored in interfaces and just implement the interface wherever the
information is needed.

Joshua Bloch and others have gone into some detail excoriating the practice of
putting application constants in an interface. The better practice is to use a
class.

An interface should define a type. The use as a container for variables is not
consistent with that design philosophy.

The addition of static imports to the language was motivated in part by the
desire to stop programmers from using interfaces as constants bags.

-- Lew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top