Question:

In Java, which of the following keyword is used to create a subclass in the class definition?

Show Hint

The {extends keyword} allows one class to inherit properties and methods from another class in Java.
  • sub
  • extends
  • child
  • derived
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Step 1: Understanding inheritance in Java.

Inheritance is an important concept in Object-Oriented Programming that allows one class to acquire the properties and behaviors of another class. This helps in code reuse and improves program structure.

Step 2: Role of the extends keyword.

In Java, inheritance is implemented using the extends keyword. When a class extends another class, it becomes a subclass and inherits the attributes and methods of the parent class.

Example:

class Animal {
    void eat() {
        System.out.println("Animal eats");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks");
    }
}
Here, the Dog class inherits properties from the Animal class.

Step 3: Evaluation of options.

(A) sub: Incorrect. This keyword does not exist in Java.

(B) extends: Correct. Used to create a subclass.

(C) child: Incorrect. This term is used conceptually but not as a keyword.

(D) derived: Incorrect. This is a concept but not a Java keyword.

Step 4: Conclusion.

Therefore, the keyword used to create a subclass in Java is extends.

Final Answer: extends
Was this answer helpful?
0
0