Questions and Answers
Identify the false statement: a. When you use inheritance, you save time and reduce errors. b. A class that is used as a basis for inheritance is called a subclass. c. When you use inheritance in Java, you can create a new class that contains all the data and methods of an existing class.
Identify the false statement: a. Subclasses are more specific than the superclass they extend. b. A derived class has access to all its parents' nonprivate methods. c. You use the keyword inherits to achieve inheritance in Java.
Identify the false statement: a. Any child class object has all the attributes of its parent, but all of those attributes might not be directly accessible. b. You override a parent class method by creating a child class method with the same identifier but a different parameter list or return type. c. When a child class method overrides a parent class method, and you use the method name with a child class object, the child class method version executes.
Identify the false statement: a. When a superclass contains only nondefault constructors, you must include at least one constructor for each subclass you create. b. When constructors initialize variables, you usually want the superclass constructor to initialize the data fields that originate in the superclass and the subclass constructor to initialize the data fields that are specific to the subclass. c. When you create any subclass object, the subclass constructor executes first, and then the superclass constructor executes.
Signup and view all the answers
Identify the false statement: a. You can use the keyword super within a method in a derived class to access a method in a base class that has not been overridden. b. You can use the keyword super within a method in a derived class to access an overridden method in a base class. c. You can use the keyword this within a method in a derived class to access an overridden method in a base class.
Signup and view all the answers
Identify the false statement: a. If a data field is defined as protected, a method in a child class can use it directly. b. A subclass inherits all the data and methods of its superclass, except the private ones. c. Information hiding describes the concept of keeping data private.
Signup and view all the answers
Identify the false statement: a. A subclass cannot override methods that are declared final in the superclass. b. A subclass cannot override methods that are declared private in the superclass. c. A subclass cannot override methods that are declared static in the superclass.
Signup and view all the answers
If School is a child class of Building, and modelHigh is an object of type School, which of the following statements is valid?
Signup and view all the answers
Employing inheritance reduces errors because __________.
Signup and view all the answers
If the only constructor in a superclass requires arguments, its subclass __________.
Signup and view all the answers
If you create a data field or method that is __________, it can be used within its own class or in any classes extended from that class.
Signup and view all the answers
When a parent class contains a static method, child classes __________ override it.
Signup and view all the answers
You call a static method using the name of __________, a dot, and the method name.
Signup and view all the answers
When you instantiate an object that is a member of a subclass, the __________ constructor executes first.
Signup and view all the answers
When a subclass method has the same name and argument types as a superclass method, the subclass method __________ the superclass method.
Signup and view all the answers
The Java keyword that creates inheritance is inherits.
Signup and view all the answers
Which of the following statements is true?
Signup and view all the answers
Study Notes
Inheritance Basics
- Inheritance allows for code reuse, minimizing errors, and creating a new class based on an existing class.
- A class used as a basis for inheritance is called a superclass, not a subclass.
- Subclasses inherit attributes and methods from their parent classes, but some inherited attributes may not be directly accessible due to access modifiers.
Class and Constructor Relationships
- Subclasses are more specific than their superclass and can access non-private methods from the superclass.
- When a superclass has no default constructors, each subclass must define its own constructor.
- The constructor of a superclass initializes data fields originating from that class, while the subclass constructor initializes its own specific fields.
Method Overriding and Access
- Overriding occurs when a subclass method has the same name as a superclass method, enabling polymorphism.
- The
super
keyword can be used in a subclass to access methods from its superclass, especially for non-overridden methods. - Private methods in a superclass cannot be accessed or overridden by subclasses, but final methods in a superclass cannot be overridden either.
Data Accessibility and Modifiers
- Protected access allows child classes to directly use inherited fields.
- Information hiding is the principle of keeping some data private, preventing access from outside classes.
Static Methods and Constructors
- Static methods in a class cannot be overridden by child classes, maintaining their original functionality.
- To call a static method, the class name followed by the method name is used.
- When creating an object of a subclass, the constructor of the parent class executes first, establishing the foundational state of the object.
Miscellaneous
- If a constructor in a superclass is defined to take arguments, the subclass is required to include a constructor to pass these arguments.
- A method in a subclass can override a superclass method if it shares the same name and argument types.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your understanding of Java inheritance concepts with this quiz. Identify true and false statements related to classes and inheritance. It's a great way to review fundamental programming principles in Java.