1-Explain protected member access?
2-Explain how to invoke a superclass method from a subclass method for the case in which the subclass method overrides a superclass method and the case in which the subclass method does not override a superclass method
NOTE: Since you have not mentioned the language of your preference, I am providing the coding examples in Java.
1)
Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.
Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.
Example
The following parent class uses protected access control, to allow its child class override openSpeaker() method −
class AudioPlayer {
protected boolean openSpeaker(Speaker sp) {
// implementation details
}
}
class StreamingAudioPlayer {
boolean openSpeaker(Speaker sp) {
// implementation details
}
}
Here, if we define openSpeaker() method as private, then it would not be accessible from any other class other than AudioPlayer. If we define it as public, then it would become accessible to all the outside world. But our intention is to expose this method to its subclass only, that’s why we have used protected modifier.
2)
In order to invoke a superclass method from a subclass method for the case in which the subclass method overrides a superclass method, the super keyword is used. See the example below for more explanation.
Example:
public class CrunchifyObjectOverriding {
public static void main(String args[]) {
Company a = new Company(); // Company reference and object
Company b = new eBay(); // Company reference but eBay object
a.address();// runs the method in Company class
b.address();// Runs the method in eBay class
}
}
class Company {
public void address() {
System.out.println("This is Address of Crunchify Company...");
}
}
class eBay extends Company {
public void address() {
super.address(); // invokes the super class method
System.out.println("This is eBay's Address...");
}
}
OUTPUT
This is Address of Crunchify Company...
This is Address of Crunchify Company...
This is eBay's Address...
The case in which the subclass method does not override a superclass method, we can just call the method without the "super" keyword.
Example
public class CrunchifyObjectOverriding {
public static void main(String args[]) {
Company a = new Company(); // Company reference and object
Company b = new eBay(); // Company reference but eBay object
a.address();// runs the method in Company class
b.address();// Runs the method in eBay class
}
}
class Company {
public void streetAddress() {
System.out.println("36-A Street");
}
}
class eBay extends Company {
public void fullAddress() {
streetAddress(); // invokes the super class method
System.out.println("This is eBay's Address...");
}
}
1-Explain protected member access? 2-Explain how to invoke a superclass method from a subclass method for...
For Java, 1. What is an abstract method? How is an abstract method created? 2. What is an abstract class? 3. Can an object of an abstract class be instantiated? 4. Does a superclass have access to the members of subclass? Does a subclass have access to the members of the superclass? 5. How do you prevent a subclass from having access to a member of a superclass? 6. Given the following hierarchy: class Alpha{ … class Beta extends Alpha...
Question 83 (1 point) To create a subclass, use the ___ keyword. extends implements interface inherits O Question 84 (1 point) Which of the following is true regarding inheritance? A superclass can force a programmer to override a method in any subclass created from it. A superclass cannot prevent a programmer from overriding a method in any subclass created from it. When creating a subclass, all methods of the superclass must be overridden. When creating a subclass, no methods of...
How do you call the superclass constructor from its subclass?
In Java Which of the following statements declares Salaried as a subclass of payType? Public class Salaried implements PayType Public class Salaried derivedFrom(payType) Public class PayType derives Salaried Public class Salaried extends PayType If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method. False True When a subclass overloads a superclass method………. Only the subclass method may be called with a subclass object Only the superclass method...
What is the code for this in Java?
Assignment Inheritance Learning Objectives Declare a subclass that derives from a superclas:s ■ Demon "Declare a variable of the superclass type and assign it an instance of the subclass type strate polymorphic behavior Access the public members of the superclass type Notice how the overridden versions of the subclass type are called Notice how the subclass specific members are inaccessible "Create an array of superclass type and use a foreach loop to...
Question 1 If a method is marked as protected internal, who can access it? Classes that are both in the same assembly and derived from the declaring class. Only methods that are in the same class as the method in question. Internal methods can only be called using reflection. Classes within the same assembly, and classes derived from the declaring class Question 2 To avoid having to use fully qualified referenced classes, you could: Add a reference to the class....
1. Employees and overriding a class method The Java program (check below) utilizes a superclass named EmployeePerson (check below) and two derived classes, EmployeeManager (check below) and EmployeeStaff (check below), each of which extends the EmployeePerson class. The main program creates objects of type EmployeeManager and EmployeeStaff and prints those objects. Run the program, which prints manager data only using the EmployeePerson class' printInfo method. Modify the EmployeeStaff class to override the EmployeePerson class' printInfo method and print all the...
(1) ____ is the principle that allows you to apply your knowledge of a general category to more specific objects. a. Inheritance c. Encapsulation b. Polymorphism d. Override (2) When you create a class by making it inherit from another class, you are provided with data fields and ____ automatically. a. fonts c. class names b. methods d. arrays (3) By convention, a class diagram contains the ____ following each attribute or...
In the first exercise, you will override the add method in a subclass in order to improve its performance. In the second exercise, you will implement an Iterator for your new linked list class. Exercise 1 The add method of the linked list class discussed during lecture performs in O(N) time. What can be done to improve its performance to O(1)? What are the boundary cases? Define a new class that inherits from the CS20bLinkedList class introduced during the lecture....
JAVA Question 1 When implementing a method, use the class’s set and get methods to access the class’s ________ data. a. public. b. private. c. protected. d. All of the above. Question 2 Set methods are also commonly called ________ methods and get methods are also commonly called ________ methods. a. query, mutator. b. accessor, mutator. c. mutator, accessor. d. query, accessor. Question 3 Composition is sometimes referred to as a(n) ________. a. is-a relationship b. has-a relationship c. many-to-one...