Question

public class Polymorph public static class CAL public void printHello System.out.println(Hya); public static class w extend
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#source code:

public class Polymorph{

public static class CA{

public void printHello(){

System.out.println("Hiya");

}

}

public static class W extends CA{

public void printHello(){

System.out.println("Howdy");

}

}

public static class PB extends W{

public void printHello(){

System.out.println("Hello");

}

}

public static void main(String args[]){

CA aa=new CA();

CA ab=(CA) new W();

CA ac=(CA) new PB();

W bc=(W) new PB();

aa.printHello();

ab.printHello();

ac.printHello();

bc.printHello();

}

}

i public class Polymorph{ public static class CA{ public void printHello() { System.out.println(Hiya); public static class

#output:

user user-Latitude - 3490:-/Desktop$ javac Polymorph.java user@user-Latitude - 3490:-/Desktop$ java Polymorph Hiya Howdy Hell

so answer:

first line:Hiya

second line: Howdy

third line:Hello

forth line:Hello

explantation:

first line:

we have class CA it not extend to another class it was unique class

so we create object to CA call the method in the CA class we have Hiya that was output

second line:

we have class W that was inherited from class CA so CA is base or parant class and W is derived class here

CA and W class both have same method so here polymorphisam exists but it was over ride in child class so

we get output as Howdy

thrid line:

PB class it was inheriteds from W class now W is parent class to PB and PB is child class but now PB class have two class properties such CA class and W class too because W class is child class to CA

but in PB class also same method here also over ride takes place so we get output as "Hello"

forth line:

coming final object which is bc so with respec to W and PB so we get output as "Hello"

Add a comment
Know the answer?
Add Answer to:
public class Polymorph public static class CAL public void printHello System.out.println("Hya"); public static class w extends...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is the output of the following code? class parent { public void someFunction() { System.out.println("Parent...

    What is the output of the following code? class parent { public void someFunction() { System.out.println("Parent Function"); } } public class child extends parent { public void someFunction() { System.out.println("Child Function"); } public static void main(String args) { parent p1 = new parent(); parent p2 = new child(); child c1 = new child(); p1.someFunction(); p2.someFunction(); c1.someFunction(); } }

  • public class Game{ public void players() { System.out.println(“1 or more”); } } public class Chess extends...

    public class Game{ public void players() { System.out.println(“1 or more”); } } public class Chess extends Game { public void boardSize() { System.out.println(“8 by 8”); } public void players() { System.out.println(“2”); } } public class ThreeDChess extends Chess{ public void boardSize() {System.out.println(“7 levels”); } public void hasMoveableBoard() { System.out.println(“true”); } } Assume that these statements have already executed: Game g = new Game(); Chess c = new Chess(); ThreeDChess t = new ThreeDChess(); Object og = g; Game gc =...

  • Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args)...

    Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args) {         TestThread thread = new TestThread();     }     @Override     public void run() {         printMyName();     }     private void printMyName() {         System.out.println("Thread is running");     } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...

  • public class SquareTest {    public static void main(String[] args) {               System.out.println("Number...

    public class SquareTest {    public static void main(String[] args) {               System.out.println("Number of sides is " + Square.NUM_OF_SIDES);                      Square s1 = new Square(-5.7f);               System.out.println(s1);               s1.setLength(0.001f);        System.out.println(s1.getLength());               s1.setLength(-9999);        System.out.println(s1.getLength());               System.out.println("Number of sides is " + s1.NUM_OF_SIDES);               s1.calculateArea();                            } }...

  • What is the output of the following code? class C1 { public double PI=3.1; public void...

    What is the output of the following code? class C1 { public double PI=3.1; public void m1() { System.out.println("M1 of C1"); } } class C2 extends C1 { public double PI=3.14; public void m1() { System.out.println("M1 of C2"); } } public class C { public static void main(String[] args) { C1 obj = new C2(); System.out.print(obj.PI); } } 3.1 3.14 None of the above

  • What is the output of the following code? class C1 { public double PI=3.1; public void...

    What is the output of the following code? class C1 { public double PI=3.1; public void m1() { System.out.println("M1 of C1"); } } class C2 extends C1 { public double PI=3.14; public void m1() { System.out.println("M1 of C2"); } } public class C { public static void main(String[] args) { C1 obj = new C2(); obj.m1(); } } M1 of C1 M1 of C2 None of the above

  • 2016 points output of following Java Program? public class Base { public final void show() {...

    2016 points output of following Java Program? public class Base { public final void show() { . System.out.println("Base::show() called"); public class Derived extends Base { public void show() { System.out.println("Derived::show() called"); } e lor rested in order public class Main { ects from and public static void main(String[] args) { Base b = new Derived(); b.show();

  • class x { x () {} private void one () {} } public class Y extends...

    class x { x () {} private void one () {} } public class Y extends x { Y() {} private void two () { one(); } public static void main (String[] args) { new Y().two(); } } What changes will make this code compile? a. Adding the public modifier to the declaration of class x b. Adding the protected modifier to the x() constructor c. Changing the private modifier on the declaration of the one() method to protected d....

  • What is the output of following program: public class Test{ public static void main(String[] args) {...

    What is the output of following program: public class Test{ public static void main(String[] args) { A a = new A(): a method B(): } } class A{ public A(){ System out println("A's constructor is executed"): } public void method(){ System out printin ("methodA is executed"): } public void methodAB(){ System out printin ("As methodAB is executed"): } } class B extends A { private int num = 0: public B (){ super(): System out printin ("B's constructor is executed"):...

  • **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args)...

    **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args) for (int k-1: k10:k if (k8) continue: System.out.println k) Question 2 3.5 pts Analyze the following code. Which one is correct? package p public class A package p2: import p1. public class B extends Af int i- 5 private int j- 10: public A0I public static void main(Stringll args) B b new B0: System.out.println(a.i+", "+ajt", "+b.it""+bj): a.i is printable None of them b.i is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT