Q) Inheritance relationship in Java language is
- Association
- Is-A
- Has-A
- None
Answer: 2
Q) Advantage of inheritance in java programming is/are
- Code Re-usability
- Class Extendibility
- Save development time
- All
Answer: 4
In fact, above benefits are related to each other. Frequent use of inheritance in java language is for deriving classes from existing classes that provides reusability. In simple terms, once we have written a class then it can be extended or sub classed without changing the code of base class. Both saves development time.
Q) Which of the following is/are true statements?
- A class can extend only one class but can implement many interfaces
- An interface can extend many interfaces
- An interface can implement another interface
- An interface can implement a class
Answer: 1 and 2
Q) Which class cannot be sub classed?
- final class
- object class
- abstract class
- child class
Answer: 1
Final class cannot be sub classed, meaning, it cannot be extended and we cannot derive any class form it. Below is the example of final class. if we execute this java code then compiler will flash an error as it is trying to extend final class.
final class Base {
public void m1(){
System.out.println("Final class()");
}
}
class Derived extends Base {
public void m2(){
System.out.println("Extended class");
}
}
public class Program {
public static void main(String[] args) {
Derived d = new Derived();
d.m2();
}
}