adplus-dvertising
frame-decoration

Question

What is the output of the below Java program on the references of Superclass and Subclass?
class Food
{
  void show()
  {
    System.out.print("FOOD ");
  }
}
class Bread extends Food
{
  void toast()
  {
    System.out.print("TOASTED ");
  }
}
public class Inheritance5
{
  public static void main(String[] args)
  {
    Food foo = new Food();
    foo.show();
    Food foo2 = new Bread();
    foo2.show();
    Bread br = new Bread();
    br.toast();
    br.show();
  }
}

a.

FOOD FOOD FOOD FOOD

b.

FOOD FOOD TOASTED FOOD

c.

FOOD TOASTED FOOD FOOD

d.

Compiler error

Posted under Java Programming

Answer: (b).FOOD FOOD TOASTED FOOD

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. What is the output of the below Java program on the references of Superclass and Subclass?

Similar Questions

Discover Related MCQs

Q. A subclass object can be used to invoke (call) a method or property of the superclass. State TRUE or FALSE.

Q. A superclass reference can not be used to invoke a method or variable of the subclass. State TRUE or FALSE.

Q. A Superclass reference can refer to a Subclass Object without casting. State TRUE or FALSE.

Q. If a class is not subclassed by any class, then defining a default constructor is not mandatory in Java.State TRUE or FALSE.

Q. What is the maximum number of levels possible in a Multilevel Inheritance in Java?

Q. To stop or block inheriting a given class, the ___ keyword is used before the class.

Q. To control inheritance to different classes and levels, Java provides ____.

Q. Can you call it a full-fledged inheritance of using ABSTRACT classes and INTERFACES in Java?

Q. Which is the keyword used to implement inheritance in Java?

Q. You can not inherit a Superclass'es constructor even after using inheritance in Java. State TRUE or FALSE.

Q. A Subclass can become a Superclass to another class extending from it in Java. State TRUE or FALSE.

Q. When a Class inherits two superclasses (not in Java), it is called ____ inheritance.

Q. In a Multi-Level Inheritance in Java, the last subclass inheritsmethods and properties of ____.

Q. In a Multi Level Inheritance Class-C inherits from Class-B and Class-B inherits from Class-A. State TRUE or FALSE.

Q. In a Single inheritance, Class Binherits only from Class A. State TRUE or FALSE.

Q. What are the types of Inheritances(Whether Java supports or not) available in Object-Oriented Programming Languages?

Q. You should use Inheritance when there is an IS-A relationship between classes. State TRUE or FALSE.

Q. Java language supports ___ type of inheritance.

Q. The class that inherits an already defined class is called ___.

Q. The class that is being inherited or subclassed is called ___.