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.
For Code Reusability.
For Method Overriding.
super
this
extent
extends
Answer: d.
Multiple inheritance (java doesn't support multiple inheritance).
Multilevel inheritance.
public class subclass extends superclass {
// all methods and variables declare here
}
class A {
int i;
void display() {
System.out.println(i);
}
}
class B extends A {
int j;
void display() {
System.out.println(j);
}
}
class inheritance_jbkdemo {
public static void main(String args[]){
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
Options :
0
1
2
Compilation Error
Answer : c.
Explanation : class A & class B 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_jbkdemo.java
$ java inheritance_jbkdemo 2
Inheritance can be implemented in JAVA using below two keywords :
extends
implements
extends is used for developing inheritance between two classes and two interfaces.
implements keyword is used to developed inheritance between interface and class.
Multilevel inheritance 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 cannot 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.
upper
super
this
None of the mentioned.
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 we cannot extend more than one classes, but a class can implement more than one interfaces.
Program:
interface A {
}
interface B {
}
class C implements A,B {
}
class A {
int i;
}
class B extends A {
int j;
void display() {
super.i = j + 1;
System.out.println(j + " " + i);
}
}
class inheritance_jbkdemo {
public static void main(String args[]){
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
Options :
2 2
3 3
2 3
3 2
Answer : c.
Explanation : None.
Output:
$ javac inheritance_jbk_demo.java
$ java inheritance_jbk_dmeo 2 3
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.
class A {
public int i;
private int j;
}
class B extends A {
void display() {
super.j = super.i + 1;
System.out.println(super.i + " " + super.j);
}
}
class inheritance_jbkdemo {
public static void main(String args[]){
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
Options :
2 2
3 3
Runtime Error
Compilation Error
Answer : d.
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.
class A {
public int i;
public int j;
A() {
i = 1;
j = 2;
}
}
class B extends A {
int a;
B() {
super();
}
}
class super_use {
public static void main(String args[]){
B obj = new B();
System.out.println(obj.i + " " + obj.j)
}
}
Options :
1 2
2 1
Runtime Error
Compilation Error
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
Options :
public member
private member
protected member
static member
Answer : b.
Explanation : A class member declared protected becomes private member of subclass.
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.
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.
class A {
public int i;
protected int j;
}
class B extends A {
int j;
void display() {
super.j = 3;
System.out.println(i + " " + j);
}
}
class Output_demo {
public static void main(String args[]){
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
Options :
12
21
13
31
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_demo.java
$ java Output_demo
1 2
That would not be a problem as both are specifying the contract that implement class has to follow. If class C_JBK implement interface A_JBK & interface B_JBK then Class C_JBK thing I need to implement print() because of interface A_JBK then again Class think I need to implement print() again because of interface B_JBK, it sees that there is already a method called test() implemented so it's satisfied.
Overriding method cannot 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.
In case of subclass and superclass define in different package, we can override only those method which have public or protected access.
We cannot override any private method because private methods can not be inherited and if method cannot be inherited then method can not be overridden.