MCQs – Java Inheritance


Q) Inheritance relationship in Java language is
  1. Association
  2. Is-A
  3. Has-A
  4. None

Answer: 2


Q) Advantage of inheritance in java programming is/are
  1. Code Re-usability
  2. Class Extendibility
  3. Save development time
  4. 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?
  1. A class can extend only one class but can implement many interfaces
  2. An interface can extend many interfaces
  3. An interface can implement another interface
  4. An interface can implement a class

Answer: 1 and 2


Q) Which class cannot be sub classed?
  1. final class
  2. object class
  3. abstract class
  4. 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();

	}

}

Related Posts