Crack your interviews with these java interview questions
Today's Addition: Hi friends. Will be out from 19th Jan to 2nd Feb for the yearly vacation. No posts till then!!

Monday, January 7, 2008

Java Basics - OOPs

Q1 What is OOPS?

OOPs --> Object Oriented Programming is viewed as a collection of cooperating objects, instead of the traditional view in which a program may be seen as a group of tasks to the computer ("subroutines"). In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects.

The Fundamental concepts of OOP are as follows:
Class(Blueprint of object)
Object
Method
Inheritance
Encapsulation
Abstraction
Polymorphism

Q2 What is the difference between Structured Programming and OOP's?

• In world of structured programming
> Data lies separate from code.
> No data abstraction or info hiding.
> Programmer is responsible for organizing everything in to logical
units of code/data.
> No help from compiler/language for enforcing modularity.

Public class MyCar
{ int numDoors;
Float speed;
}
public void speedUp(Mycar m)
{.....}
public void speedDown(Mycar m)
{.....}


• OOP comes to the rescue
> Model everything as objects.
> Keep data near the relevant code.
> Avoid reinventing the wheel.
> Easier to understand, manage and maintain.
> Simplify testing and debugging.

Public class MyCar
{ int numDoors
float speed;
public void speedUp()
{.....}
public void speeDown()
{.....}
}


Q3 What is encapsulation?

Encapsulation is the mechanism that binds together code and data and keeps both safe from outside interference and misuse. This is achieved by making the fields of an object private and creating getters and setters for them which are public.

Q4 What is Inheritance?

• It is the capability of a class to use the properties and methods
of another class while adding its own functionality
• Represents is-a relationship
• In java this is done by using the extends keyword.
• Analogy
Just as inheritance in real life ( you get traits from your parent),
OOP inheritance is what you get from your parent (class)
• Why?
> Enhances ability to reuse code.
> Make designing much simpler and cleaner process.
• Advantage
> Efficiency, extensibility.

Q5 What are the different types of Inheritance?

• Types of Inheritance
> Single Inheritance
> Multiple Inheritance
> Multilevel Inheritance










Q6 What is polymorphism?

• It is the ability of an object to place a boundary around its
properties (data, methods).
• It seals the data and methods safely inside the 'capsule' of a class,
where it can be accessed only by trusted users (ie methods of the
class).
• Analogy
> While driving a car, user don't have to know the details of
cylinders, ignition etc.
• Why?
> No need to know implementation details of a component to use
it.
> Implementation can change without effecting any calling code.
• Advantage
> Provides Modularity, Code quality, ease of maintenance

Q7 Benefits of OOP's

• Promotes and facilitates software reuse.
> Save development time and cost
• Enables Easy Debugging
> Classes can be tested independently
> Reused objects have already been tested
• Facilitates interoperability
• Results in software that is easy to modify, extend
and maintain
• Reduces integration effort

Q8 what are OOP design principles?

• Identify interacting Objects.
• Characterize each object, establish attributes.
• Identify the data and operations within each object.
• Identify requests answered by each object.
• Identify services required of other objects.
• Establish relationships to other objects.
• Group similar objects together.
• Implement common super classes.
• Implement different objects as classes.

No comments: