22.6k views
5 votes
Consider the classes Food, Cheese, and Velveeta where Cheese is a subclass of Food and Velveeta is a subclass of Cheese. State which of the following lines of code are legal.

Cheese c =new Food( );
Velveeta v =new Food( );
Cheese c =new Velveeta( );
Food f =new Velveeta( );
Food f =new Cheese( );

1 Answer

2 votes

Answer:

Cheese c =new Velveeta( );

Food f =new Velveeta( );

Food f =new Cheese( );

Step-by-step explanation:

The super-class is the parent class from which features and attributes are inherited from while the subclass or the child class is the class inheriting from the super or parent class.

In Java, the object instance of a class is created with the syntax;

class_name instance_name = new class_name();

Food is the super or parent class of Cheese (which makes Cheese its subclass) and Cheese is the parent class of Velveeta. The extends keyword is used to denote inheritance.

User Afeshia
by
6.6k points