Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Please anyone find what's wrong my code??

0 votes
asked Apr 24, 2021 by shaukat ali (120 points)

abstract class Food {

    int foodprice=2000;

    String Foodname ="Hamburger";

}

class choclate extends Food{

    public void kitkat() {

        System.out.println("Ilove Kitkat choclate");

    }

    public static void main(String[] args) {

        choclate rony= new choclate();

        System.out.println(rony.foodprice);

        System.out.println(rony.Foodname);

        

    }

}

1 Answer

0 votes
answered Apr 27, 2021 by Peter Minarik (86,640 points)

In Java, your file name must match your class name. Your class with the main() is called chocolate, while your file is probably called "Main.java".

Your code with the below changes would work (with the file name being "Main.java"):

public class Main
{
    public static void main(String[] args) {
        choclate rony = new choclate();
        System.out.println(rony.foodprice);
        System.out.println(rony.Foodname);
    }
}

abstract class Food {
    int foodprice=2000;
    String Foodname ="Hamburger";
}

class choclate extends Food{
    public void kitkat() {
        System.out.println("Ilove Kitkat choclate");
    }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...