Ok, if you have:
public interface Interface{
public void method1();
public int   method2();
}


class Class2 implements Interface{
int field1;
String field2;
public void method1(){
System.out.println(this.field2);}

public int method2(){
return field1*5;}
}

class Class2 {// doesn't extend Class1 and doesn't have to implement the
methods of Interface
double field1;
Interface field2=new Class1; // field2 is a field of type Interface which is
implemented in Class1
/** now the interface can be used as a type where ever it was and it will
give access to the methods and fields of the class which implements it to
the class which is using it as a type. you may say that we can do this by
using Class1 as a type instead of Interface, yes but not if Class1 is not in
the same package  of Class2 or it is defined private, here the interface
make the inheritance available between two differently located and non
related classes. see so when i write Class1 and i don't want you to take a
look at it, i define it private, and when i want you to get benefit of some
of its methods, i right an interface and make your Class2 implements it.*/
// so the following is valid
field2.method1();// a list of Class1 methods will appear while you can't see
there body
field2.method2();// which gives you access to field1 of Class1 which is
private.


i wish that helps.

On Fri, Sep 4, 2009 at 9:55 AM, vinay basavanal <[email protected]> wrote:

> hi
>   thanks for reply i was busy so could not reply
>   i did not get this point
> "  so when you make claas1 implements an interface that is implemented in
> class2, you will have access threw class1 to class2 even without needing to
> see or know the interface methods implementations in class 2, right?? "
>
> can you give me example  of code
>
> On Wed, Sep 2, 2009 at 12:39 PM, Abbas Zaini <[email protected]>wrote:
>
>> i'll tell you , when you write an interface, you must write a class that
>> implements all the methods of the interface, right??
>> now this class is going to have fields and methods that are not
>> implemented in other classes, right??
>> so when you make claas1 implements an interface that is implemented in
>> class2, you will have access threw class1 to class2 even without needing to
>> see or know the interface methods implementations in class 2, right?? the
>> abstract class doesn't provide these features because you can't implement an
>> abstract class, the interface may join classes which lay in different
>> packages and don't share any of there info ( fields and methods) i think
>> that is also right, if you have any thing in addition to discuss about it
>> will be my pleasure to serve.
>> yours.
>> On Wed, Sep 2, 2009 at 8:55 AM, vinay basavanal <[email protected]>wrote:
>>
>>> i understand the concept of interface but my question is ,is it really
>>> necessary interface was introduced in order to avoid problems of multiple
>>> inheritance that is  a single class inheriting from two classes but
>>>
>>> only reason why  we go for inheritance is to get advantages of already
>>> implemented methods but with interface we don't get that advantages so why
>>> interface
>>>
>>> >>>
>>>
>>
>>
>> --
>> Abbas Zaini
>>
>
>


-- 
Abbas Zaini

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to