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.
All Online E-Books,
Wednesday, January 13, 2010
Objects
Objects
A class needs to be instantiated before you can use the instance variables or attributes and methods. An object is an instance of a class and is analogous to a relational table row. The instance contains the attributes, which are known as its data or state, and the methods defined in the class.
above figure shows an example of an Employee class defined with two attributes, id, which is the employee identifier, and lastName, which is the last name of the employee, and the getId() and setId(String anId) methods. The id attribute and the getId() method are private, and the lastName attribute and the setId(String anId) method are public.
When you create an instance, the attributes store individual and private information relevant only to the employee. That is, the information contained within an employee instance is known only to that particular employee. The above figure shows two instances of the
source:-http://download.oracle.com/docs/cd/B28359_01/java.111/b31225/chone.htm#BABDECCF
A class needs to be instantiated before you can use the instance variables or attributes and methods. An object is an instance of a class and is analogous to a relational table row. The instance contains the attributes, which are known as its data or state, and the methods defined in the class.
above figure shows an example of an Employee class defined with two attributes, id, which is the employee identifier, and lastName, which is the last name of the employee, and the getId() and setId(String anId) methods. The id attribute and the getId() method are private, and the lastName attribute and the setId(String anId) method are public.
When you create an instance, the attributes store individual and private information relevant only to the employee. That is, the information contained within an employee instance is known only to that particular employee. The above figure shows two instances of the
Employee
class, one for the employee Smith and one for Jones. Each instance contains information relevant to the individual employee.source:-http://download.oracle.com/docs/cd/B28359_01/java.111/b31225/chone.htm#BABDECCF
Methods,Classes
Classes
All object-oriented programming languages support the concept of a class. As with a table definition, a class provides a template for objects that share common characteristics. Each class can contain the following:
* Attributes
Attributes are variables that are present in each object, or instance, of a particular class. Attributes within an instance are known as fields. Instance fields are analogous to the fields of a relational table row. The class defines the variables and the type of each variable. You can declare variables in Java as static and public, private, protected, or default access.
Variables of a class that are declared as static are global and common to all instances of that class. Variables that are not declared as static are created within each instance of the class.
The public, private, protected, and default access modifiers define the scope of the variable in the application. The Java Language Specification (JLS) defines the rules of visibility of data for all variables. These rules define under what circumstances you can access the data in these variables.
In the example illustrated in Figure below, the employee identifier is defined as private, indicating that other objects cannot access this attribute directly. In the example, objects can access the id attribute by calling the getId() method.
*
Methods
Methods are blocks of code that perform specific tasks or actions. You can call methods on an instance of a class. You can also call methods that are inherited by a class. Methods provide a way for instances to interact with other instances and the application. Similar to attributes, methods can be declared as public, private, protected, or default access.
All object-oriented programming languages support the concept of a class. As with a table definition, a class provides a template for objects that share common characteristics. Each class can contain the following:
* Attributes
Attributes are variables that are present in each object, or instance, of a particular class. Attributes within an instance are known as fields. Instance fields are analogous to the fields of a relational table row. The class defines the variables and the type of each variable. You can declare variables in Java as static and public, private, protected, or default access.
Variables of a class that are declared as static are global and common to all instances of that class. Variables that are not declared as static are created within each instance of the class.
The public, private, protected, and default access modifiers define the scope of the variable in the application. The Java Language Specification (JLS) defines the rules of visibility of data for all variables. These rules define under what circumstances you can access the data in these variables.
In the example illustrated in Figure below, the employee identifier is defined as private, indicating that other objects cannot access this attribute directly. In the example, objects can access the id attribute by calling the getId() method.
*
Methods
Methods are blocks of code that perform specific tasks or actions. You can call methods on an instance of a class. You can also call methods that are inherited by a class. Methods provide a way for instances to interact with other instances and the application. Similar to attributes, methods can be declared as public, private, protected, or default access.
Subscribe to:
Posts (Atom)