Object Oriented Programming Interview Questions & Answers
Object Oriented Programming Interview Questions & Answers
public class X{
}
public class X
{
public void do()
{
}
}
Public Class Y
{
X x=new X();
x.do();
interface R
{
void do();
}
class X implements R
{
void do()
{
Do it in a X way
}
}
class Y implements R
{
void do()
{
Do it in a Y way
}
}
class App
{
R ref;
public App(R ref)
{
this.ref=ref;
}
}
}
Good to know:
1. composition is easily achieved at runtime
while inheritance provides its features at
compile time
class FourWheeler
{
void start()
{
// some default behavior
}
void stop()
{
// some default behavior
}
}
interface Engine
{
void start();
void stop();
}
class MarutiEngine implements Engine
{
@Override
public void start()
{
// maruti specifics
}
@Override
public void stop()
{
// maruti specifics
}
}
class ToyotaEngine implements Engine
{
@Override
public void start()
{
// Toyota specifics
}
@Override
public void stop()
{
// Toyota specifics
}
}
class Car extends FourWheeler
{
private Engine engine; // program to
interface
private String type;
public Car(String type)
{
this.type=type;
engine=new MarutiEngine();
}
void start()
{
super.start();
engine.start(); // black-box reuse
}
void stop()
{
super.stop();
engine.stop(); // black-box reuse
}
}
public class Demo1
{
public static void main(String[]
args)
{
Car mycar=new Car("Maruti");
mycar.start();
// some more statements
mycar.stop();
}
}
Account
getRateOfInterest() //
method
SavingAccount CurrentAccount
FixedDeposit RecurringDeposit // overriding
“getRateOfInterest()” method.
Example 2:-
An abstract class called "Animal".
1. public class Samsumg : Mobile
2. {
3. public void GetWIFIConnection(
)
4. {
5. Console.WriteLine("WIFI co
nnected");
6. }
7.
8. //This is one mwthod which sho
ws camera functionality
9. public void CameraClick()
10. {
11. Console.WriteLine("Camera
clicked");
12. }
13.
14. //This is one overloaded metho
d which shows camera functionality as
well but with its camera's different
mode(panaroma)
15. public void CameraClick(string
CameraMode)
16. {
17. Console.WriteLine("Camera
clicked in " + CameraMode + " Mode");
18. }
19. }
Perform()
{
Read() // of FileReader // tight
coupling
Write() // of FileWriter // tight coupling
}
}
drawbacks:
• Tight coupling- if base class ( FileReader or
FileWriter) is changed, sub class
(MyApplication) will break.
• inheritance breaks encapsulation. white-box
reuse. That is ,with inheritance, the parent class
implementation is often visible to the subclasses.
e.g.
interface Reader
{
Void read();
}
Interface Writer
{
Void write();
}
Class FileReader implements Reader
{
Void read()
{
Code to read from file
}
}
Class FileWriter implements Writer
{
Void write()
{
Code to write to file
}
}
Class MyApplication
{
// program to interface, enables loose
coupling
Reader ref1;
Writer ref2;
Public MyApplication(Reader
ref1,Writer ref2)
{
This.ref1=ref1;
This.ref2=ref2;
}
Void perform()
{
Ref1.read(); // late binding
Ref2.write(); // late binding
}
}
Advantages:
• black-box reuse as it does not break
encapsulation. MyApplication knows only
selected functionalities from “Reader” and
“Writer”.
• Loose coupling, program to interface.
During runtime any implementations
(such as “FileReader” or
“SocketReader” and “FileWriter” or
“SocketWriter”) can be passed to
“Reader” or “Writer” respectively and
“read()” method can be invoked on it
polymorphically.
class Traveler
{
Car c=new Car();
void startJourney()
{
c.move();
}
}
class Car
{
void move()
{
// logic...
}
}
class Traveler
{
Vehicle v;
public void setV(Vehicle v)
{
this.v = v;
}
void startJourney()
{
v.move();
}
}
//=========================Interface======
==============================
Interface Vehicle
{
void move();
}
//====================Multiple class
implement vehicle interface.
First class====
class Car implements Vehicle
{
public void move()
{
// logic
}
}
//===================
Second class================
class Bike implements Vehicle
{
public void move()
{
// logic
}
}
Superhero
Superman
Animal
Bird
IFlying f=list.nextObj();
}
Composition is a special case of aggregation.
Composition is more restrictive. When there is a
composition between two objects, the composed
object cannot exist without the other object. This
restriction is not there in aggregation. eg: rooms in
a house, which cannot exist after the lifetime of the
house.
class House {
private Room room;
House(Room roomSpecs) {
room = new Room(roomSpecs);
}
What is Dependency?
When one class depends on another
because it uses that at some point in
time then this relationship is known as
Dependency. One class depends on
another if the independent class is a
parameter variable or local variable of a
method of the dependent class. A
Dependency is drawn as a dotted line
from the dependent class to the
independent class with an open
arrowhead pointing to the independent
class.
/*
The Animal class below corresponds to
class
A in our graphic above
*/
public:
};
int main( )
{
Liger lg ;
You can see that the only change we’ve made is to add the "virtual" keyword to the Tiger and
Lion class declarations. Now the Liger class object will have only one Animal subobject, and
the code below will compile just fine:
int main( )
{
Liger lg ;