27 March 2009

Java Job Interview questions in Object Oriented Programming

In the name of ALLAH,

One of the most important part of the Jobs Interview questions I found is the object-oriented programming, so I decided to write in this important topic.

Most of us know about Object-Oriented programming basics which are Encapsulation, Inheritance and Polymorphism and Abstraction. We will talk further about them and about other topics such as abstract classes, interfaces, multiple inheritance and many more in this post, and will continue our discussion in other posts.

In my point of view, Object orientation divided over 3 layers on understanding, first level of understanding is Object-oriented basics, that is what is object, what is a method, what is a class, what is abstraction, encapsulation, inheritance and polymorphism but this level is not enough to know actually on how to uses such great programming methodology, but you should go further one level higher to know about Object-Oriented principles; Object-oriented principles are principles that helps you to use object-oriented basics you learned from the first level in best usage. Example for these principles are : Program to interface not to implementation and also Your Class (Design) should be opened for extension but closed for modifications and your low level component shouldn't relay on higher level ones and many more (1) .
Myself found these principles more interesting that the third level which are the famous Design Patterns.

Design patterns(2) are applying for object oriented principles and basics and reuse for other expertise in order to make best usage from the object oriented designing methodology.
In this discussion we will talk about Basic Object oriented programming methodology what is object, class, interface, abstract class, final ( steal) steal class and many more.

So, lets start by the most famous questions in interviews,

Question) What is the deference between Abstract class and Interface ?
Abstract class is a class that have properties and methods that can be one of these two cases : class that represent abstract concept in the real domain such as the java.lang.Number class in the Java JDK. second case that when I have a domain problem such as I have many classes that shares common functionality and I want share these functionality and make the subclasses provide their own functionality in this case I made a super class an abstract and provide implementation for the share method and non-shared method I do it as abstract methods.
Example :

class Shape {
void calculateArea (){
// do some thing
// c...
abstract void display(); // share by name only, each subclass has its own implementation
class Rectangle extends Shape{
void display(){
// do some stuff....

Note that, an abstract class cannot be instantiated ( take an instance from It ) but can be subclassed (i.e. inherited ) as an opposite for the final class ( steal in MS.net terms ) that final class can be instantiated but cannot be inherited.
Abstract class is a regular class that have a constructor, methods and fields zero ore more of its methods are abstract. An abstract method should reside in an abstract class.
interface is other thing than abstract classes, interface found in Java for three reasons.
First to be such class that defined in .h files that found in C++, if you know about C++ you could remember that for a given class, there where two files for that class, a .h file and a .cpp file, the .h file is a file that define the interface of that class without any code implementations at all, first usage for the interfaces in Java is to be just an interface for a particular class for the programmer to implement. For an Employee class, there is an Employee interface that contains the prototype for its method.
The second usage is to be used as a contract, ( the interfaces are allows do that), for a class to have a particular service, he should fulfill that contract (implement that interface). Some service could be delivered just by adding the word implements InteraceXYZ (marker interfaces) and others by implementing complex methods.
Examples for the first type is the java.io.Serializable and java.lang.Clonable interfaces that provide services for your classes just be adding the implements keyword behind class declaration.
The other type requires you to provide an implementation for certain methods ( i.e. all methods in that interface ).
example : We have a store that contains items to be sold, these items are of different types, and we want to make sure that the buyer can bye them , here is the method of the buyer class :

public void buyProduct (Buyable product ){
// c....
double price = product.getPrice();

so for your products to get bought, all should implement the Buyable interface so that the the buyer can use the buying capabilities found is this product.
Public interface Buyable {
double getPrice(); // public abstract by default

each class who want sold, should impalements the getPrice method so the buyer can use to buy this product.
public class Computer implements Buyable {
public double getPrice(){
// c

Third usage is that the interface provides multiple interface (non-implementation ) inheritance.
Where a class can implement more than interface in case that the interfaces cannot have method impalement, so no confusion can come in you class.

(1) see O'Reilly Head First Design patterns
(2) see Design Patterns Reusable software components and O'Reilly Head First Design patterns
Post a Comment