Inheritance Interview Questions - Part1

  • Inheritance is one of the oops concepts in java.inheritance is concept of getting properties of one class object to another class object.
  • Inheritance represents the IS-A relationship,also known as parent-child relationship.
1.For Code Re usability.
2.For Method Overriding.
1.Multiple inheritance( java doesn't support multiple inheritance).
2.Multilevel inheritance.
                            
public class subclass extends superclass{
//all methods and variables declare here
}
Answer: c

Explanation: class A & class B both contain display() method, class B inherits class A, when display() method is called by object of class B, display() method of class B is executed rather than that of Class A.

output:

$ javac inheritance_demo.java
$ java inheritance_demo 2
• Inheritance can be implemented in JAVA using below two keywords:

1.extends
2.implements

• extends is used for developing inheritance between two classes and two interfaces. • implements keyword is used to developed inheritance between interface and class.
Getting the properties from one class object to another class object level wise with different priorities.
• The concept of Getting the properties from multiple class objects to sub class object with same priorities is known as multiple inheritance.

• In multiple inheritance there is every chance of multiple properties of multiple objects with the same name available to the sub class object with same priorities leads for the ambiguity. also known as diamond problem. one class extending two super classes.

• Because of multiple inheritance there is chance of the root object getting created more than once.

• Always the root object i.e object of object class hast to be created only once.

• Because of above mentioned reasons multiple inheritance would not be supported by java.

• Thus in java a class can not extend more than one class simultaneously. At most a class can extend only one class.
By declaring that member as a private. Because, private members are not inherited to sub classes
Answer: b
Explanation: whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.
Using interfaces java can support multiple inheritance concept in java. in java can not extend more than one classes, but a class can implement more than one interfaces.

Program:


interface A{
}
interface B{
}
class C implements A,B{
}
No,A class can't extend itself.
Super class field will be hidden in the sub class. You can access hidden super class field in sub class using super keyword.
No, Java doesn't support multiple inheritance. Interfaces doesn't facilitate inheritance and hence implementation of multiple interfaces doesn't make multiple inheritance.
You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of it's superclasses. One of the main reasons is because you probably don't want to override the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language.
d)Compilation Error

Explanation: class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it. output: $ javac inheritance.java Exception in thread “main” java.lang.Error: Unresolved compilation problem: The field A.j is not visible
                            
class A {
void test() {
System.out.println("test() method");
}
}

class B {
void test() {
System.out.println("test() method");
}
}

Suppose if Java allows multiple inheritance like this,
class C extends A, B {
}

A and B test() methods are inheriting to C class. So which test() method C class will take? As A & B class test() methods are different , So here we would Facing Ambiguity.
No, only classes in java are inherited from Object class. Interfaces in java are not inherited from Object class. But, classes which implement interfaces are inherited from Object class.
Answer: a

Explanation: Keyword super is used to call constructor of class A by constructor of class B. Constructor of a initializes i & j to 1 & 2 respectively.

output:

$ javac super_use.java
$ java super_use
1 2
1.this is a reference to the current object in which this keyword is used whereas super is a reference used to access members specific to the parent Class.

2.this is primarily used for accessing member variables if local variables have same name, for constructor chaining and for passing itself to some method whereas super is primarily used to initialize base class members within derived class constructor.
Answer: a

Explanation: Both class A & B have member with same name that is j, member of class B will be called by default if no specifier is used. I contains 1 & j contains 2, printing 1 2.

output:

$ javac Output.java
$ java Output
1 2
That would not be a problem as both are specifying the contract that implement class has to follow. If class C implement interface A & interface B then Class C thing I need to implement print() because of interface A then again Class think I need to implement print() again because of interface B, it sees that there is already a method called test() implemented so it's satisfied.
1. Overriding method can not be more restrictive than the overridden method.

reason : in case of polymorphism , at object creation jvm look for actual runtime object. jvm does not look for reference type and while calling methods it look for overridden method.

If by means subclass were allowed to change the access modifier on the overriding method, then suddenly at runtime—when the JVM invokes the true object's version of the method rather than the reference type's version then it will be problematic

2. In case of subclass and superclass define in different package, we can override only those method which have public or protected access.

3. We can not override any private method because private methods can not be inherited and if method can not be inherited then method can not be overridden.