Question

Consider the following classes (For Java): class A { public void process() { // Details omitted...

Consider the following classes (For Java):

class A {
public void process() {
// Details omitted
}
}

class B extends A {
public void compute() {
// Details omitted
}
}

class C extends B {
public void calculate() {
// Details omitted
}
}

Which of the following client code fragments are legal?

Select one or more:

C c = new C();
c.compute();

A c = new C();
c.process();

C c = new A();
c.process();

A a = new A();
a.process();

B b = new B();
b.process();

B b = new B();
b.compute();

A c = new A();
c.calculate();

C c = new C();
c.process();

0 0
Add a comment Improve this question Transcribed image text
Answer #1


The following are legal

C c = new C();
c.compute();
-------
A c = new C();
c.process();
-------
A a = new A();
a.process();
-------
B b = new B();
b.process();
----
B b = new B();
b.compute();
-----
C c = new C();
c.process();


We can assign derived class objects to super class variables but not vice versa. Also methods in super class are available in derived classes

Add a comment
Know the answer?
Add Answer to:
Consider the following classes (For Java): class A { public void process() { // Details omitted...
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
  • Consider the following Java classes: class A{ public int foo () { return 1; } public...

    Consider the following Java classes: class A{ public int foo () { return 1; } public void message () { System.out.println( "A" + foo()); } } class B extends A { public int foo() {return 2; } } class C extends B { public void message () { System.out.println( "C" + foo()); } } (i) What are the outputs of the following code? (ii) What would be the outputs if Java used static dispatching rather than dynamic dispatching? B b...

  • Q17 Suppose the following classes (some details were omitted) 10 Points class Hair { public boolean...

    Q17 Suppose the following classes (some details were omitted) 10 Points class Hair { public boolean isClean() { } // Returns true if the hair is clean public void lather() { } // Rubs shampoo into hair until // it's nice and frothy سہه class Shampoo { public void openBottle() { } // Opens the bottle of shampoo public void closeBottle() { } // Closes the bottle of shampoo public void apply(Hair hair) { } // Applies shampoo to hair...

  • please answer correctly public class Eve public void a print("Eve a ": public void bo print("Eveb...

    please answer correctly public class Eve public void a print("Eve a ": public void bo print("Eveb "; public String toString() { return "Eve ts": public class Sam extends Eve public void bo a(): print("Sanb ": public String toString() { return "Sants": public class Lucas extends Sam public void a() { print("Lucas a ": print (toString() + " "); public String toString() { String sup = super.toString): return sup""+ sup: public class Josh extends Lucas public void b) { print("Josh b...

  • 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();

  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...

  • Java question Consider the following code segment. public class KaBoom { public int b; public void...

    Java question Consider the following code segment. public class KaBoom { public int b; public void m1 () {//Your code goes here } public void m2 (KaBoom k) { System, out. print In (k. b); } } Complete the method body of m1 () indicated by the comment line. The m1 () method simply calls the m2 () method by passing in the current KaBoom object variable to m2 ().

  • What is the output of running class C? The three Java classes are in separate Java...

    What is the output of running class C? The three Java classes are in separate Java files in the same directory (or in the same package). class A { public AO { System.out.println("The default constructor of A"); } // end A constructor } // end class A class B extends A { public BCString s) { System.out.println(s); } // end B constructor } // end class B public class C { public static void main(String[] args) { B b =...

  • Java Question abstract class A { public void templateMethod() { operation1 (); operation3 (); } abstract...

    Java Question abstract class A { public void templateMethod() { operation1 (); operation3 (); } abstract public void operation1 (); public void operation3() { System.out.print("A-op3"); } } Class B extends A { public void operation1() { System.out.print("B-op1"); } } 1.What is the output of: B b1 = new B(); b1.operation1(); 2.What is the output of: A b1 = new B (); a1.operation() ; 3.What is the output of : A b1 = new B(); a1.templateMethod();

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • please answer correctly. Language/Type Java Inheritance polymorphism Assume that the following classes have been defined: public...

    please answer correctly. Language/Type Java Inheritance polymorphism Assume that the following classes have been defined: public class George extends Elaine ( public void method1() { print("George 1 "); public class Jerry { public void method10) { print("Jerry 1 "); public void method20 { print("Jerry 2 "); public String toString() { return "Jerry": public class Elaine extends Kramer public String toString() { return "Elaine " + super.toString(); public class Kramer extends Jerry public void method1() { super.method1(); print ("Kramer 1"); public...

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