Home arrow static arrow Java Programming [Archive] - Question regarding Inrefaces:Please GUIDE.
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Question regarding Inrefaces:Please GUIDE.
This topic has 17 replies on 2 pages.    1 | 2 | Next »

Posts:754
Registered: 6/13/03
Question regarding Inrefaces:Please GUIDE.  
Aug 6, 2004 8:39 AM



 
A question regarding INTERFACES.

'Each interface definition constitutes a new type.
As a result, a reference to any object instantiated from any class
that implements a given interface can be treated as the type of
the interface'.

So :

interface I{} class A implements I{ } 


Now,class A is of type I.Right?

Now,if class A implements more than one interface,then what
is the actual type of A?

For example:

interface I{}interface R{} class B implements I,R{ } 


What is now B's type? I or R? or both?
[/code]
 

Posts:6,750
Registered: 1/25/04
Re: Question regarding Inrefaces:Please GUIDE.  
Aug 6, 2004 8:40 AM (reply 1 of 17)



 
An instance of B is of type B, I, R, and Object.
 

Posts:668
Registered: 9/2/03
Re: Question regarding Inrefaces:Please GUIDE.  
Aug 6, 2004 8:59 AM (reply 2 of 17)



 
This kind of example cleared my mind from interfaces a while ago:
interface SomeInterface {  void doThis();}// ---interface AnotherInterface {  void doSomethingElse();}// ---class MyClass implements SomeInterface, AnotherInterface {  public void doThis() {  }  public void doSomethingElse() {  }}////////////////////////////////////class Testing {  public static void main(String[] ****) {    MyClass myClass = new MyClass();    if(myClass instanceof AnotherInterface) {        // class is type AnotherInterface    }    if(myClass instanceof SomeInterface) {        // class is type SomeInterface    }    if(myClass instanceof MyClass) {        // class is type AnotherInterface    }  }}

All of the if clauses are true.
[/code]
 
Guest
Re: Question regarding Inrefaces:Please GUIDE.  
Aug 6, 2004 9:38 AM (reply 3 of 17)



 
I think of interfaces as a 'view' of an implementation (class). A class that implements an interface does not have an "is a" relationship with the interface, even if the instanceof operator works.

An interface is a definition of (one type of) behavior of the class, and a class can exibit several different 'types of' behaviors, by implementing several interfaces.
 

Posts:37,103
Registered: 3/30/99
Re: Question regarding Inrefaces:Please GUIDE.  
Aug 6, 2004 9:58 AM (reply 4 of 17)



 
I think of interfaces as a 'view' of an implementation
(class). A class that implements an interface does not
have an "is a" relationship with the interface, even
if the instanceof operator works.

Actually, it does have the is-a relationship, IMAO. I don't view "X is-a Y" as completely defining X to be exactly a Y and nothing more. I view it as meaning that it meets all the requirements to be called a Y. If it also meets requirements to be called a Z, that doesn't make it less of a Y. AFAIK, this is consistent with the general use if "is-a" to describe class relationships.
 

Posts:6,750
Registered: 1/25/04
Re: Question regarding Inrefaces:Please GUIDE.  
Aug 6, 2004 9:59 AM (reply 5 of 17)



 
A class that implements an interface does not
have an "is a" relationship with the interface, even
if the instanceof operator works.

An instance of a class that implements ResultSet is not a ResultSet? An instance of a class that implements List is not a List?
 

Posts:24,036
Registered: 2/3/03
Re: Question regarding Inrefaces:Please GUIDE.  
Aug 6, 2004 10:13 AM (reply 6 of 17)



 
An instance of a class that implements ResultSet is
not a ResultSet? An instance of a class that
implements List is not a List?

Depends on who you talk to... :o) Folks have argued here that inheritance of interface is "less strong" than inheriting implementation, and that implements implies an "is-a-type-of", or even (questionably, IMAO) "has-a". I agree with nasch. For me, the whole interface thing boils down to polymorphism (and to a lesser extent, encapsulation); namely, that any implementation can be used where the interface is called for (i.e., "is-a").
 
Guest
Re: Question regarding Inrefaces:Please GUIDE.  
Aug 6, 2004 10:58 AM (reply 7 of 17)



 

The class (that implements the interface) actually defines the behavior, and the interface just serves as a contract for that behavior - a view.

IMHO, the 'types' are the classes, which qualify for the 'is a' relationship

(which is what the words "extends" and "implements" strongly suggest)
 

Posts:24,036
Registered: 2/3/03
Re: Question regarding Inrefaces:Please GUIDE.  
Aug 6, 2004 11:02 AM (reply 8 of 17)



 
IMHO, the 'types' are the classes, which qualify for
the 'is a' relationship

I don't see how this supports your previous assertion that implementations don't have an "is-a" relationship with the interface.

[url=http://java.sun.com/docs/books/tutorial/java/interpack/interfaceAsType.html]Using an Interface as a Type[/url]
 

Posts:24,036
Registered: 2/3/03
Re: Question regarding Inrefaces:Please GUIDE.  
Aug 6, 2004 11:04 AM (reply 9 of 17)



 
IMHO, the 'types' are the classes...

And since I'm quibbling with your use of "type", I offer this from the [url=http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html#238680]JLS[/url]:

An interface declaration introduces a new reference type whose members are classes, interfaces, constants and abstract methods. This type has no implementation, but otherwise unrelated classes can implement it by providing implementations for its abstract methods.
 

Posts:37,103
Registered: 3/30/99
Re: Question regarding Inrefaces:Please GUIDE.  
Aug 6, 2004 12:26 PM (reply 10 of 17)



 

The class (that implements the interface) actually
defines the behavior, and the interface just serves as
a contract for that behavior

Yes.

- a view.

Call it that if you want, but it being "a view" doesn't take away is-a-ness.

IMHO, the 'types' are the classes, which qualify for
the 'is a' relationship

As yawmark points out, your use of "type" is not consistent with the JLS. Regardless of how you want to define type, the face it that it makes sense to say "A LinkedList is a List" and "A String is (a) Comparable" etc. Additionally, the way I've always seen the is-a relationship described, and the way that makes the most sense to me, is that "A is-a B" means "A can be used where B is expected." In this respect, superclasses and implemented interfaces are no different.

(which is what the words "extends" and "implements"
strongly suggest)

"Foo extends Bar" in plain English doesn't suggest to me that Foo is a Bar, but quite clearly, in the context of Java's OO model, it means precisely that.

"Foo implements Bar" in plain English doesn't suggest much to me. Maybe that Foo provides the implementation specified in Bar, and therefore can be used where a Bar is required, which is exactly what implements means in Java and which, as far as I can tell, is the core of what the is-a relationship is supposed to be about in general OO.
 
Guest
Re: Question regarding Inrefaces:Please GUIDE.  
Aug 9, 2004 10:03 AM (reply 11 of 17)



 

In terms of design, I tend to think of an interface as a type independent entity, with specific behaviour contracts that the interface defines. For e.g. Comparable
(and also as types, used where multiple inheritence is desired)

A String is a type which is comparable. But using it as a comparable does not tell you its type - just the behaviour, which leads me to think that a String "is a" Comparable, is not right.

Coming back to data structures, a Collection is a good example of an interface, as simply being called a Collection does not make it a type. (data objects and structures are probably not the best examples of class-interface relationships)

*(type = nouns in the problem space)
 
Guest
Re: Question regarding Inrefaces:Please GUIDE.  
Aug 9, 2004 10:10 AM (reply 12 of 17)



 

"Foo extends Bar" in plain English doesn't suggest to
me that Foo is a Bar, but quite clearly, in the
context of Java's OO model, it means precisely that.

One of the definitions of extends - "To enlarge the area, scope, or range of."


"Foo implements Bar" in plain English doesn't suggest
much to me. Maybe that Foo provides the implementation
specified in Bar, and therefore can be used where a
Bar is required, which is exactly what implements
means in Java and which, as far as I can tell, is the
core of what the is-a relationship is supposed to be
about in general OO.

Implements - Suggests only behaviour, not the means
("To put into practical effect")
 

Posts:24,036
Registered: 2/3/03
Re: Question regarding Inrefaces:Please GUIDE.  
Aug 9, 2004 10:15 AM (reply 13 of 17)



 
as simply being called a Collection does not make it a type.

Your interpretation doesn't seem to jive with the language specification, which specifically refers to interfaces as "types".

"Type" and "Class" are not necessarily synonymous: http://www.javaworld.com/jw-01-2001/jw-0119-type.html

A String is a type which is comparable. But using it as a comparable does not tell you its type -
just the behaviour, which leads me to think that a String "is a" Comparable, is not right.

String is a class. Comparable is an interface. Both are "types". In common usage among Java programmers, implementing an interface imparts an "is-a" relationship.
 

Posts:37,103
Registered: 3/30/99
Re: Question regarding Inrefaces:Please GUIDE.  
Aug 9, 2004 10:18 AM (reply 14 of 17)



 
A String is a type which is comparable. But using it
as a comparable does not tell you its type - just the
behaviour, which leads me to think that a String "is
a" Comparable, is not right.

Why not? The fact that you can use it as a Comparable seems to me to be a very strong argument that it is a Comparable (in the OO sense of "is-a") It is also a String and it is an object. I repeat: "A is-a B" does not mean that B completely defines A. Rather it means that A meets the minimum requirements to call itself a B. String is a Comparable because it meets the requirements to be called a Comparable. The way the is-a relationship is used in OO to describe inheritance is consistent with this.

Also, in C++, if you had something that multiply inherits from several classes, would you say "is-a" is not an accurate description of any of those child/parent relationships?
 
This topic has 17 replies on 2 pages.    1 | 2 | Next »