Getting Started With Object Oriented Programming in Java

 

Introduction to Object Oriented Programming


 

To build today’s complex software it is not just enough to put together a sequence of programming statements. We need to use sound construction techniques and program structure that are easy comprehend, implement and modify in a wide verity of situations.

          With the advent of language such as C, structures programming become very popular was the paradigm of the 1980s. However, as the programmes grew larger, even the stricter approach failed to show the desired result in term of bug-free, easy-to-maintain, and reusable programs.

       To overcome these problems the Object Oriented Programming came in to picture. It is a new way of organizing and developing programs and has nothing to do with any particular language.

However, not all languages are suitable to implement the Object Oriented Programming concepts easily. Language that support OOP(Object Oriented Programming) features includes SmallTalk, Objective C, C++, Ada and Object Pascal. C++, an extension of C Language, is the most popular OOP language today. C++ basically a procedural language with Object-oriented extension.

Java, a pure object-oriented language, is one of the recent languages added to this list, the latest one being C#.

Object Oriented Programming basic concept

          Object-oriented is a term, which is interpreted differently by different people.

Object-oriented programming is an approach that provides a way of modularizing programs by creating portioned memory areas for both data and functions that can be used as templates for creating copies of such modules on demand. It will reduce our code size and as well as time for writing code.

 Now we will discuss some basic and general concepts of Object-oriented programming.

A] Classes and Objects:

          Object is a runtime entity of class. And class defines behaviour and working of Object. The term of Object it may be one person, one account, a table of data or any item that the programm may handle.

For example: A simple class in Java is like

Class Person{

          String firstName;

          String lastName;

          String address;

          void setFirstName(){}

          void setLastName(){}

          void setAddress(){}

          String getFirstName(){}

          String getLastName(){}

          String getAddress(){}

}

From the above class we can create number of Object when we need.

          When a program is executed, the Objects interact by sending messages to one another. Each Object contains data and code to manipulate the data. Objects can interact without having to know the details of each other’s data or code.

          Once a class has been defined we can create any number of objects belonging to that class. Each object is associated with the data of type class with which they are created. The class is thus a collection of Objects of similar type. Classes are user-defined data types and behave like the built-in types of programming language. For example the syntax used to create an object is no different than the syntax used to create an integer object in C. If Person has been defined as a class, then the statement

Person amit;

Will create an object amit belonging to the class Person.

B] Data Abstraction and Encapsulation:

          Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class.

          The data encapsulation is the most striking feature of a class. The data is not accessible to the outside world and only those methods, which are wrapped in the class, can access it. These methods provide the interface between the object data and the programm. This insulation of the data from direct access by the programm is called Data hiding.

          The concept of data encapsulation and abstraction can be well understood by considering the sample example of a mobile phone. It is complex electronic device, which is used for text and voice communication. How the internal circuitry of the mobile phone functions to enable the end use to talk to remote person is irrelevant from his viewpoint.

To achieve encapsulation in Java

a. Declare the variables of a class as private.

b. Provide public setter and getter methods to modify and view the    

  variables values.

C] Inheritance

 A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.

Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.

          In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one. The new class will have the combined features of both the classes. Thus the real appeal and power of the inheritance mechanism is that it allows the programmer to reuse a class that is almost, but not exactly, what he wants, and to tailor the class in such a way that is does not introduce any undesirable side effects into the rest of the classes. In Java, derived class is known as subclass. Each subclass defines only those features that are unique to it. Without the use of inheritance, each class would have to explicitly include all of its features.

In our example consider Addres is another class.

Class Address{

          String house;

          String street;

          String city;

}

We can use address class in Persone class using inheritance as follows.

Class Person extends Address{

.

.

}

In Person class member we don’t write any member which is already exist in Address class.

D] Polymorphism

          Polymorphism is another important concept of OOP. Polymorphism means the ability to take more than one form. For example, an operation may exhibit different behaviour in different instances. The behaviour depends upon the types of data used in the operation. For example, consider the operation of addition. For two numbers, the operation will generate a sum. If the operation are string, then the operation would produce a third string by concatenation.

Example


In above figure a shape objects draw method can draw Circle, Box or Triangle.

Polymorphism plays an important role in allowing objects having different internal structures to share the same external interface. This means that a general class of operations may be accessed in the same manner even though specific actions associated with each operation may differ; Polymorphism is extensively used in implementing inheritance.

Compile Time and Runtime Mechanisms

          In the context of programming language, compile time may refer to any of the following.

·       The operations performed by the compiler such as syntactic and semantic analysis.

·       The pre-runtime assessment of the program’s behaviour.

In Java, apart from syntax and semantic checks, one important logic task that is considered to be performed at compile time is the implementation of inheritance.

Runtime, other hand, is the time period when a syntactically correct program is actually executed in the compiler system. Thus, all the task that are performed during this time period are prefixed with the word ‘runtime’ or ‘dynamic’, such as runtime type checking or dynamic memory allocation. In java, one of the important concepts that is associated with runtime is the implementation of polymorphism. It is also referred as dynamic binding.

Dynamic Binding

 Binding refers to the linking of a procedure call to the code to execute in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at runtime. It is associated with polymorphism and inheritance. A procedure call associated with polymorphic reference depends on the dynamic type that reference.

          Consider the procedure ‘draw’ in above figure. By inheritance, every object will have this procedure. Its algorithm is, however, unique to each object and so the draw procedure will be defined in each class that defines the object. At runtime, the code matching the object under current reference will be called. In other word we can say that there is no way to know the behaviour of draw method at compile time.

Following are the simple example of

 Static Binding and Dynamic Binding

class Dog

{  

private void eat()

{

        System.out.println("dog is eating...");

}  

       public static void main(String args[])

{  

 Dog d1=new Dog();  

                    d1.eat();  

}

}

Example of Dynamic Binding.

class Animal

{  

          void eat()

{

System.out.println("animal is eating...");

}  

}  

  

class Dog extends Animal

{  

 void eat()

{

System.out.println("dog is eating...");

}  

 public static void main(String args[])

{  

 Animal a=new Dog();  

                    a.eat();  

 }  

}  

Benefits of Object Oriented Programming

OOPs offers several benefits to both program designer and the user. Object-Oriented contributes to the solution of many problems associated with the development and quality of software products. The new technology promises greater programmer productivity, better quality of software and lesser maintenance cost. The principal advantages are:

👉Through inheritance, we can eliminate redundant code and extend the use of existing class.

👉  We can build programs from the standard working modules that   communicate with one another, rather than having to start              writing the code from scratch. This leads to saving of development time and higher productivity.

👉 It is one of the secure development approaches in which data is hidden that cannot be assessed by any external function. With the use of abstraction mechanisms and Data Hiding, programmers can filter out the limited data for the exposure which means that the security is maintained easily and also provides necessary data.

            👉 Object-oriented system can be easily upgrade from small to large                     systems.

👉 Design stability Once a stable base class has been developed, the new classes that are derived may have fewer less errors and bugs.

 

Thank you for reading my article.

 

 

 

 

 

 

 

Comments

Post a Comment