Inheritence(Java Program ) Questions and Answers
Explanation:-
Answer: Option C. -> "X extends Y" is correct if X and Y are either both classes or both interfacesA is incorrect because classes implement interfaces, they don't extend them. B is incorrect because interfaces only "inherit from" other interfaces. D is incorrect based on the preceding rules.
Question 2. What is the result of compiling and running the following code?
class Base{
public Base(){
System.out.print("Base");
}
}
public class Derived extends Base{
public Derived(){
this("Examveda");
System.out.print("Derived");
}
public Derived(String s){
System.out.print(s);
}
public static void main(String[] args){
new Derived();
}
}
class Base{
public Base(){
System.out.print("Base");
}
}
public class Derived extends Base{
public Derived(){
this("Examveda");
System.out.print("Derived");
}
public Derived(String s){
System.out.print(s);
}
public static void main(String[] args){
new Derived();
}
}
Explanation:-
Answer: Option C. -> BaseExamvedaDerived1. new Derived(); statement executes and invoke the non-parametrized constructor of derived class i.e.
public Derived();
2. As Derived class is a subclass of class Base so super(); executes and calls the super class constructor and prints "Base".
3. After that
this("Examveda"); executes and call the parametrized constructor
public Derived(String s); of Derived class as this always refer to the current object. So, it prints "Examveda".
4. Lastly the print statement executes and prints "Derived"
Hence output is BaseExamvedaDerived.
Explanation:-
Answer: Option C. -> 123Explanation:-
Answer: Option C. -> 3 and 5
Question 6. Determine output:
class A{
public void printName(){
System.out.println("Name-A");
}
}
class B extends A{
public void printName(){
System.out.println("Name-B");
}
}
class C extends A{
public void printName(){
System.out.println("Name-C");
}
}
1. public class Test{
2. public static void main (String[] args){
3. B b = new B();
4. C c = new C();
5. b = c;
6. newPrint(b);
7. }
8. public static void newPrint(A a){
9. a.printName();
10. }
11. }
class A{
public void printName(){
System.out.println("Name-A");
}
}
class B extends A{
public void printName(){
System.out.println("Name-B");
}
}
class C extends A{
public void printName(){
System.out.println("Name-C");
}
}
1. public class Test{
2. public static void main (String[] args){
3. B b = new B();
4. C c = new C();
5. b = c;
6. newPrint(b);
7. }
8. public static void newPrint(A a){
9. a.printName();
10. }
11. }
Explanation:-
Answer: Option C. -> Compilation fails due to an error on lines 5Reference variable can refer to any object of the same type as the declared reference OR can refer to any subtype of the declared type. Reference variable "b" is type of class B and reference variable "c" is a type of class C. So Compilation fails.
Question 7. What will be the result of compiling and executing the following program code?
class Vehicle{
public void printSound(){
System.out.print("vehicle");
}
}
class Car extends Vehicle{
public void printSound(){
System.out.print("car");
}
}
class Bike extends Vehicle{
public void printSound(){
System.out.print("bike");
}
}
public class Test{
public static void main(String[] args){
Vehicle v = new Car();
Bike b = (Bike) v;
v.printSound();
b.printSound();
}
}
class Vehicle{
public void printSound(){
System.out.print("vehicle");
}
}
class Car extends Vehicle{
public void printSound(){
System.out.print("car");
}
}
class Bike extends Vehicle{
public void printSound(){
System.out.print("bike");
}
}
public class Test{
public static void main(String[] args){
Vehicle v = new Car();
Bike b = (Bike) v;
v.printSound();
b.printSound();
}
}
Explanation:-
Answer: Option B. -> ClassCastException exception is thrown at runtime.Explanation:-
Answer: Option C. -> a b cExplanation:-
Answer: Option B. -> Name-BYou can not override private method , private method is not availabe in subclass . In this case printName() method a class A is not overriding by the printName() method of class B. printName() method of class B is a different method. So you can call printName() method of class B.
Explanation:-
Answer: Option A. -> Compilation FailsChoice A is the correct answer.
The code does not compile because the constructor of class A is declared as private. This creates a problem when the subclass constructor makes an implicit super() call to the parent class constructor at the time B is instantiated.
Since the code does not compile, all the other choices are incorrect. If the constructor of A had not been private, the output would have been 7.