|
JAVA Interview Questions 11 - 15
|
Back to the list of all Java Interview Questions
|
|
Question 11: How to call the superclass constructor?
|
|
If a class called “SpecialPet” extends your “Pet” class then you can
use the keyword “super” to invoke the superclass’s constructor. E.g.
|
|
public
SpecialPet(int id)
{
super(id);
//must be the very first statement in the constructor.
} |
|
To call a regular method in the super class use: “super.myMethod( );”. This can be called at any line. Some
frameworks based on JUnit add their own initialization code, and not only do they need to remember to invoke
their parent's setup() method, you, as a user, need to remember to invoke theirs after you wrote your initialization
code:
|
|
public
class DBUnitTestCase
extends TestCase
{
public
void setUp()
{
super.setUp();
// do my own initialization
}
} |
|
|
|
public
void cleanUp() throws Throwable
{
try
{
… // Do stuff
here to clean up your object(s).
}
catch
(Throwable t) {}
finally
{
super.cleanUp();
//clean up your parent class. Unlike constructors
// super.regularMethod()
can be called at any line.
}
} |
|
|
Question 12: What are the advantages of Object Oriented Programming Languages (OOPL)?
|
The Object Oriented Programming Languages directly represent the real life objects like Car, Jeep, Account,
Customer etc.The features of the OO programming languages like polymorphism, inheritance and
encapsulation make it powerful.
[Tip: Remember pie which, stands for Polymorphism, Inheritance and
Encapsulation are the 3 pillars of OOPL]
|
|
Question 13: How does the Object Oriented approach improve software development?
|
|
The Key Benefits are:
|
- Re-use of previous work: using implementation inheritance and object composition.
- Real mapping to the problem domain: Objects map to real world and represent vehicles, customers,
products etc: with encapsulation.
- Modular Architecture: Objects, systems, frameworks etc are the building blocks of larger systems.
The increased quality and reduced development time are the by-products of the key benefits discussed above.
If 90% of the new application consists of proven existing components then only the remaining 10% of the code
have to be tested from scratch.
|
|
Question 14: How do you express an ‘is a’ relationship and a ‘has a’ relationship or explain inheritance and composition?
|
|
The ‘is a’ relationship is expressed with inheritance and ‘has a’ relationship is expressed with composition. Both
inheritance and composition allow you to place sub-objects inside your new class. Two of the main techniques for
code reuse are class inheritance and object composition.
|
|
Inheritance is uni-directional. For example House is a Building. But Building is not a House. Inheritance uses
extends key word. Composition: is used when House has a Bathroom. It is incorrect to say House is a
Bathroom. Composition simply means using instance variables that refer to other objects. The class House will
have an instance variable, which refers to a Bathroom object.
|
|
Question 15: Which one to favor, composition or inheritance?
|
The guide is that inheritance should be only used when subclass ‘is a’ superclass.
- Don’t use inheritance just to get code reuse.
If there is no ‘is a’ relationship then use composition
for code reuse. Overuse of implementation inheritance
(uses the “extends” key word) can break all the subclasses, if
the superclass is modified.
- Do not use inheritance just to get polymorphism.
If there is no ‘is a’ relationship and all you want is polymorphism
then use interface inheritance with composition,
which gives you code reuse
|
Back to the list of all Java Interview Questions
|