简体   繁体   中英

the code under class which implement the interface class does not execute

I have an interface class called intclass. this interface class has a method called mymethod. I have created a class called impclass which implements the interface class, whithin which have the code of mymethod. however I have another method called dmeth which receives the interface intclass as a parameter. the method dmeth is in a different class called dclass. however when I run dclass, the code under mymethod does not execute. I don't understand why. below is the code:

public interface intclass{

public void mymethod(string message);
}


public class impclass 
implements intclass{

public void mymethod(String message){

System.out.println("this is the message"+message);
}

}



public class dclass{

public void dmeth(intclass cl){
....
}

public static main(String[] arg0){
impclass icl =new impclass()
dclass d=new dclass();
d.dmeth(icl);
}
}

I have no Idea on what is going wrong.. In case there is even an error concerning the expected parameter for mymethod function, I expect the error, or crashing of the program. but in my case the program run smoothly but the System.out.println("this is the message"+message); does not print. I tried to put a break in mymethod function and debug, but it was then that I realised mymethod function is not even reached. does anyone has an Idea on what ould be going on

String and string are not the same thing. Be aware of it when you type

public interface intclass{
    public void mymethod(string message);
}

and

public class impclass 
implements intclass{

    public void mymethod(String message){ //Note the Uppercase S
        System.out.println("this is the message"+message);
    }
}

Also, your impclass is missing a constructor, and you are not calling the method in any way. Your dmeth should contain

public void dmeth(intclass cl){
    cl.mymethod("Message");
}

in order to the mymethod to be executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM