What keyword is used to create a subclass in Java?
extends
What keyword is used to access a superclass's members from a subclass?
super
True or False: A class can inherit from multiple classes in Java.
False
When a subclass has a method with the same signature as a method in the parent class, it is said to _________ the parent method.
a. extend
b. override
c. overload
d. polymorph
override
What type of polymorphism is achieved by having multiple methods with the same name but different parameters?
a. method overriding
b. method overloading
c. class deriving
d. polytypism
method overloading
What type of polymorphism is achieved by a subclass providing a specific implementation of a method declared in its superclass?
a. method overriding
b. method overloading
c. class deriving
d. polytypism
method overriding
What is the process by which Java can determine which method to invoke when methods have been overridden?
a. compile-time polymorphism
b. overloading polymorphism
c. overriding polymorphism
d. runtime polymorphism
runtime polymorphism
Which keyword is used to add an interface class into another class?
What is implements?
When you call the super() in the constructor of the subclass, where does it have to be?
in the first line of the body of the constructor
Which of the following is true about inheritance in Java?
1) Private methods are final.
2) Protected members are accessible within a package and inherited classes outside the package.
3) Protected methods are final.
4) We cannot override private methods.
1, 2 and 4
class Base {public void Print() {
System.out.println("Base");} }
class Derived extends Base { public void Print() {
System.out.println("Derived");}}
class Main{public static void DoPrint( Base o ) {
o.Print();}
public static void main(String[] args) {
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
DoPrint(x); DoPrint(y); DoPrint(z);}}
Base
Derived
Derived
What is the ability to call a method on a parent class in a child class?
ex. speak() in a Pet class, without knowing whether the Pet object is really a Cat, Dog, AngryBird, etc.
polymorphism
class Base {
public void foo() { System.out.println("Base"); }}
class Derived extends Base {
private void foo() { System.out.println("Derived");}}
public class Main {
public static void main(String args[]) {
Base b = new Derived();
b.foo(); }}
Compiler Error
class Animal { public void makeSound() {
System.out.println("Animal sound"); }}
class Dog extends Animal {
@Override
public void makeSound() {System.out.println("Woof!"); }}
public class Test {
public static void main(String[] args) {
Animal animal = new Animal();
Animal dog = new Dog();
animal.makeSound();
dog.makeSound(); }}
Animal sound
Woof!
Which Java modifiers such as public, final, and static can be used in interface and abstract classes?
What is "Interface can only use the public modifier while abstract can use any"