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); }}
Solution
This is a Java program that demonstrates the concept of polymorphism in object-oriented programming. Here's a step-by-step explanation:
-
The program defines a class named
Basewith a methodPrint()that prints the string "Base". -
Then, it defines another class
Derivedthat extendsBase. This meansDerivedis a subclass ofBaseand it inherits all the public and protected members ofBase.Derivedalso has a methodPrint()that prints the string "Derived". -
The class
InhDrivercontains the main method which is the entry point of the program. It also has a static methodprintMsg(Base ob)that calls thePrint()method on the object it receives. -
In the main method, three objects are created:
xof typeBase,yof typeBasebut instantiated withDerived, andzof typeDerived. -
The
printMsg()method is then called with each of these objects. -
When
printMsg(x)is called, thePrint()method ofBaseis called, so "Base" is printed. -
When
printMsg(y)is called, even thoughyis of typeBase, it was instantiated withDerived, so thePrint()method ofDerivedis called and "Derived" is printed. This is an example of polymorphism, where the type of the object determines which method is called. -
Similarly, when
printMsg(z)is called, thePrint()method ofDerivedis called and "Derived" is printed.
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
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.