Tuesday 2 October 2012

Interactive Programming (OOP) In Java

Object Oriented Programming Language – Java


Object orientation means that everything in the program is an object.

Class - A class describes a particular kind of object. It can contain related methods and data members (variables). A class must have the same name as the file it is contained in.

Example 1:
public class myClass(){   // class name – same as file name
     //data members
     //constructors
     //methods
}


Example 2: Car is a class and object of this class can be a wheel or a seat etc.

Object - The principal code building block of Java programs. Each object in a program consists of both variables (data) and methods (functionality).

An object like a car is composed of methods, like driving, braking, opening and locking a door, yelling at you when your seatbelt's off, etc, and data like the colour, make, model, etc.

Difference between Class and object in Java

In Java, Class is a blue print used to create objects. In other words, a Class is a template that defines the methods (Functions) and Members (variables) to be included in a particular kind of Object.

Here, “Student” is a class. And “Jenna” is an object of the class “Student”. And “John” is another object of the class “Student”. A Class is a template for an object, a user-defined datatype that contains the variables and methods in it.

A class defines the abstract characteristics of a thing (object), including its characteristics (its attributes, fields or properties) and the thing’s behaviours (the things it can do, or methods, operations or features). That is, Class is a blue print used to create objects. In other words, each object is an instance of a class. One class can have multiple number of instances. (There can be Number of instances of the class “Student”, like “Jenna”, “John” etc).

A class is a predefined frame of an object, in which its mentioning what all variables and methods an object (object of that class) can have.
The methods (seen in the code as methodName()) contain actions, like openDoor(), drive(), and inside each method are instructions on how to do that.

Encapsulation

Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction.

Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.
Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.

The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.

Abstraction

Abstraction means to show only the necessary details without including the background details. Class uses the concept of abstraction. Example of abstraction is class.

Simple words:
When we create the class we include methods inside the class which will perform some complex operations as per our requirements so whenever we would like to do those operations we simple call the methods by passing necessary arguments if required and it does all the work for us, now this we all know but this is abstraction.



No comments:

Post a Comment