Knowee
Questions
Features
Study Tools

class Base { public void Print() { System.out.println("Base"); }} class Derived extends Base { public void Print() { System.out.println("Derived"); }} class InhDriver { public static void printMsg(Base ob) { ob.Print(); } public static void main(String[] args) { Base x = new Base(); Base y = new Derived(); Derived z = new Derived(); printMsg(x); printMsg(y); printMsg(z); }}

Question

class Base { public void Print() { System.out.println("Base"); }} class Derived extends Base { public void Print() { System.out.println("Derived"); }} class InhDriver { public static void printMsg(Base ob) { ob.Print(); } public static void main(String[] args) { Base x = new Base(); Base y = new Derived(); Derived z = new Derived(); printMsg(x); printMsg(y); printMsg(z); }}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

This is a Java program that demonstrates the concept of polymorphism in object-oriented programming. Here's a step-by-step explanation:

  1. The program defines a class named Base with a method Print() that prints the string "Base".

  2. Then, it defines another class Derived that extends Base. This means Derived is a subclass of Base and it inherits all the public and protected members of Base. Derived also has a method Print() that prints the string "Derived".

  3. The class InhDriver contains the main method which is the entry point of the program. It also has a static method printMsg(Base ob) that calls the Print() method on the object it receives.

  4. In the main method, three objects are created: x of type Base, y of type Base but instantiated with Derived, and z of type Derived.

  5. The printMsg() method is then called with each of these objects.

  6. When printMsg(x) is called, the Print() method of Base is called, so "Base" is printed.

  7. When printMsg(y) is called, even though y is of type Base, it was instantiated with Derived, so the Print() method of Derived is called and "Derived" is printed. This is an example of polymorphism, where the type of the object determines which method is called.

  8. Similarly, when printMsg(z) is called, the Print() method of Derived is called and "Derived" is printed.

This problem has been solved

Similar Questions

What will be the output of the following code?This question is required.*class Base {public void display() {System.out.println("Base");}}class Derived extends Base {public void display() {System.out.println("Derived");}}public class Main {public static void main(String[] args) {Base b = new Derived();b.display();}}

Select the correct answerWhat will be the output of the following Java program? final class base     {         int a;    }        class derived extends base    {        int b;        System.out.println(b + " " + a);      }        class inheritance     {        public static void main(String args[])        {            derived obj = new derived();            obj.display();             }   }OptionsRuntime Error3 32 2Compilation Error

What will be printed after executing following program code?class Base{ int value = 0; Base(){ addValue(); } void addValue(){ value += 10; } int getValue(){ return value; }}class Derived extends Base{ Derived(){ addValue(); } void addValue(){ value += 20; }}public class Test{ public static void main(String[] args){ Base b = new Derived(); System.out.pr  10203040

Select the correct answerWhat will be the output of the following Java code? class main {    public int x;    protected int y; }     class derived extends main {     int y;     void display() {         super.y = 5;         System.out.println(x + " " + y);     } }     class Output {     public static void main(String args[]) {          derived obj = new derived();         obj.x=2;         obj.y=3;            obj.display();          } }Options3 22 32 55 2

Debug the code: #include <iostream> class Base { public: virtual void print() { std::cout << "Base\n"; } }; class Derived : public Base { public: void print() override { std::cout << "Derived\n"; } }; int main() { Base* ptr = new Derived(); ptr->print(); delete ptr; return 0; } (2 Points) a) Base b) Derived c) Compilation Error d) Runtime Error

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.