Wednesday, January 13, 2010

Interfaces, Encapsulation,Simplicity,Portability

Interfaces

Java supports only single inheritance, that is, each class can inherit attributes and methods of only one class. If you need to inherit properties from more than one source, then Java provides the concept of interfaces, which is equivalent to multiple inheritance. Interfaces are similar to classes. However, they define only the signature of the methods and not their implementations. The methods that are declared in the interface are implemented in the classes. Multiple inheritance occurs when a class implements multiple interfaces.

Encapsulation
Encapsulation describes the ability of an object to hide its data and methods from the rest of the world and is one of the fundamental principles of object-oriented programming. In Java, a class encapsulates the attributes, which hold the state of an object, and the methods, which define the actions of the object. Encapsulation enables you to write reusable programs. It also enables you to restrict access only to those features of an object that are declared public. All other attributes and methods are private and can be used for internal object processing.

In the example illustrated in above figure, the id attribute is private, and access to it is restricted to the object that defines it. Other objects can access this attribute using the getId() method. Using encapsulation, you can deny access to the id attribute either by declaring the getId() method as private or by not defining the getId() method.
Inheritance

Inheritance is an important feature of object-oriented programming languages. It enables classes to acquire properties of other classes. The class that inherits the properties is called a child class or subclass, and the class from which the properties are inherited is called a parent class or superclass. This feature also helps in reusing an already defined code.

In the example illustrated in above figure, you can create a FullTimeEmployee class that inherits the properties of the Employee class. The properties inherited depend on the access modifiers declared for each attribute and method of the superclass.
Polymorphism

Polymorphism is the ability for different objects to respond differently to the same message. In object-oriented programming languages, you can define one or more methods with the same name. These methods can perform different actions and return different values.

In the example in Figure 1-1, assume that the different types of employees must be able to respond with their compensation to date. Compensation is computed differently for different types of employees:

* Full-time employees are eligible for a bonus.
* Non-exempt employees get overtime pay.

In procedural languages, you write a switch statement, with the different possible cases defined, as follows:

switch: (employee.type)
{
case: Employee
return employee.salaryToDate;
case: FullTimeEmployee
return employee.salaryToDate + employee.bonusToDate
...
}

If you add a new type of employee, then you must update the switch statement. In addition, if you modify the data structure, then you must modify all switch statements that use it. In an object-oriented language, such as Java, you can implement a method, compensationToDate(), for each subclass of the Employee class, if it contains information beyond what is already defined in the Employee class. For example, you could implement the compensationToDate() method for a non-exempt employee, as follows:

private float compensationToDate()
{
return (super.compensationToDate() + this.overtimeToDate());
}

For a full-time employee, the compensationToDate() method can be implemented as follows:

private float compensationToDate()
{
return (super.compensationToDate() + this.bonusToDate());
}

This common use of the method name enables you to call methods of different classes and obtain the required results, without specifying the type of the employee. You do not have to write specific methods to handle full-time employees and part-time employees.

In addition, you can create a Contractor class that does not inherit properties from Employee and implements a compensationToDate() method in it. A program that calculates total payroll to date would iterate over all people on payroll, regardless of whether they were full-time or part-time employees or contractors, and add up the values returned from calling the compensationToDate() method on each. You can safely make changes to the individual compensationToDate() methods or the classes, and know that callers of the methods will work correctly.
Key Features of the Java Language

The Java language provides certain key features that make it ideal for developing server applications. These features include:

* Simplicity
Java is simpler than most other languages that are used to create server applications, because of its consistent enforcement of the object model. The large, standard set of class libraries brings powerful tools to Java developers on all platforms.

* Portability
Java is portable across platforms. It is possible to write platform-dependent code in Java, and it is also simple to write programs that move seamlessly across systems.

1 comment:

Lucas Kain said...

Thanks for the post! Pretty awesome blog, keep up the good work.

___
Cheap international calls