Home | | Sitemap ||Page number :1

JAVA AND OOP

Computers have been with us for a long time. In the recent years computer’s hardware as well as software has evolved quickly. Before we plunge into a discussion of Java, a quick study of how it evolved is quite necessary.

A structured oriented programming language named C commenced the modern era of programming language. Prior to the evolution of C, many languages such as COBOL, PASCAL, BASIC and FORTRAN were in commonplace. All these languages were not efficient enough to be considered as an answer to the problems that programmers faced while working with software codes. C became the most extensively used language during the late 1970s and 1980s. Soon a need for a better language was raised, the cause being the increasing complexity of programs. To cope with this problem C++ was developed by Bjarne Stroustrup in 1979.

Java was developed by a group of programmers at Sun Microsystems in 1991. Earlier it was named Oak. The intention behind the development of this language was to create a platform independent language that could be used in devices like microwave ovens. Now by using this characteristic of Java the language is widely used in mobiles for developing games, applications etc. The secret of the platform independent characteristic of Java lies in bytecode and JVM (Java Virtual machine).When we compile a java program it is converted to bytecode and then Java Virtual Machine executes the bytecode. Thus as a developer we don’t need to make separate versions of our software for different platforms. A code in java will run perfectly in Windows, Linux, Macintosh or any other Operating system. Even a hardware upgrade does not affect the program.

OOP concept----

Java and C++ both are OOP (Object Oriented Programming) language. OOP is the crux of the two languages, especially Java. A curiosity arises in our mind, what is OOP and objects? We will now study OOP in some detail. Earlier we read that C++ was able to handle the increasing complexity in programs, this is all because Java is Object Oriented unlike C which is Structured Oriented. After we have studied OOP, students will be given a basic introduction of structured oriented approach. In OOP we take a program in terms of objects rather than methods or procedures. Characteristics of objects are determined by data and behaviour by methods or functions. OOP corresponds better to real life situations then any other paradigm.

Objects, also known as instances, are an independent unit with certain characteristics. Another important term associated with OOP is class. A class is group of objects. In C++ we can directly work on methods without creating a class, but in Java designing a class is binding on the programmer. A class acts as an encapsulating unit thus binding data and function in itself. A real life example of class can be fruits, which has instances like Mango, Apple and Banana.

sd

In structured oriented approach the stress is on procedures, sometimes also known as methods or functions.

The three principles of OOP -

  1. Encapsulation
  2. Inheritance
  3. Polymorphism

Encapsulation - As we studied earlier, Encapsulation is the mechanism of binding Data and function into class. This principle enables data abstraction i.e. hiding complexity and we can concentrate on relevant details. We can understand this concept better by taking a practical example of mobile phone which forwards hundreds of text messages everyday. Although we know the application but “how our mobile connects to network and how the network handles the traffic of thousands of such messages” is not known to most of us. Some access keywords like private and public help us to encapsulate our data or allow it to be accessed anywhere.

Public --- Data members declared public are available outside the class. Private ---Data members declared private are available within the class.

Inheritance -The word ‘Inheritance’ describes a lot about itself. We inherit traits from our parents and have our own added qualities, thus we are not clones but have an individual personality. The same principle applies in programming. After creating a parent class we can define its child class and hence reusing our code. The child class will have some added properties. Inheritance allows us to create an object oriented hierarchy.

Polymorphism -If you understand the meaning of the word, the whole concept will quickly become apparent. ‘Polymorphism’ is a Greek word that means “many forms”. Again taking a practical example of mobiles, we can compare polymorphism to the ringing of phone. If you get a message from Ankit the phone rings, the phone will even ring if you get a call from Nikhil. The ringing capability of phone is polymorphic. Take a look at ‘+’ i.e. addition operator; it is used to add variables and constants. This operator can also be used to concatenate two ‘strings’ which is just a group of characters like “k3b” or “splash”. In the upcoming chapters we will deal with method and constructor overloading that too demonstrates polymorphism.