Problem with constructors if inherited from Java

Asked 2 years ago, Updated 2 years ago, 96 views

Java created a Bus class that inherits the Car class as follows. Running results in an error.

class Solution {
  public static void main(String[] args) {
    Bus bus = new Bus("abc",1000,1000);
  }
}

class Car{
  String name;
  int number;
  public Car(String name, int number){
    this.name = name;
    this.number = number;
  }
}

class Bus extends Car{
  int fee;
  public Bus(String name, int number,int fee){
    this.name = name;
    this.number = number;
    this.fee = fee;
  }
}

public Bus(String name, int number,int fee){ ^ reason: actual and formal argument lists differ in length 1 error

If you change the Bus class to the following, the error does not occur.

class Bus extends Car{
  int fee;
  public Bus(String name, int number,int fee){
    super(name,number);
    this.fee = fee;
  }
}

Is the original constructor not allowed to use super?

java constructor

2022-09-21 15:41

1 Answers

In the code written by Tod, you must call super(). I'll explain the reason step by step.

Let's say you create a car class and an object with nothing at first. (Let's delete all the code Todd wrote and start with a blank class with nothing.))

class Car {

}

Car car = new Car();

This is how we normally create objects. When we create an object with the keyword new, we call the constructor.

newCar();<---Calling the constructor

But you don't see a constructor in the class Car. You can create a constructor yourself, but if you don't create a constructor, the compiler will create it on its own.

The default constructors created by the compiler are as follows:

public Car {

}

If you overload the constructor and implement it, the compiler does not automatically create the constructor.

class Car{
  String name;
  int number;
  public Car(String name, int number){
    this.name = name;
    this.number = number;
  }
}

Since the constructor created by Tod is included, the basic constructor called Car() is not created. That's why Carcar = newCar(); it's impossible to call. Clear the generator overload code and return to the original class. You now inherit a class called Car and create a class called Bus.

class Car {

}
class Bus extends Car {

}

What happens when you create a Bus object here? Bus = new Bus(); This calls the constructor of Bus, which calls the constructor of Car and calls the constructor of the Object class. (You can't see it in Java, but they all inherit the class Object.) This process is called Constructur chaining. When you create a Bus, the heap contains not only the Bus, but also all the information Car has. All of the instance variables in both classes are in the heap.


public class Car {
    public Car(){
        System.out.println("Making an Car");
    }
}

public class Bus extends Car {
    public Bus() {
        System.out.println("Making an Bus");
    }
}

If you look at the output statement result with System.out.println(), Car is printed first and Bus is printed later. Car is output because the compiler automatically calls super().

If you have created a constructor but did not invoke super(), the compiler automatically adds a code that calls super() to all constructors. Only if the constructor does not call another overloaded constructor. When you automatically add a declaration that calls a parent class constructor in the compiler, use the constructor super(); in the form without a factor.

Now let's add the constructor code that contains the factors that Todd created.

class Car {
    String name;
    int number;

    public Car(String name, int number){
        this.name = name;
        this.number = number;
        System.out.println("CAR 2");
    }
}

class Bus extends Car {

    int fee;

    public Bus(String name, int number, int fee) {
        this.name = name;
        this.number = number;
        this.fee = fee;
    }
}


Bus bus = new Bus("서울", 1004, 2000);

When you create an object by invoking the constructor, the compiler automatically invokes super();. You attempt to create a parent first, but the parent Car class does not have a constructor named Car();. The compiler automatically creates a constructor named publicCar(){} only if there are no constructors. If you create a publicCar(){} constructor in a class called Car, there will be no errors.

class Car {
    String name;
    int number;

    public Car(){

    }
    public Car(String name, int number){
        this.name = name;
        this.number = number;
        System.out.println("CAR 2");
    }
}

If you add a constructor without a factor like this, or call Car's constructor public Car (String name, int number) in the Bus class, the error does not occur.

A parent class of a new child class object must always be created before it can be created. Think simply like this. The parent is always created first. Parents are born before their children. :)


2022-09-21 15:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.