25 July 2009
0 BASICS OF JAVA Object Oriented Concepts
BASICS OF JAVA - CHAPTER-1 -Object Oriented Concepts (OOC)
Java is an Object-Oriented Language. So before learning java, let us have a look on the Object-Oriented Concepts.
Object Oriented Concepts (OOC)
Class and Object
A class is prototype that defines the variables and the methods (functions) common to all objects of a certain kind.
Elements of a class are :
1. Member Variable: Variables declared within a class
2. Member Functions: Functions or methods declared within the class. These methods operate upon the member variables of the class. So a class is a definition of a set of data and methods (functions). When memory space for this data is actually allocated, we say that class is instantiated i.e an object is created.
An object is a representative or specimen of a class. In other words, object is an instance of a class. Each instance of a class will have its own data. Actually an object is a bundle of related variables and methods (functions).
Objects possess two characteristics :
1. State : condition of an item.
2. Behavior : Observable effects of an operation or event including its results.
State can be of two types : Active state which reflects the behavior and Passive State refers to state which does not change. The behavior of an object forms the interface to the external world.
Features of OOP (Features of Object oriented Concept)
Abstraction:
The process of extracting the essential information and hiding the irrelevant details
Encapsulation:
The process of binding code and data together in the form of a capsule
Inheritance:
The feature by which one class acquires the properties and functionalities of another class
Polymorphism:
The feature that allows the same interface to be used for a general set of actions
Explanations with examples
Abstraction is the process of exposing the relevant things and ignoring the irrelevant details. The easiest way to understand and appreciate this concept of handling complexity is by studying the example of Globe, a model/prototype of earth that is used by students to understand its geography. Globe provides only that information that is required and if too much of information is mentioned in it i.e. streets, lakes etc, it becomes too complex to comprehend. Hence Globe abstracts unwanted information and makes it easy to comprehend the complex earth.
Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. The data is not accessible to the outside world and only those functions that are wrapped in the class can access it. These functions provide the interface between the object’s data and the program. The insulation of the data from the direct access by the program is called data hiding.
In OOP, code and data are merged into an object so that the user of an object can never peek inside the box. This is defined as encapsulation (i.e. Object is a capsule encapsulating data and behavior). All communication to it is through messages (i.e function calls which we use to communicate to the object). Messages define the interface to the object. Everything an object can do is represented by its message interface. Therefore, we need not know
anything about what is in the object when we use it.
Inheritance is the process by which one class acquires the properties and functionalities of another class. This is important because it supports the concept of hierarchical classification.. Inheritance provides the idea of reusability of code and each sub class defines only those features that are unique to it.
Polymorphism is a feature that allows one interface to be used for a general class of actions. An operation may exhibit different behavior in different instances. The behavior depends on the types of data used in the operation. It plays an important role in allowing objects having different internal structures to share the same external interface. Polymorphism is extensively used in implementing inheritance.
0 comments:
Please give your valuable comments.