Java is Object Oriented Programming Language and widely
used language for web development or stand alone applications today. Its
so popular due its feature "Write once, run anywhere". This simply
means that the java program that you write can run on multiple JVMs and
Operating Systems such as Windows, Linux, Mac etc. Java is architecture
neutral. The language has been evolving day by day adding new features,
improving existing implementations to help programmer write better java
code with ease. Today there are many frameworks developed basing the
language and are popular for writing 3-tier applications that are
scalable. Before going on to all of that, lets understand the basics of
object oriented concepts.
Encapsulation
"The
wrapping up of data and functions into a single unit" is what is called
an encapsulation. You can also define encapsulation as "process of
hiding implementation details of an object". Once
an object is encapsulated, its implementation details are not
immediately accessible any more. Instead they are packaged and are only
indirectly accessed via the interface of the object achieved by access
modifiers.
Abstraction
"Showing
the essential and hiding the non-essential" is what is called
abstraction. As an example, List interface is an abstraction. It only
defines the general behavior of a list and leaves it open how it gets
implemented.
Inheritance
An
object is able to inherit(re-use) characteristics from another object
or an object is able to pass on its state and behavior to its children.
Inheritance provides a powerful and natural mechanism for organizing and
structuring your software. When use inheritance? When we want to create
an entirely new class but wish to borrow a group of existing attributes
or methods resident in an existing abstract or super class instead of
re-inventing the wheel. Inheritance allows us to treat a subclass as if
it was a super class. A subclass can only extend one super class. There
can be any number of sub classes for a super class.
Polymorphism
"Ability
to appear in many forms". Ability of a reference variable to change
behavior according to what object instance it is holding.
Example:
class Person{
public String getName(){ }
}
class Student extends Person{
public String getName(){
return "Student Name";
}
}
class Employee extends Person{
public String getName(){
return "Employee Name";
}
}
Now,
Student studentObj = new Student();
Employee employeeObj = new Enployee();
Person personRef = studentObj;
personRef.getName(); //Student Name gets printed
public static printName(Person p){
p.getName(); // getName() of actual object passed is called
}
Characteristics
of polymorphism are (a) Simplicity - Easy to understand the code and
(b) Extensibility - Other sub classes can be added to the family of
types. Objects of these sub classes work with existing code.
There are 3 forms of polymorphism in Java
- Method Overriding: Method of sub classes override the methods of a super class
- Method overriding (Implementation) of abstract methods. Methods of subclass implement abstract methods of an abstract class.
- Method overriding through Java Inheritance - Methods of a concrete class implement the methods of the interface.
Compile time polymorphism is achieved by method overloading (Early binding)
Runtime polymorphism is achieved by method overriding (Late binding)
Rules of method overloading:
- Arguments ---> Must cahnge
- Return type ---> Can change
- Exceptions ---> Can change
- Invocations ---> Which method to call is based on the reference type at compile time
I will be writing more on Java Technologies in my future articles.