interface trouble


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: interface trouble

  1. #1
    Join Date
    Mar 2004
    Posts
    1

    Question interface trouble

    hi,
    i have this basic problem with interfaces. i have been stuck for a while. i Have two interfaces A and B declaring the same function prototype get(). a class C implements these two interfaces and defines get(). Now in the main function when i call a.get() (a is an object of A) or b.get() (where b is an object of B)...is there any way to find out which interface called the function?..chk out the code below to understand my problem better.
    interface A {
    void get();
    }
    interface B {
    void get();
    }

    class C implements A,B {
    public void get(){
    System.out.println("Hello");
    }
    }
    public class inter {
    public static void main(String[] args) {
    C c = new C();
    A a;
    B b;
    a = c;
    a.get();//At this point
    b = c;
    b.get();//And at this point
    }
    }

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    it doesnt matter

    an interface doesnt contain any code.. an interface is merely a set of guidelines that ensures compatibility between objects

    if an interface states "a class must supply a method called X taking parameters Y and returning value Z to conform with this interface"

    then thats it.. your class C conforms with both interfaces.. it can be asserted to be of type B or of type A as well as being of type C..

    Interfaces are merely a "requirement to implement these methods" - they dont actually implement methods, so there is no code to call, so whether you make a class of type C and call it A, or call it B, it is always the code in C that will be run. interfaces cant contain code

    interfaces are used where some fonformity is required.. to be stored ina HashTable an object MUST provide at least an equals(Object other) and a hashCode() method
    To successfully implement the hashable (not hash_T_able) interface, an object will provide these methods, and can then be claimed to be of type Hashable, because no matter what other methods it has, it conforms to the Hashable requirements
    HashTables store Hashables, so if an object doesnt conform, it cant be stored

    its a bit like saying "I have a requirement that a car is manual transmission, with the drivers wheel on the right hand side"
    A mercedes that is UK-spec manual.. i can drive it. A Mitsubishi that is UK spec but automatic, i cant drive it. An american car that is left-hand drive manual trans, i cant drive it.. they dont conform to spec, even though they are all cars.

    public class UKCar implements ManualTransmission, RightHandDrive

    now, it doesnt matter whether its a merc, beemer, ford, so long as it conforms.. i can drive it

    public class UKCar implements ManualTransmission, ChangeGearsManually, RightHandDrive, SteeringWheelOnTheRight

    there's repeated interfaces in there.. it doesnt matter.. i dont sit in the car and think "im driving a RightHandDrive car.." and 5 minutes later "im driving a car with the SteeringWheelOnTheRight" - because neither is of operational consequence.
    similarly your get methods in the interfaces are of no operational consequence, only an appearance or a presentational one - so there isnt "one that is performed" at time X, and "another that is performed" at time X+n

    *hopes that makes sense*
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Feb 2004
    Posts
    808
    note; you use "function prototype" instead of "method signature" .. are you by chance a C programmer?

    if you are.. an interface is NOT the equivalent of a .h header file.. interfaces dont "declare methods" only stipulate "required method signatures" - if you dont write a method that conforms to the signature, the class doesnt compile, its as simple as that.. but any code you do write, is part of the class, and the interface hasnt got anything to do with it beyond ensuring that that method exists
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center