简体   繁体   中英

How do I create a Java child class that compiles in IntelliJ Idea?

Below is my code on Inheritance:

class Noodle {

    double lengthInCentimeters;
    String shape;
    String texture = “brittle”;

    public void cook() {
      this.texture = "cooked";
    }

    public static void main(String args) {
      Spaghetti spaghettiPomodoro = new Spaghetti();
      System.out.println(spaghettiPomodoro.texture);
    }

 }

I googled for a solution, the only advise that was close to what I needed was the following: " If you are already in the Project View, press Alt+Insert ( New ) | Class. Project View can be activated via Alt+1.

To create a new class in the same directory as the current one use Ctrl+Alt+Insert ( New… ).

You can also do it from the Navigation Bar, press Alt+Home, then choose package with arrow keys, then press Alt+Insert.

Another useful shortcut is View | Select In (Alt+F1), Project (1), then Alt+Insert to create a class near the existing one or use arrow keys to navigate through the packages.

And yet another way is to just type the class name in the existing code where you want to use it, IDEA will highlight it in red as it doesn't exist yet, then press Alt+Enter for the Intention Actions pop-up, choose Create Class." I tried this: ctrl + alt + insert. This took me to a GUI where I was asked to name the class, and, in addition, choose from “Class”, “Interface”, “Enum” and “Annotation”. For name I typed in “Spaghetti” and for “Kind”, I selected “Class”. This created a seemingly child class “Spaghetti” as a sub-file to “Noodle”. I was happy as there were no red squiggles in my code, but my happiness was short-lived as compilation failed. Can someone tell me what am I doing wrong?

Without using shortcuts you can simply write your code

I am guessing you have your parent class named Spaghetti . So, you can write your code as follows:-

class Spaghetti{

        String texture = “brittle”;

        public void cook() {
          this.texture = "cooked";
        }
    }

Noodle class:

class Noodle extends Spaghetti{

        double lengthInCentimeters;
        String shape;
        String texture = “brittle”;

        public void cook() {
          this.texture = "cooked";
        }
    public static void main(String args) {
      Spaghetti spaghettiPomodoro = new Spaghetti(); //this is your parent class object
      System.out.println(spaghettiPomodoro.texture); //here you are calling instance 
                                                     //variable of parent class i.e. texture
      Noodle obj=new Noodle(); // this is child class object 
      System.out.println(obj.texture); //this will call child class instance variable "texture"

    }

 }

PS:- I recommend you to make a separate class for main method

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