Home>

This is a program that creates two types of airplanes, FighterAiracraft and passenger plane (PassengerPlane) class instances, and executes the methods of each class. These two classes have common members.
Therefore, rewrite the program so that members common to these two classes are aggregated into the AirPlane class, and the FighterAiracraft class and PassengerPlane are inherited from those classes. However, at this time, the AirPlane class should be an abstract class with an abstract method fly ().

The answers are correct, but the members common to these two classes are aggregated into the AirPlane class, and the FighterAiracraft class and PassengerPlane are inherited from those classes. Rewrite the program. However, at this time, the AirPlane class should make the abstract method fly () an abstract class. I would like to be able to teach you without the part of.

Here is the code I wrote:

public class Airplane extends FighterAircraft {
    / **
     * @return
     *
     * /
    public void fly () {
        System.out.println ("Fly to attack.");
    }
}
public class FighterAircraft {
    private String Type = "fighter";
    public String getType () {
        return Type;
    }
    public void fly () {
        System.out.println ("Fly to attack" ");
    }
    public void fight () {
        System.out.println ("Battle".);
    }
}
public class PassengerPlane {
    private String type = "passenger plane";
    public String getType () {
        return type;
    }
    public void fly () {
        System.out.println ("Flight passengers to the destination");
    }
    public void carryPassengers () {
        System.out.println ("Transport passengers to their destination.");
    }
}
public class Problemex3_1 {
    / **
     * @param args
     * /
    public static void main (String [] args) {
        FighterAircraft fighter = new FighterAircraft ();
        PassengerPlane airlinear = new PassengerPlane ();
        // Fighter class processing
        fighter.fly ();// fly
        fighter.fight ();// fight
        // Passenger aircraft class processing
        airlinear.fly ();// fly
        airlinear.carryPassengers ();// carry passengers
    }
}
  • Answer # 1

    Isn't the inheritance relationship strange?

    For question sentences (something isn't it?)

    // Some reserved words are omitted
    abstract class AirPlane
    class FighterAircraft extends AirPlane
    class PassengerPlane extends AirPlane


    It is written like this, but in the source code in the question

    class AirPlane extends FighterAircraft
    class FighterAircraft
    class PassengerPlane


    It looks like this.
    Expressed in words,
    "There is a type of vehicle called a fighter, and a plane is one type. A passenger plane is another vehicle."
    It is a feeling.

      

    Rewrite the program so that members common to these two classes are aggregated into the AirPlane class, and the FighterAiracraft class and PassengerPlane are inherited from those classes. However, at this time, the AirPlane class should make the abstract method fly () an abstract class.

    This is theAirPlaneclass

    public abstract class AirPlane {
        protected String type;
        public abstract void fly ();
        // Getter (accessor) if necessary
        public String getType () {
            return type;
        }
    }


    I think this should be rewritten and the remaining two classes can be inherited.

  • Answer # 2

    [Java] What is abstract?
    How to create an interface
    It is explained in a very easy-to-understand manner on the search and output site with java abstract.

    "Fly to attack."
    "Fly to your destination with passengers."

    If this is broken down, it will be easier to understand.

    "Fly "quote +" in order to attack."

    "Passengers and fly "quote +" to the destination."

    fly ()""

    It looks good if you change each first half.