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!!

Wednesday, January 9, 2008

Core Java - I

What is an interface?

interface Runnable() {
abstract void run();
}

• Interface is completely abstract.
• No partial implementation.
• Only method signatures and constants are declared.

• All constants are implicitly static final.
• All methods are implicitly abstract.
• Treat it like a function pointer table.
• A class can implement one or more interfaces.


Benefits of Interface?

• Declaring Methods or constants that one or more classes are expected to implement.
• Capturing similarities between unrelated classes without forcing a class relationship.
• Determining an objects programming interface without revealing the actual body of the class.

Abstract class?

• An abstract class is a class that is declared abstract—it may or may not include abstract methods.
• Abstract classes cannot be instantiated.
• The purpose of abstract class is to be subclassed.
• It can have zero abstract methods, and still be declared abstract.
• If even one method is abstract the whole class must be declared abstract
• An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract void moveTo(double LocX, double LocY);
• When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.

Abstract Classes versus Interfaces?

• Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods.
• Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation.
• If an abstract class contains only abstract method declarations, it should be declared as an interface instead.
Multiple interfaces can be implemented by classes (similar or dissimilar) anywhere in the class hierarchy, whether or not they are related to one another in any way. Think of Comparable or Cloneable, for example.
• By comparison, abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).

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.