#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    24
    Rep Power
    0

    Class implements with more than one interface...


    Hello,

    My main class(MYCLASS) in my program implements 2 interfaces. Lets call it interfaceA and interfaceB. interfaceA contains 3 methods(methodA1, methodA2, methodA3). And interfaceB contains 2 methods(methodB1, methodB2).

    After writing the code for my class, I try to run the program. I type something like:
    interfaceA obj = new MYCLASS();
    System.out.println(obj.methodA1)
    System.out.println(obj.methodA2)
    It works.

    but when I type:
    System.out.println(obj.methodB1) or anything method that are in interfaceB, then I got an error mesage.

    Therefore I decide to put:
    interfaceB obj2 = new MYCLASS();
    System.out.println(obj2.methodB1);

    Here, I STILL got an error message, but this time it's like it can't recognize any variables in MYCLASS.....


    HOWEVER, It works perfectly if I copy the methods in interfaceB and add it into interfaceA. And then when I type:
    InterfaceA obj = new MYCLASS();
    System.out.println(obj.methodB1());

    It works perfectly, and it can recognize those variables in MYCLASS now....But the problem is I can't copy those methods into interfaceA

    How do I fix this problem?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    11
    Rep Power
    0

    Look on the code


    Hi,

    May be u can post the code , so that we can take a look at how the interfaces are implemented and used.


    Sathya
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    24
    Rep Power
    0
    I can't post the code here, since it's too big...and it's an assignment.

    For your information, some method in interfaceA(methodA1....etc) is like "inserting" element in a list. And for interfaceB(methodB1...etc) it just play around with the elements inside the array.

    BUt the problem is...it returns an error message...more precisely it's a NullPointerException error message. I think it thinks the list is null or not create or something like that.
  6. #4
  7. AYBABTU
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Here or There
    Posts
    1,256
    Rep Power
    380
    Originally Posted by victorti83plus
    I can't post the code here, since it's too big...and it's an assignment.
    If you want help, you should post SOME code and the exceptions, including stacktrace.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    11
    Rep Power
    0
    Originally Posted by victorti83plus
    I can't post the code here, since it's too big...and it's an assignment.

    For your information, some method in interfaceA(methodA1....etc) is like "inserting" element in a list. And for interfaceB(methodB1...etc) it just play around with the elements inside the array.

    BUt the problem is...it returns an error message...more precisely it's a NullPointerException error message. I think it thinks the list is null or not create or something like that.
    Hi,

    You can put the parts of the code where InterfaceB is involved.
  10. #6
  11. No Profile Picture
    rebel with a cause
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2004
    Location
    The Batsh!t Crazy State.
    Posts
    5,814
    Rep Power
    3467
    There's a feature in java called encapsulation. Basically the gist of it is that if you have a class C that implements or extends A you can create an instance of C by using A = new C(). That's what you're doing here intentionally or not.

    Now encapsulation is great for some things but you've happened across one of the downsides. Once initialize the class as type A the compiler sees an object of type A only. It can only use those methods and variables that are defined in A. (including anything that's overloaded in C).

    Now here's what you can do about it. As you've discovered you can declare a new instance with another variable. But that's not always viable. Actually it usually sucks. A slightly better option is to just create a second reference to the same instance.
    Code:
    MYCLASS myClass = new MyClass();
    instanceA myA = myClass
    instanceB myB = myClass
    There is one far better option. Simply declare an instance of MYCLASS. That will give you access to everything.
    With what you've got so far I don't see a reason for not simply declaring an instance of MYCLASS.

    Comments on this post

    • wsa1971 agrees : Good explanation. No points available, alas!
    Dear God. What is it like in your funny little brains? It must be so boring.
  12. #7
  13. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,040
    Rep Power
    1291
    Originally Posted by victorti83plus
    I can't post the code here, since it's too big...and it's an assignment.

    For your information, some method in interfaceA(methodA1....etc) is like "inserting" element in a list. And for interfaceB(methodB1...etc) it just play around with the elements inside the array.

    BUt the problem is...it returns an error message...more precisely it's a NullPointerException error message. I think it thinks the list is null or not create or something like that.
    If you're using an IDE, then it should print the stack trace and tell you on what line the exception occurs.

    A NullPointerException means that you are trying to access a method or field associated with a reference that is null.

    If you look at the line on which the exception occurs, you should be able to trace why it is null.
  14. #8
  15. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Oct 2004
    Location
    Brussels
    Posts
    1,001
    Rep Power
    845

    Lightbulb use casting


    Originally Posted by victorti83plus
    Hello,

    My main class(MYCLASS) in my program implements 2 interfaces. Lets call it interfaceA and interfaceB. interfaceA contains 3 methods(methodA1, methodA2, methodA3). And interfaceB contains 2 methods(methodB1, methodB2).

    After writing the code for my class, I try to run the program. I type something like:
    interfaceA obj = new MYCLASS();
    System.out.println(obj.methodA1)
    System.out.println(obj.methodA2)
    It works.

    but when I type:
    System.out.println(obj.methodB1) or anything method that are in interfaceB, then I got an error mesage.
    [trimmed]

    How do I fix this problem?
    How about this:
    Java Code:
    interfaceA objA = new MYCLASS();
    //If you <b>know</b> that your object belongs to a class
    //implementing both interfaces, use a <b>cast-expression</b>
    interfaceB objB = (interfaceB) objA;
    //objB is a reference of type interfaceB 'pointing' to the same object
    //now you can call the methods declared by interfaceB
    objB.methodB1();
    objB.methodB2();
     
    //Alternatively, you can make the calls without using another variable; 
    //just cast the variable to the needed interface:
    ((interfaceB)objA).methodB1();
     
    //In general, if you just have an Object variable but you're expecting some interface,
    //use <b>instanceof</b> operator to make sure you won't
    //get a ClassCastException at runtime
    Object obj = .... //instantiated elsewhere; could be a method parameter
    if(obj instanceof interfaceA) {
        ((interfaceA)obj).methodA1();
    }


    I hope this clarifies things a little bit.

IMN logo majestic logo threadwatch logo seochat tools logo