Java/Concepts of Object Oriented Programming

From Meshplex

Jump to: navigation, search
Image:Java_programming.jpg
Introduction to Java
Overview of Java
How to Setup Java on your PC
Java Data Types, Variables,and Arrays
Java Operators
Java Packages, Classes, and Interfaces
Java Program
Java Modifiers
Java Control Statements
Java Exception Handling
Java Object Oriented Programming
* Java Basic Concepts
* Java IS-A and HAS-A
* Java Inheritance and Polymorphisim
* The Object class
Java Wrappers
Java Strings
Java Math
Java Arrays
Java Random Numbers
Java Date and Time
Java Regular Expressions
Java Collections
Java Generics
Java Thread and Multithreading
Java IO and file Handling
Java Network Programming
Java RMI
Java Image, Audio, Video
Java GUI
Java Applets
Java Internationalization
JDBC
Java and XML

Contents

[edit] Java Object Oriented Programming

Java is an object-oriented programming language as it works totally on the concepts of Object oriented Programming concepts.


We will try and understand the basic concept of Object Oriented Programming language and the concept of object in the section below.

Object oriented programming (called OOP for short) is the concept, which got its way after the procedural programming languages which was developed in 1970’s.

The programming language which existed before was more of process oriented and this gave the concept of small entities called objects which could be made and reused as and when the need arises.

OOP hails from the idea of Objects. All the real life entities are basically nothing but objects. In any program every code will have an object having few characteristics features called the properties of that particular object or entity and then it will always have few actions to be performed over those entities or objects termed as Methods or Functions. To work with OOP, you should be able to identify three key characteristics of objects:

• The behavior of object — what can you do with this object, or what methods can you apply to it? • The state of the object— how does the object react when you apply those methods?

• The identity of the object— how is the object distinguished from others that may have the same behavior and state?

This would have surely given an overview as to what is OOPs , why and how it has resulted in the beginning of a new era in programming language but to have a proper understanding on OOPs , it is very important to make yourself familiar with one more term Class .

[edit] Java Classes

The objects actually originate from the class or to define in an easy way the class is the template from which the object is derived. Whenever we create an object from the class it is called the instance of that particular class. All code that you write in Java is inside a class only.

For example there is a class called Animal and it has an instance Dog as an object to the class Animal and hence called the instance of it.

Principles of OOPs

All object programming languages provide us with few basic mechanisms which can be termed as Basic principles of OOPs and they are

1. Encapsulation

2. Inheritance

3. Polymorphism

We will discuss each one of them in detail in the section below as they hold the key to successful implementation to the object Oriented Model approach.

Encapsulation

One of the very important mechanisms implemented in OOPs is encapsulation. It helps the programmer to keep its code and the data safe from outside interference and misuse. It keeps the code intact to be used safely. It can also be termed as the cover or the wrapper which works as a shield to safeguard the code from the unauthenticated use of it. Access to the code and data inside the wrapper is tightly controlled through a well defined interface.

The encapsulated data can be used easily by the users without any fear of being misused and without being having side effect.

[edit] Java Inheritance

As the term illustrates this mechanism has something to do with the feature inherit which means to acquire the characteristics of the features of any object. this feature makes the OOPs more compatible as it needs any object to acquire only the features or the characteristic which are unique and rest can be inherited from its class.

Let’s try and understand it with the example.

We have a class called mammal and we create instances of it by the name whale and horse. They both inherit the common features of a mammal as they both give birth to young ones and feed them on their milk.

They are required to have only the unique features separately as horse feeds on grass and lives on ground and whale is a water animal and survives in water.

Thus Inheritance makes OOPs very much user friendly and compatible. But there is one more thing in Java that it does not support multiple inheritance like C++. It is not like that Java does not have the potential to handle that but it is not include in Java because multiple inheritance has it own drawback that is called diamond shape problem. Dont worry, Java provides the mechanism for multiple inheritance through Java Interfaces.

[edit] Java Polymorphism

This term originates from the Greek word which means many forms. With this mechanism it is possible in OOPs to design one interface and implement it with multiple methods or actions. More generally, the concept of polymorphism is often expressed by the phrase one interface and multiple methods. The compiler takes the entire responsibility of identifying the situation and implements that particular method on the code with respect to it and hence making the life of the programmer very easy and simple.

Taking an example of the smelling characteristic of Dog.

Smelling a rat or mouse will make him realize that there is a prey nearby and hence he barks and tries to locate the prey, on the other hand if the dog gets the smell of food he is very much sure that it is his food and thus runs towards its food instead of barking which he was doing in the previous situation, so it’s the same feature having the different actions or methods to be implemented.

The same concept is been applied by the complier in case of polymorphism making it very effective and compatible for the programmer while implementing the concepts of OOPs.


Previous Next