• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

interface

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dear all
I got confused about interface implemantation theory.
my doubt is suppose a
interface crazy{
doit();
eatit();
walk();
}
is there now i will create one class that is..
class just implements crazy{
doit(){System.out.println("doit");}
eatit(){System.out.println("eatit");}
walk(){System.out.println("walk");}
}
so implemented the crazy interface.if i can also define the class without implementing the crazy as foll.
class just{
doit(){System.out.println("doit");}
eatit(){System.out.println("eatit");}
walk(){System.out.println("walk");}

}
if both definations are true what is the use of implementing the interface?I can simply write the required methods with full implemented code without using " implements "word
so anna,ajit and any one please clarify me where is actual use of interface implementation?
---thanks
--sivaram
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interface gives you the ability of Multi-Inheritance.
Suppose you have a class that has been a subclass of the other.
you need add some additional function to it.
But Java doen't allow Multi-Inheritance,so you can define a interface that includes the methods you want.Then you can make you class implement that interface.Also,you can implement as many interfaces as you wish.
Hope this helps to you.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Using Interface you can force what a class MUST do and not how it implements.
2. Interfaces are designed to support dynamic run time method resolution. Means Interface disconnects the definition of methods from the class hierarchy. Interfaces are totally different hierarchy from classes.
Hope this helps,
Anna S Iyer.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
U know that java only suports single inharitance but by using interfaces you have the ability of Multi-Inheritance.
Once defined any number of classes can implement it and one class can implement as many no. of interfaces
hope it helps
Rahul
 
psr krishna
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
Still iam not convinced why because?
the real use of inheritance is when your really inheriting one or more defined properties from parent class or interface.But in case of interface the property(method) is (are) completely not implemented,So instead of inheriting that un implemnted methods from interface i can easily give complete implementation for those methods.We can take the case of EVENT LISTENER INTERFACES
For example I can simply write addActionPerformed() and etc., methods with complet implementation as per my need without inhereting from ActionListener interface then it will work.
So now my point is better clear now if iam wrong please pardon .
--thanks
sivarm
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The notion that interfaces provide multiple inheritance is misleading. This is because one would normally relate inheritance with implementation inheritance. Actually, the inheritance can itself be divided into two categories.
1. Implementation inheritance or fuctionality inheritance.
(using 'extends' in java)
2. Rules or specification inheritance.
(using 'implements' in java)
In the first case, you are interested in some useful functionality implemented by the base class. This is not possible by implementing an interface.
What you really inherit from implementing an interface is a specification or a set of rules. You have to implement the methods inherited from the interface.
The uses of interface inheritance are many some of which are given in replies above. Any object of a class which implements an interface 'I' can be treated as type of 'I'. This is useful in collections. In a typical project, your project lead may specify a mandatory interface which all classes have to implement thus ensuring that all classes have those methods which the project lead had agreed with the client. These could be like getVersion or methods which are client standard etc. There is no chance of a developer missing to provde these methods once the class implements an interface.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi krishna,

class Me {
public void jump() {
// Codes and more codes
}
}
class You {
public void jump() {
// Codes and more codes
}
class They {
public void jump() {
// Codes and more codes
}
}


Ok, now I want to notify all the instances of the above classes
when some interesting events happens
in some other object (call it "X"). When "X" jumps, objects of class
"Me", "You", "They" needs to notifed, that is i want 2 call the
jump() method in those objects.
[ Later u may want to create few more classes, that needs to be notified. ]
How do i go about the design ? How do the class "X" calls the
jump() method of these objects ?. [ assuming an instance of class "X" has
reference to the above instances, in a Collection (some how i managed it !! ) ]

So i need to say

" class You extends Me " ??? // Assuming am not a human !!

and

" class They extends You " ?? // They r humans !! maybe ..

NOOOO.

I don't inherit from some meaningless class just to reuse some codes.

But the above classes have something in common, they are interested to
know when "X" jumps, thats all in common.

So wat do i do ??

I will take that "commonality" in those classes and make an "interface" !!.

interface JumpingListener {
public void jump();
}

and i modify the classes to say that they are jumping listeners!!.

class Me implements JumpingListener {
public void jump() {
// implementing the method in my own way.
}
}

class You implements JumpingListener {
public void jump() {
// implementing the method in Your own way.
}

class They implements JumpingListener {
public void jump() {
// implementing the method the way They like.
}
}
Now all these classes have something in common, they r jumping listeners.

Now some skeleton of class X
class X {
Collection l;
public void addJumpingListener(JumpingListener jl) {
// add to to collection
}

// X jumps here ( the event )

// listeners gets notifed here [ the listeners are taken from the collection
// and their jump() method is called.

}
* X need not know the exact "class" of the object, ("Me", "You".. and some classes
not yet invented)all it want to know is the type of the object, in this case
these objects will be of type "JumpingListener". So "X" deals with the
JumpingListener types.
* In future you can create many more objects of type "JumpingListener",
class "X" need not know about them.

( these r just the few things abt the interfaces, and u will really appreciate the interfaces when u c the Java APIs)
So r "interface" good enough ??? Convinced ?

OK atleast u got bored !.
---------
Jon Aryan
---------

Originally posted by psr krishna:
hi all
Still iam not convinced why because?
the real use of inheritance is when your really inheriting one or more defined properties from parent class or interface.But in case of interface the property(method) is (are) completely not implemented,So instead of inheriting that un implemnted methods from interface i can easily give complete implementation for those methods.We can take the case of EVENT LISTENER INTERFACES
For example I can simply write addActionPerformed() and etc., methods with complet implementation as per my need without inhereting from ActionListener interface then it will work.
So now my point is better clear now if iam wrong please pardon .
--thanks
sivarm



[This message has been edited by Jon Aryan (edited October 11, 2000).]
 
psr krishna
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi arpitha and aryan,
many many thanks for your good examples. Now i got the point with clear theory. So once again let me repeat what i understand from your examples.If any misunderstand please correct me! OK!
1. WITH RESPECT TO A SINGLE CLASS PROGRAM THE IMPLEMENTATION OF AN INTERFACE IS NOT A GOOD USE

2. WITH RESPECT TO MULTI CLASS PROGRAM AND PACKAGE LEVEL PROGRAMS THE IMPLEMENTATION OF AN INTERFACE IS VERY GOOD USE BECAUSE OF SOME EVENT NOTIFICATIONS AND SHARING OF SOME COMMANALITY AMONG THE CLASSES WHICH ARE IMPLEMENTING THE CORRESPONDING INTERFACE.
Is it right?
---shivaram
 
Why is the word "abbreviation" so long? And this ad is so short?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic