What does the following code output?
```java
class A {
public void printMessage() {
System.out.println("Class A");
}
}
class B extends A {
}
public class Main {
public static void main(String[] args) {
B b = new B();
b.printMessage();
}
}
```
Class A
Identify the error in the following code:
```java
class Animal {
}
class Dog extends Animal, Pet {
}
```
Java does not support multiple inheritance of classes. A class can only extend one class. Remove either 'Animal' or 'Pet'.
What does the following code output?
```java
class Vehicle {
public String modelName = "Generic Vehicle";
}
class Car extends Vehicle {
public String modelName = "Car Model";
public void displayModel() {
System.out.println(modelName);
}
public static void main(String[] args) {
Car myCar = new Car();
myCar.displayModel();
}
}
```
Car Model
Complete the code snippet to make 'Dog' a subclass of 'Animal':
```java
class Animal {
}
class Dog ______ Animal {
}
```
extends
What does the following code output?
```java
class Parent {
public void display() {
System.out.println("Parent class");
}
}
class Child extends Parent {
public void display() {
System.out.println("Child class");
}
}
public class Main {
public static void main(String[] args) {
Parent obj = new Child();
obj.display();
}
}
```
Child class
Identify the error in the following code:
```java
class A {
private int x = 5;
}
class B extends A {
public void printX() {
System.out.println(x);
}
}
```
The instance variable 'x' in class 'A' is private and cannot be accessed directly from subclass 'B'.
What does the following code output?
```java
class Animal {
public void makeSound() {
System.out.println("Generic animal sound");
}
}
class Dog extends Animal {
public void makeSound() {
System.out.println("Woof!");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog();
myAnimal.makeSound();
}
}
```
Woof!
Complete the following code to call the superclass's constructor:
```java
class A {
public A(int x) {
}
}
class B extends A{
public B(int x){
______;
}
}
```
super(x);
What does the following code output?
```java
class A {
public A() {
System.out.println("A");
}
}
class B extends A {
public B() {
System.out.println("B");
}
}
public class Main {
public static void main(String[] args) {
B b = new B();
}
}
```
A
B
Identify the error in the following code:
```java
class Animal {
public final void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
public void eat() {
System.out.println("Dog is eating");
}
}
```
The method 'eat()' in class 'Dog' cannot override the final method 'eat()' in class 'Animal'.
What is the primary benefit of using inheritance?
Code reusability. Subclasses inherit methods and instance variables from superclasses, reducing code duplication.
Why is inheritance considered a pillar of object-oriented programming?
Because it promotes abstraction and code organization by establishing hierarchical relationships between classes.
How does inheritance relate to the 'is-a' relationship?
A subclass 'is-a' more specific type of its superclass (e.g., 'APSubject is-a SchoolSubject').
What is the restriction on the number of superclasses a Java class can inherit from?
A Java class can only inherit from one superclass (single inheritance).
What is the role of polymorphism in OOP?
It allows objects of different classes to be treated as objects of a common type (usually a superclass), enabling flexibility and code extensibility.
How does method overriding contribute to polymorphism?
It allows subclasses to provide their own specific implementations of methods defined in the superclass, enabling different behaviors for the same method call.
Explain the concept of code reusability in inheritance.
Subclasses inherit the properties and methods of their superclasses, avoiding the need to rewrite the same code in multiple classes.
What is the significance of the Object class in Java's inheritance hierarchy?
It is the root of the class hierarchy. Every class in Java directly or indirectly inherits from the Object class.
What is the purpose of creating subclasses?
To create more specialized classes that inherit and extend the functionality of a more general superclass.
What is the difference between inheritance and composition?
Inheritance is an 'is-a' relationship, while composition is a 'has-a' relationship. Inheritance creates a tight coupling between classes, while composition promotes loose coupling.
What is the definition of Inheritance?
A mechanism where one class (subclass) acquires the properties (methods and instance variables) of another class (superclass).
What is the definition of a Superclass?
The class whose properties are inherited by another class.
What is the definition of a Subclass?
The class that inherits properties from another class (the superclass).
What is the definition of Polymorphism?
The ability of an object to take on many forms. Allows an object to be treated as an instance of its own class or its parent class.
What is the definition of Object-Oriented Programming (OOP)?
A programming paradigm based on the concept of 'objects', which contain data and code to manipulate that data.
What is the definition of Method Overriding?
When a subclass provides a specific implementation for a method that is already defined in its superclass.
What is the definition of the 'extends' keyword in Java?
A keyword used in Java to indicate that a class inherits from another class.
What is the definition of the 'super' keyword in Java?
A keyword used to access members of the superclass from within a subclass.
What is the Diamond Problem?
A problem that arises in multiple inheritance where a class inherits from two classes that have a common ancestor, leading to ambiguity in method resolution.
What is Dynamic Typing?
Type checking is done during run-time.
What is Static Typing?
Type checking is done during compile-time.