简体   繁体   中英

Abstract method do not abstract class

Somehow, I am getting an JNI error for the code below when using an abstract. This shouldn't happen as I think I have wrote it correct. Can please someone have a look.

package Abstraction;

public class Abstraction1_After {
    public static void main(String[] args) {
        Iphone obj = new Iphone();
        Samsung obj1 = new Samsung();
        showPrice(obj);
    }


    public static void showPrice(Phone obj) {
        obj.showPrice();
    }
}

abstract class Phone {
    public abstract void showPrice();
}


class Iphone extends Phone {
    public void IphonePrice() {
        System.out.println("Price of Iphone Xr is 500€");
    }
}


class Samsung extends Phone {
    public void Samsungs9Price() {
        System.out.println("Price of Samsung S9 is 600€");
    }
}

Your Iphone class is inheriting the abstract method showPrice() from the Phone class. You have to override it since a non-abstract class can't have an abstract method

You probably want something like this:

class Iphone extends Phone {
    public void showPrice() {   
        System.out.println("Price of Iphone Xr is 500€");
    }
}

If you're trying to override a method you have to let the compiler know by giving the new method the same name. So just change the method name to be the same as the parents method and you should be good to go.

The same goes for your Samsung class of course

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