click below
click below
Normal Size Small Size show me how
Java (Chapter 11)
Inheritance
| Question | Answer |
|---|---|
| [True/False] A subclass is a subset of a superclass. | False. A subclass is an extension of a superclass and normally contains more detail information than its superclass. |
| What keyword do you use to define a subclass? | The “extends” keyword is used to define a subclass that extends a superclass. |
| What is single inheritance? What is multiple inheritance? Does Java support multiple inheritance? | Single inheritance allows a subclass to extend only one superclass. Multiple inheritance allows a subclass to extend multiple classes. Java does not allow multiple inheritance. |
| How does a subclass invoke its superclass’s constructor? | A subclass can explicitly invoke a superclass’s constructor using the “super” keyword. |
| [True/False] When invoking a constructor from a subclass, its superclass’s no-arg constructor is always invoked. | False. If a subclass’s constructor explicitly invokes a superclass’s constructor, the superclass’s no-arg constructor is not invoked. |
| [True/False] You can override a private method defined in a superclass. | False. You can only override accessible instance methods. |
| [True/False] You can override a static method defined in a superclass. | False. You can only override accessible instance methods. |
| How do you explicitly invoke a superclass’s constructor from a subclass? | Use “super()” or “super(<args>)”. This statement must be the first in the constructor in the subclass. |
| How do you invoke an overridden superclass method from a subclass? | Use super.method() or super.method(args). |
| Explain the difference between method overloading and method overriding. | Method overloading defines methods of the same name in a class. Method overriding modifies the methods that are defined in the superclasses. |
| If a method in a subclass has the same signature as a method in its superclass with the same return type, is the method overridden or overloaded? | It is a method overridden. |
| If a method in a subclass has the same signature as a method in its superclass with a different return type, will this be a problem? | It will be a problem if the return type is not a compatible return type. |
| If a method in a subclass has the same name as a method in its superclass with different parameter types, is the method overridden or overloaded? | It is method overloading. |
| What is the benefit of using the @Override annotation? | It forces the compiler to check the signature of the overridden method to ensure that the method is defined correctly. |
| What are the three (3) pillars of object-oriented programming (OOP)? What is polymorphism? | Encapsulation, Inheritance, and Polymorphism. In simple terms, polymorphism means that a variable of a super-type can refer to a sub-type object. |
| What is dynamic binding? | A method may be implemented in several classes along the inheritance chain. The JVM decides which method is invoked at runtime. This is known as dynamic binding. |
| Describe method matching. | The declared type of the reference variable decides which method to match at compile time. The compiler finds a matching method, IAT parameter type, # of parameters, and order of parameters at compile time. |
| Describe method binding. | A method may be implemented in several subclasses. The JVM dynamically binds the implementation of the method at runtime, decided by the actual class of the object referenced by the variable. |
| Can you assign new int[50], new Integer[50], new String[50], or new Object[50], into a variable of Object[] type? | new int[50] CANNOT be assigned to into a variable of Object[] type, but new Integer[50], new String[50], or new Object[50] are FINE. |
| [True/False] You can always successfully cast an instance of a subclass to a superclass. | True. For example, casting apple to fruit is fine. |
| [True/False] You can always successfully cast an instance of a superclass to a subclass. | False. For example, casting fruit to apple is not always successful unless a fruit is an apple. |
| 11.13.1. Write statements that create a MyStack and add number 11 to the stack. | MyStack stack = new MyStack(11); stack.push(11); // 11 is auto boxed into new Integer(11) |
| 11.14.1. What modifier should you use on a class so that a class in the same package can access it, but a class (including a subclass) in a different package cannot access it? | default visibility modifier |
| 11.14.2. What modifier should you use so that a class in a different package cannot access the class, but its subclasses in any package can access it? | protected |
| 11.15.1. How do you prevent a class from being extended? How do you prevent a method from being overridden? | Use the “final” keyword. |
| Object-oriented programming allows you to derive new classes from existing classes. This is called ________. | Inheritance |
| [True/False] A subclass is usually extended to contain more functions and more detailed information than its superclass. | True. |
| [True/False] “class A extends B” means A is a subclass of B. | True |
| What modifier should you use on the members of a class so that they are not accessible to another class in a different package, but are accessible to any subclasses in any package? | protected |
| The visibility of these modifiers increases in this order: | private; none (if no modifier is used); protected; and public |
| A class design requires that a particular member variable must be accessible by any subclasses of this class, but otherwise not by classes which are not members of the same package. What should be done to achieve this? | The variable should be marked protected. |
| [True/False] A method with no visibility modifier can be accessed by a class in a different package. | False. |
| [True/False] “final class A { }” cannot be extended. | True. |
| You can create an ArrayList using _________. | new ArrayList <> () |
| Invoking __________ removes all elements in an ArrayList x. | x.clear() |
| Suppose ArrayList x contains two strings [Beijing, Singapore]. Define a method that will cause the list to become [Beijing, Chicago, Singapore]. | x.add(1, “Chicago”) |
| Suppose ArrayList x contains two strings [Beijing, Singapore]. Define a method that will cause the list to become [Beijing]. | x.remove(“Singapore”) OR x.remove(1) |
| Suppose ArrayList x contains two strings [Beijing, Singapore]. Define a method that will cause runtime errors. | x.set(2, “New York”); x.get(2); x.remove(2) - there is no element at index 2. |
| Invoking ____________ returns the first element in an ArrayList x. | x.get(0) |
| Invoking ____________ returns the number of the elements in an ArrayList x. | x.size() |
| Object-Oriented programming allows you to derive new classes from existing classes. This is called _____. | inheritance |