Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

4 Questions on Java interface class

Thread view: 
Sem - 14 Mar 2007 05:35 GMT
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
Alan Krueger - 14 Mar 2007 05:41 GMT
> Hello Please help
>
[quoted text clipped - 4 lines]
> 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
Sem - 14 Mar 2007 05:52 GMT
> > Hello Please help
>
[quoted text clipped - 10 lines]
>
> http://java.sun.com/docs/books/jls/third_edition/html/interfaces.htmlhttp://java
.sun.com/docs/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??
> >Q 1)  Is it possible for an interface class to have no method?
             my answer is NO

> > Q 2) Is it possible for an interface class to declare variables only no method?
             my answer is NO
> > Q 3) What is the main difference between interface and abstract class?
           If you extends more than one interface and did not
implement some methtod then for
           these classes you have to name them abstract
> > Q 4) Is it possible to have only abstract class without interface
          Based on the above definition my answer is NO

Am I right? or wrong?

-sem
> > class?
Lew - 14 Mar 2007 06:12 GMT
> 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??
>>> Q 1)  Is it possible for an interface class to have no method?
>               my answer is NO

The correct answer is yes.

>>> Q 2) Is it possible for an interface class to declare variables only no method?
>               my answer is NO

Interfaces may not have instance variables at all.

>>> Q 3) What is the main difference between interface and abstract class?
>             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.

>>> Q 4) Is it possible to have only abstract class without interface
>            Based on the above definition my answer is NO

The correct answer is yes.

-- Lew
Lew - 14 Mar 2007 06:21 GMT
> 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??
>>> Q 1)  Is it possible for an interface class to have no method?
>               my answer is NO

The correct answer is yes.

>>> Q 2) Is it possible for an interface class to declare variables only no
method?
>               my answer is NO

The correct answer is yes.

>>> Q 3) What is the main difference between interface and abstract class?
>             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.

>>> Q 4) Is it possible to have only abstract class without interface
>            Based on the above definition my answer is NO

The correct answer is yes.

-- Lew
Lew - 14 Mar 2007 05:54 GMT
Sem wrote:
> Hello Please help

> Q 2) Is it possible for an interface class to declare variables only
> no method?
[quoted text clipped - 4 lines]
>    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
Lew - 14 Mar 2007 05:51 GMT
> Hello Please help
>
[quoted text clipped - 4 lines]
> public interface One{
> }

Yes.

> Q 2) Is it possible for an interface class to declare variables only
> no method?
[quoted text clipped - 4 lines]
>    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
Alan Cui - 14 Mar 2007 06:09 GMT
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
>
[quoted text clipped - 11 lines]
>
> }

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.

> Thank you
>
> -sem
vishist - 14 Mar 2007 19:53 GMT
> Hi, Sem

> On Mar 14, 12:35 pm, "Sem" <semj2e...@gmail.com> wrote:> Hello Please help
>
[quoted text clipped - 43 lines]
>
> > -sem

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.
Lew - 15 Mar 2007 02:51 GMT
>      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
Alex Hunsley - 15 Mar 2007 02:01 GMT
> Hello Please help
>
[quoted text clipped - 4 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.