Inheritance
- What is the output of this program?
class N
{
public int K;
public int L;
N()
{
K = 5;
L = 2;
}
}
class M extends N
{
int p;
M()
{
super();
}
}
public class inheritance_super_use
{
public static void main(String args[])
{
M obj = new M();
System.out.println(obj.K + " " + obj.L);
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
Keyword super is used to call constructor of class N by constructor of class M. Constructor of a initializes K & L to 5 & 2 respectively.
- What is not type of inheritance?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
Inheritance is way of acquiring attributes and methods of parent class. Java supports hierarchical inheritance directly.
- Using which of the following, multiple inheritance in Java can be implemented?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
Multiple inheritance in java is implemented using interfaces. Multiple interfaces can be implemented by a class.
- What is the outcome of below code box class?
class Shape
{
public int area()
{
return 0;
}
}
class Square extends Shape
{
public int area()
{
return 5;
}
}
class Rectangle extends Shape
{
public int area()
{
return 9;
}
}
public class box
{
public static void main(String[] args)
{
Shape obj = new Square();
Shape obj0 = new Rectangle();
obj = obj0;
System.out.println(obj.area());
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
The method of the child class object is accessed. When we reassign objects, the methods of the latest assigned object are accessed.
- In order to restrict a variable of a class from inheriting to subclass, how variable should be declared?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
By declaring variable private, the variable will not be available in inherited to subclass.