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.
- Which two classes use the Shape class correctly?
A. public class Circle implements Shape
{
private int radius;
}
B. public abstract class Circle extends Shape
{
private int radius;
}
C. public class Circle extends Shape
{
private int radius;
public void draw();
}
D. public abstract class Circle implements Shape
{
private int radius;
public void draw();
}
E. public class Circle extends Shape
{
private int radius;
public void draw()
{
/* code here */
}
}
F. public abstract class Circle implements Shape
{
private int radius;
public void draw()
{
/* code here */
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
If one is extending any class, then they should use extends keyword not implements.
- What is the output of this program?
class N
{
int K;
void display()
{
System.out.println(K);
}
}
class M extends N
{
int L;
void display()
{
System.out.println(L);
}
}
public class inheritance_Example
{
public static void main(String args[])
{
M object = new M();
object.K=5;
object.L=6;
object.display();
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
Class N & class M both contain display() method, class M inherits class N, when display() method is called by object of class M, display() method of class M is executed rather than that of Class N.
- What is the output of this program?
class N
{
int K;
}
class M extends N
{
int L;
void display()
{
super.K = L + 5;
System.out.println(K + " " + L);
}
}
public class inheritance_Example
{
public static void main(String args[])
{
M object = new M();
object.K=4;
object.L=3;
object.display();
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
Output: 8 3
- Which of these is correct way of inheriting class N by class M?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
class M extends N{}