Answers for "inheritance'"

0

inheritance

class left {
public:
    void foo();
};

class right {
public:
    void foo();
};

class bottom : public left, public right {
public:
    void foo()
    {
        //base::foo();// ambiguous
        left::foo();
        right::foo();

        // and when foo() is not called for 'this':
        bottom b;
        b.left::foo();  // calls b.foo() from 'left'
        b.right::foo();  // call b.foo() from 'right'
    }
};
Posted by: Guest on October-05-2021
0

inheritance'

// Base Class Vehicle
class Vehicle {

    // Private Fields
    private String vehicle;
    private int year;
    private String model;

    // Parameterized Constructor
    public Vehicle(String vehicle, int year, String model) {
        this.vehicle = vehicle;
        this.year = year;
        this.model = model;
    }

    // public method to print details
    public void printDetails() {
        System.out.println("Vehicle Type: " + vehicle);
        System.out.println("Year: " + year);
        System.out.println("Model: " + model);
    }

}

// Derived Class Car
class Car extends Vehicle {

    // Private field

    // Parameterized Constructor
    public Car(String vehicle, int year, String model) {
        super(vehicle, year, model); // calling parent class constructor

    }

    public void carDetails() { // details of car
        printDetails(); // calling method from parent class

    }

}

class Sample {

    public static void main(String[] args) {
        Car elantraSedan = new Car("Car", 2019, "Elantra"); // creation of car Object
        elantraSedan.carDetails(); // calling method to print details
    }
}
Posted by: Guest on April-03-2022

Browse Popular Code Answers by Language