Welcome

Sunday, 15 September 2013

What are object orientation principles?

According to object oriented paradigm, there are four principles namely:
  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance

Abstraction:
Hiding the complex details and presenting only the general necessary details is known as abstraction. For example, when you go to a car dealer to purchase a car, he/she explains you about the features of the car like: mileage, type of engine, color, details about interior and exterior etc. But, the car dealer never explains about how the engine is built, which nuts and bolts are used build the car etc. The later details are not useful for the buyer. Abstraction is like a concept/theory and encapsulation is the practical implementation of abstraction.

In java, abstraction can be implemented using abstract keyword and interfaces.

Encapsulation:
Wrapping or combining code (methods) and data (instance variables) together is known as encapsulation. Encapsulation provides abstraction. A common side effect of encapsulation is information hiding. For example, there is no need for a person to know about the internal working of a car to drive it. A person interacts with the car through its interface like driving wheel, gear, buttons etc.

In java, encapsulation is implemented using the class concept.
In java, data/information hiding is implemented using the access specifiers.

Polymorphism:
Simply can be defined as many forms or single interface multiple implementations. Polymorphism gives the ability for an object to perform multiple behaviors through a single interface. A real world example for polymorphism is the animal chameleon. Based on the environment, a chameleon can change its body color to save itself from the predators.

In java, polymorphism can be implemented using overloading and overriding.

Inheritance:
Defined as deriving properties and behavior from one class (parent) to another (child) class. For example, consider a general class laptop (parent). All the laptops will have common properties like a screen, keyboard, touchpad etc. and behavior like: laptop starts on the push of a power button.

Now, consider a sub class or child class of laptop like a gaming laptop. A gaming laptop (child class) will have all the properties and behavior of a laptop as mentioned above. In addition, the gaming laptop can have its own properties and behavior.

In java, abstraction can be implemented using the keywords extends and implements.

Note:
If in an interview, you are asked the question, what are the three object orientation principles, you can say:
Encapsulation, Polymorphism and Inheritance.

Saturday, 14 September 2013

Is java a pure object oriented programming language?

Short answer for is java a pure object oriented programming language? is yes.

Before java version 5 was released, java was not considered as a pure or full object oriented programming language because of the 8 primitive data types (int, char, float etc...). The problem with these primitive data types is, the values of these types are not objects. They are simply plain values. This is the reason why java was not considered a pure object oriented programming language.

But, in version 5, Java introduced the concept of auto boxing which means whenever a programmer uses a value of the primitive data type, it will be automatically converted to an object of its identical wrapper class behind the scenes. Due to this reason, from java version 5, it is called as a pure object oriented programming language.

Wednesday, 11 September 2013

What is java bytecode?

In java, after compilation, the java compiler generates an intermediate non-human readable code. This code file is know as bytecode. The extension of bytecode file is .class.

The bytecode generated by the java compiler is platform independent i.e this code can be executed on a machine with any operating system or any hardware. The bytecode is generated in such a way that when it runs on any machine it gives the same output. This bytecode is executed by the JVM

Q: Why Java is called as portable language or platform independent?
A: Due to the bytecode generated by the java compiler.

Q: Is JVM platform independent?
A: No. It is platform dependent.