T/F A subclass can directly access private fields of its superclass.
False
What does the @Override annotation do, what happens if it is omitted?
The @Override annotation ensures that a method is correctly overriding a superclass method. If omitted, the code will still work but it may allow accidental method overloading instead of method overriding.
T/F A constructor of a subclass calls the constructor of its superclass if super() is not explicitly written.
True
What will happen when you try to inherit from a final class and why does that happen?
A compilation error will be thrown because final classes cannot be extended. cannot inherit from final <classname>
class Animal {
void sound() {
System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() {System.out.println("Dog barks"); }
void bark() { System.out.println("Woof!");}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
// a.bark();
}
}
What is the output of this code? If an error is thrown, state the exact error (write on whiteboard).
Dog barks because Polymorphism allows the overridden sound method in Dog to be called, but a.bark() will cause a compilation error because bark is not defined in Animal. cannot find symbol
What will happen if a superclass does not have a no-argument constructor and a subclass does not call super explicitly and why?
Compilation error because subclass constructor needs to explicitly call a superclass constructor if the superclass does not have a no-argument constructor (constructor <classname> in class <classname> cannot be applied to given types)
What is the process of simplifying complex systems by focusing on essential features while hiding unnecessary details?
A. Abstraction B. Inheritance C. Polymorphism D. Encapsulation
Abstraction
Which of the following can be overriden in a subclass? (Select all that apply) a. Static methods b. Instance methods c. Private methods
Only instance methods can be overriden. Static methods are hidden, and private methods are not inherited.
class A {
A() { System.out.println("A"); }
}
class B extends A {
B() { super(); System.out.println("B"); }
}
class C extends B {
C() { System.out.println("C"); }
}
public class Main {
public static void main(String[] args) {
C c = new C();
}
}
What is the output?
A B C because constructors are called in a chain starting from the topmost superclass
class A {
int x = 10;
void printX() {System.out.println("A's x: " + x); }}
class B extends A {
int x = 20;
void printX() { System.out.println("B's x: " + x); }}
public class Main {
public static void main(String[] args) {
A obj = new B();
obj.printX();
System.out.println("x: " + obj.x);
}
}
What will happen if you compile and run the following code?
A: B's x: 20
x: 10
class Parent {
static void display() {
System.out.println("Parent display");
}
}
class Child extends Parent {
static void display() { System.out.println("Child display"); }
}
public class Main {
public static void main(String[] args) {
Parent obj = new Child();
obj.display();
}
}
Predict the output of the above code.
Parent display
class Parent {
Parent(int x) {
System.out.println("Parent: " + x);
}
}
class Child extends Parent {
Child() {
________(42);
System.out.println("Child");
}
}
Fill in the blank so that the code compiles.
super