Java Tech Tutorials
000webhost logo

OOP's Concept in Java

Core Java

Object Oriented Programming or OOP is the technique to create programs based on the real world.

Why OOP's is soo Important

Suppose that you want to assemble your own PC, you go to a hardware store and pick up a motherboard, a processor, some RAMs, a hard disk, a casing, a power supply, and put them together.
Similarly, a car is assembled from parts and components, such as chassis, doors, engine, wheels, break, transmission, etc. The components are reusable, e.g., a wheel can be used in many cars (of the same specifications).
Hardware, such as computers and cars, are assembled from parts, which are reusable components.

What about software?
Unlike hardware, it is very difficult to "assemble" an application from software components. Since the advent of computer 60 years ago, we have written tons and tons of programs. However, for each new application, we have to re-invent the wheels and write the program from scratch.

Traditional Procedural-Oriented languages
Can we do this in traditional procedural-oriented programming language such as C, Fortran, Cobol, or Pascal? Traditional procedural-oriented languages (such as C and Pascal) suffer some notable drawbacks in creating reusable software components:

The programs are made up of functions. Functions are often not reusable. It is very difficult to copy a function from one program and reuse in another program because the the function is likely to reference the headers, global variables and other functions. In other words, functions are not well-encapsulated as a self-contained reusable unit.

The procedural languages are not suitable of high-level abstraction for solving real life problems

In brief, the traditional procedural-languages separate the data structures and algorithms of the software entities.

In the early 1970s, the US Department of Defense (DoD) commissioned a task force to investigate why its IT budget always went out of control; but without much to show for. The findings are:

1. 80% of the budget went to the software (while the remaining 20% to the hardware).

2. More than 80% of the software budget went to maintenance (only the remaining 20% for new software development).

3. Hardware components could be applied to various products, and their integrity normally did not affect other products. (Hardware can share and reuse! Hardware faults are isolated!)

4. Software procedures were often non-sharable and not reusable. Software faults could affect other programs running in computers.

The task force proposed to make software behave like hardware OBJECT. Subsequently, DoD replaces over 450 computer languages, which were then used to build DoD systems, with an object-oriented language called Ada.

Object-Oriented Programming Languages

Object-oriented programming (OOP) languages are designed to overcome these problems.

1. The basic unit of OOP is a class, which encapsulates both the static attributes and dynamic behaviors within a "box", and specifies the public interface for using these boxes. Since the class is well-encapsulated (compared with the function), it is easier to reuse these classes. In other words, OOP combines the data structures and algorithms of a software entity inside the same box.

2. OOP languages permit higher level of abstraction for solving real-life problems. The traditional procedural language (such as C and Pascal) forces you to think in terms of the structure of the computer (e.g. memory bits and bytes, array, decision, loop) rather than thinking in terms of the problem you are trying to solve. The OOP languages (such as Java, C++, C#) let you think in the problem space, and use software objects to represent and abstract entities of the problem space to solve the problem.


As an example, suppose you wish to write a computer soccer games (which I consider as a complex application). It is quite difficult to model the game in procedural-oriented languages. But using OOP languages, you can easily model the program accordingly to the "real things" appear in the soccer games.

Most importantly, some of these classes (such as Ball and Audience) can be reused in another application, e.g., computer basketball game, with little or no modification.

Benefits of OOP

The procedural-oriented languages focus on procedures, with function as the basic unit. You need to first figure out all the functions and then think about how to represent data. The object-oriented languages focus on components that the user perceives, with objects as the basic unit. You figure out all the objects by putting all the data and operations that describe the user's interaction with the data.

Object-Oriented technology has many benefits:

Object Oriented Programming in Java

Class and Objects (Instances)

In General - A class is collection of data + methods.
In technical - A class is collection of related data + relevant methods.
An instance(object) is a realization of a particular item of a class. In other words, an instance is an instantiation of a class. All the instances of a class have similar properties, as described in the class definition. For example, you can define a class called "Student" and create three instances of the class "Student" for "Peter", "Paul" and "Pauline".
The term "object" usually refers to instance. But it is often used loosely, which may refer to a class or an instance.

Class Definition

In Java, we use the keyword class to define a class. For examples:

The syntax for class definition in Java is : -

[AccessControlModifier] class ClassName {
   // class body contains definition of variables and methods

public class Circle {        // class name
   double radius;            // variables
   String color;

   double getRadius() {...}  // methods
   double getArea() {...}
public class SoccerPlayer {  // class name
   int number;               // variables
   String name;
   int x, y;

   void run() {...}          // methods
   void kickBall() {...}
We will explain the access modifier, such as public and private, later.

Class Naming Convention:

A class name shall be a noun or a noun phrase made up of several words. All the words shall be initial-capitalized (camel-case). Use a singular noun for class name. Choose a meaningful and self-descriptive classname. For examples, SoccerPlayer, HttpProxyServer, FileInputStream, PrintStream and SocketFactory.

Creating Object(Instances) of a Class

To create new object, you have to :

Student studentObj;    // Declare new object reference
studentObj = new Student();   // Allocate and construct the instances via new operator

//you can do this in a single line
Student studentObj = new Student();

//new operator instantiates a class by allocating memory for a new object and returning
//a reference to that memory. The new operator also invokes the object constructor.

note : JVM create the object but JVM does not returns the original address, It returns duplicate address this duplicate address is also called as "HashCode"