Java class requirements

In the next task

1. Which of the following is true for Java classes?
a) All classes must have 1 parent element, but can have any number of child (derived or extended) classes.
b) All classes must have 1 child (derived or extended) class, but may have any number of parent classes.
c) All classes must have 1 parent class and may have one child (derived or extended) class. d) All classes can have any number (0 or more) of parent classes and any number of child (derived or extended) classes.
e) All classes can have 0 or 1 parent class and any number of child (derived or extended) classes.

I figured the answer would be e) . I thought about this because all classes can have at most one parent class or at least 0 (the Object class does not have a parent class). In addition, you can have any number of children (provided that the integers are greater than or equal to 0). The correct answer is: a) :

All classes must have 1 parent element, but can have any number of child (derived or extended) classes.

Does this mean that you cannot consider the class Object to be a class?

Is there a parent in the Object class?

In addition, I recently asked another question about Java, also from this series of assignments (in fact, an optional test review, not a homework assignment). Sorry for the two posts, but I didn’t think that he followed the correct etiqutte to combine the questions in one post.

+8
java class
source share
2 answers

From a technical point of view, neither A nor E are correct. The Object class is unique in that it does not have a parent; every other class must have exactly one parent. Thus, it is not that every class must have a parent element - Object - and it is not true that all classes can have null parents - only Object allowed to do this.

However, if we restrict the question to user-defined classes - leaving the special case of Object outside the image - the answer is correct.

+1
source share

The question seems to be a little misleading. (e) appears closest to the correct one. The following concepts may help to better understand the issue:

  • The object class is the superclass of all Java classes.
  • Java does not support multiple inheritance
  • Multiple inheritance can be achieved in Java using interfaces.
  • Interfaces are not classes.
  • A class that is not final can be subclassed by any number of Java classes.
0
source share

All Articles