简体   繁体   中英

Type mismatch on java and cannot instantiate the type of object

right now i'm learning about interface in Java and now i got confused when this error is pop out

  • Type mismatch: cannot convert from Bola to bangunRuang
  • Cannot instantiate the type Bola

and the code is

package Modul5no2;

public interface bangunRuang {

public double keliling();

public double luas();

public double volume();
}  *//this is the interface*

public class kotakBalok implements bangunRuang {
double keliling,luas,volume;
public double keliling(double p, double l, double t){
    keliling = 4*(p+l+t);
    return keliling;
}

public double luas(double p,double l, double t){
    luas = 2*((p*l)+(p*t)*(l*t));
    return luas;
}

public double volume(double p,double l, double t){
    volume = p*l*t;
    return volume;
}

} *//this is for collect from interface*


public class Driver {
public static void main(String[]args){

    bangunRuang br1 = new Bola();  //i assume this is the problem

    br1.keliling();
    br1.luas();
    br1.volume();
}
}

ok thank u for your attention hope you understand what i mean lol xD

Its actually not clear what you want to achieve here.

Didn't you get compilation error in class kotakBalok because you have not provided body or implemented the methods of the interface bangunRuang as your class class kotakBalok implements bangunRuang . Though you have methods with same name but have different signature than the methods of interface , So that doesn't count as providing implementation of methods of interface.

In your Driver class you are calling new on class Bola ie bangunRuang br1 = new Bola() but you in the code you provided the class Bola does not exist. perhaps you want to do bangunRuang br1 = new kotakBalok();

At last this will not work if you have not provided the implementation of methods of interface bangunRuang in class kotakBalok

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