Exam 10: Introduction to Inheritance

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

How are real-life examples of inheritance similar to object-oriented programming inheritance?

Free
(Essay)
5.0/5
(33)
Correct Answer:
Verified

You might choose plants and animals based on inheritance. You plant impatiens next to your house because of your shady street location; you adopt a Doberman Pinscher because you need a watchdog. Every individual plant and pet has slightly different characteristics; but within a species, you can count on many consistent inherited attributes and behaviors. Similarly, the classes you create in object-oriented programming languages can inherit data and methods from existing classes. When you create a class by making it inherit from another class, you are provided with data fields and methods automatically.

public class Student { private String firstName; private String lastName; private String address; private String username; public Student(String studentFirstName, String StudentLastName, String studentAddress String studentUsername) { firstName = studentFirstName; lastName = studentLastName; address = studentAddress; username = studentUsername; } } public void displayStudentInfo() { System.out.println("Name: " + firstName + " " + lastName); System.out.println("Address: " + address); System.out.println("Username: " + username); } Using the above code, call the constructor to create a new instance of the Student object named joe. The constructor method will take the four string values (first name, last name, address, username) and set the initial state of the Student object to be: firstname = "Joe", lastname = "Jones", address = "123 Oak St.", username = "JJones".

Free
(Essay)
4.8/5
(36)
Correct Answer:
Verified

Constructor methods must be called using the new keyword:
public class StudentExample
{
public static void main(String[] args)
{
Student joe = new Student("Joe", "Jones", "123 Oak St.", "JJones");
joe.displayStudentInfo();
}
}

An error is generated by the compiler when you attempt to override a static method with a nonstatic method.

Free
(True/False)
4.9/5
(29)
Correct Answer:
Verified

True

When a protected data field or method is created, it can be used within its own class or in any classes extended from that class; but it cannot be used by outside classes.

(True/False)
4.9/5
(44)

Using the keyword ____ provides you with an intermediate level of security between public and private access.

(Multiple Choice)
4.9/5
(44)

If a superclass contains only constructors that require arguments, you do not need to create a subclass constructor.

(True/False)
4.8/5
(42)

Match each term with the correct statement below. -An instance method call

(Multiple Choice)
4.9/5
(33)

You are never aware that ____ is taking place; the compiler chooses to use this procedure to save the overhead of calling a method.

(Multiple Choice)
4.9/5
(35)

What is information hiding and how is it used?

(Essay)
4.8/5
(34)

public class Student { private String firstName; private String lastName; private String address; private String username; public Student(String studentFirstName, String StudentLastName, String studentAddress String studentUsername) { firstName = studentFirstName; lastName = studentLastName; address = studentAddress; username = studentUsername; } } Create a method called displayStudentInfo that will display the value of the first name, last name, address, and username objects using println() statements.

(Essay)
4.9/5
(43)

You use the keyword ____ to achieve inheritance in Java.

(Multiple Choice)
4.8/5
(34)

class Vehicle {} public class Car extends Vehicle { public static void main(String args[]) { Vehicle myCar = new Car(); boolean result = myCar instanceof Car; System.out.println(result); } } The above code uses the instanceof operator to determine whether an object is a member of a class. What will be the output following execution?

(Essay)
4.9/5
(36)

The class used as a basis for inheritance is the ____ class.

(Multiple Choice)
4.9/5
(37)

It is useful to override the parent class members when the superclass data fields and methods are not completely appropriate for the subclass objects.

(True/False)
4.9/5
(33)

You have a Student class with a constructor that requires two arguments: a character "F" for Freshman and an integer "2020" for the year. Create a Freshman class that is a subclass of Student with a constructor for Freshman. Include the initial super() statement and include a comment that reads "//Other statements go here".

(Short Answer)
4.7/5
(36)

public class Student { private String firstName; private String lastName; private String address; private String username; public Student(String ____, String ____, String ____, String ____) { ____ = ____; ____ = ____; ____ = ____: ____ = ____; } } In the shaded areas provided, create a constructor that accepts the values of the student's first name, last name, address, and username. Use these values to set the initial state of the object.

(Essay)
4.8/5
(40)

What happens when you write your own constructor in Java and what must you pay attention to?

(Essay)
5.0/5
(30)

Match each term with the correct statement below. -Keeps data private

(Multiple Choice)
4.8/5
(43)

____ polymorphism is the ability of one method name to work appropriately for different subclass objects of the same parent class.

(Multiple Choice)
4.8/5
(33)

When you instantiate an object that is a member of a subclass, how many constructors are called?

(Essay)
4.7/5
(42)
Showing 1 - 20 of 70
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)