dummyagain
May 9th, 2004, 05:58 AM
I would like to ask when we should use an interface, and the actual functions of the interface in Java?
|
Click to See Complete Forum and Search --> : interface dummyagain May 9th, 2004, 05:58 AM I would like to ask when we should use an interface, and the actual functions of the interface in Java? mikeBarr81 May 9th, 2004, 08:36 AM interfaces are used in java to specify certain method signatures that a class must have when it implements the interface. They are used as a way to simulate multiple inheritance in java because literal multiple inheritance isn't alowed. A class can only extend one super class. Interfaces should be used when there is certain behaviour you want a number of different classes to have but which aren't in the same class hierarchy. A good example of interfaces are the various listener interfaces. They define abstract methods that must all be overided if you implement the interface, and allow whichever class implements them to be considered as that listener type. dlorde May 9th, 2004, 02:08 PM interfaces are used in java to specify certain method signatures that a class must have when it implements the interfaceA somewhat circular definition, but it carries the gist... Whenever you have a number of classes that are variations of a particular type, i.e. 'polymorphic' classes (e.g. car makes, animal species, job types, display components), they will typically be subclasses of that base type. Good OO practice recommends making that type an interface that specifies the methods that define the behaviour of that type. This allows all the code that deals with this type (i.e. uses these classes) to refer to them all via the interface, which means simple, clear code with no duplication and the flexibility to add new types to the application without changing the core code. Even where there is common code used by all the subclasses that can be moved up ('abstracted') into a superclass (an abstract superclass), that superclass itself should implement the interace. It is worth providing an interface even when you may only have one example of such a type in your application and there is just the possibility or likelihood of introducing others later. Using an interface clarifies and simplifies code. The key to performance is elegance, not batallions of special cases... J. Bently & D. McIlroy cjard May 10th, 2004, 05:36 AM Originally posted by dummyagain I would like to ask when we should use an interface, and the actual functions of the interface in Java? Sometimes, you have to PROVE to the compiler, that a class you have written, is capable of doing certain things. Like, if you want to PROVE that your "WizzBang" object is Comparable to another WizzBang object (so that it can be stored in a sorted container) then it must implement the Comparable interface. In writing the "implements Comparable" words into your java file, the compiler will then go and look to check that your class does indeed, have all the methods that the interface specifies.. So if Comparable mandates that you provide methods of these exact signatures: public int compareTo(Comparable other) public boolean equals(Comparable other) then you must put those exact method signatures into the class, then write sensible code to make the results sensible. - You would write your own interface, if you had invented some supercool WizzBang class, that could only work on WobbleBong compatible objects.. You could then say "programmers wishing to use this to operate on their classes, must implement my WobbleBong interface" WobbleBong isnt exactly a TYPE of object, cos if I wrote: public class HulaHoop implements WobbleBong the TYPE is actually HulaHoop.. but if i implement the interface correctly, it proves that the HulaHoop class can be used to function like a WobbleBong.. and hence it is compatible with any objects that require a WobbleBong a real world example may be: public class Person implements Driver and then we have a Car object with a method: public void setMainDriver(Driver d) we wouldnt want to jsut stick a Person in there, because it might be a 13 year old that cant drive.. so the method demands that a Driver be put in there.. We could program a robot to be able to drive a car too. in this case Robot implements Driver.. both entities, Robot and Person, are PROVEd to be capable of operating a Car object, because they are capable of acting as a Driver.. yet you note that they dont have to be an inherited child of a Driver object. Interfaces in java, get round a lot of the issues presented by a lack of multiple inheritance (no MI = a good thing) because an object can implement multiple interfaces, that PROVE they can do a particular job: public class Person implements Driver, Teacher, Swimmer, Golfer ... amusingly.. you may then get a situation where method names conflict.. in this case, the parameters should be chosen to separate them: Driver interface may have a "void drive(Car c)" method to allow anything that implements it, the ability to drive a car and Golfer may also have a drive method.. "void drive(GolfBall g)" in this case, java will distinct them based on the parameter codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved. |