Test Inheritance PDF
Test Inheritance PDF
Dashboard / Java / Inheritance, Polymorphism, Abstract class, Interface / Test Your Understanding - Inheritance
package mainpack ;
public class Employee{
package subordpack ;
public class PermanentEmployee extends Employee{
.....
}
//TemporaryEmployee.java
package subordpack ;
public class TemporaryEmployee extends Employee{
.....
}
For a child class that is residing in a package to access a variable of its parent that is residing in a different package,
the variable in the parent has to be declared "protected" so that it will be visible to all its children across packages. The
"protected" basicPay attribute is not accessed directly by the other classes in the subordpack, in our case.
Employee.java
package [mainpack];
public class Employee{
[protected] int basicPay;
}
/*PermanentEmployee.java*/
package [subordpack];
public class PermanentEmployee extends Employee{
.....
}
//TemporaryEmployee.java
package [subordpack];
public class TemporaryEmployee extends Employee{
.....
}
Question VIVEK KUMAR .
2 Constructor of the superclass can be invoked by from its subclass.
Correct
A default member of a super class in one package cannot be accessed in its own subclass which is in a
Mark 1.00 out of
1.00 different package.
A protected member of a super class in one package can be accessed by its own subclass which is
in a different package.
A default member of a super class in one package [cannot] be accessed in its own subclass which is in a different
package.
A protected member of a super class in one package [can] be accessed by its own subclass which is in a different
package.
Question
State True or False
3 Child class objects can be instantiated when the parent class constructor is protected
Correct
False
Question Say that there are three classes: Computer, AppleComputer, and IBMComputer. What are the likely relationships
4 between these classes
Correct
b. Computer is the super class, AppleComputer and IBMComputer are subclasses of computer
b. object of subclass referenced by super class type can invoke overridden sub class methods
c. object of subclass referenced by super class type can invoke super class methods
d. object of subclass referenced by super class type can access newly defined sub class variables
e. object of subclass referenced by super class type can invoke newly defined sub class methods
The correct answers are: object of subclass referenced by super class type can invoke super class methods, object of
subclass referenced by super class type can invoke overridden sub class methods, object of subclass referenced by
super class type can access super class variables
Question
public class ArithmeticOperation{
6 private void add(int operand1, int operand2)
Correct {
Mark 1.00 out of System.out.println(operand1 + operand2);
1.00 }
}
public class Addition extends ArithmeticOperation
{
public void show()
{
add(10, 12);
}
public static void main(String args[])
{
Addition ob = new Addition();
ob.show();
}
}
What will be the output of above code when compiled and executed?
Select one:
a. Runtime error as add method is not defined in MethodOverriding class
The method add cannot be invoked from anywhere since it's declared private
Select one:
a. 123
b. 321
c. 3
The first statement that always gets executed from within any constructor is super() which means the invocation of
super class no-parameterized constructor.
FourWheeler is the parent of Car and Car is the parent of Audi. The no-parameterized constructor call of Audi happens
from the driver class. Followed by the no-parameterized constructor call of Car. Followed by the no-parameterized
constructor call of FourWheeler. Hence, the output "123".
The correct answer is: 123
Question VIVEK KUMAR .
10. class FourWheeler {
8
11. private void display() { }
Correct
15. }
The visibility of a method can only be increased as we traverse down the hierarchy.
15. }
Which method at line 14, will correctly complete class Car?
Question VIVEK KUMAR .
What will be the output of the following program ?
9 class FourWheeler
Correct {
Mark 1.00 out of public FourWheeler()
1.00 {
System.out.println("Class FourWheeler");
}
}
class Car extends FourWheeler
{
public Car()
{
System.out.println("Class Car");
}
}
class Audi extends Car
{
public Audi()
{
super();
System.out.println("Class Audi");
}
}
class Driver
{
public static void main(String args[])
{
Audi cc=new Audi();
}
}
Select one:
a.
Class FourWheeler
Class Car
Class Audi
b.
Class Audi
Class Car
Class FourWheeler
c.
Compile Time Error
d.
Exception occurs
The first statement that always gets executed from within any constructor is super() which means the invocation of
super class no-parameterized constructor.
FourWheeler is the parent of Car and Car is the parent of Audi. The no-parameterized constructor call of Audi happens
from the driver class. Followed by the no-parameterized constructor call of Car. Followed by the no-parameterized
constructor call of FourWheeler. Hence, the output "Class Four WheelerClass Car Class Audi".
private FourWheeler getObject() {} Car getObject() {}
protected void getObject() {}
Java 5.0 onwards it is possible to have different return type for a overriding method in child class, but child’s return
type should be sub-type of parent’s return type. The visibility of a method can only be increased as we traverse down
the hierarchy.
[Default Access] is the most restrictive access modifier that will allow members of one class to have access to
members of another class in the same package.
Question
Given a method in a public class, private access modifier must be used to restrict access to that
12 method to only the other members of the same class.
Correct