Object-Oriented for Java

Java is an object-oriented proamming(OOP) Lanaguage, concept of OO is  imporatant for all Java progarmmer. It is useful to learn some basic principles in order to write a better program. Followings are questions that related to the OOP to help one who want to understand Java.

  • What is Object-Oriented technologies?
  • Why should we use OO in software development?
  • How Java acts as an OOP?
  • How can we adopt the OO in an effective way?
  • Is there any guideline for us during the OO software development?

In this arcticle, we will discuss some answers of above.

Object-Oriented technologies

In computer science, object-oriented is a software engineering concept, as the name suggested it refer to things with “objects".  “Object" can be a variable, function or even  a data structure. An object usually have a location in memory that have a value and referenced by an identifier. There are many related development with object-oriented concept and technologies, such as Object-oriented analysis and design, Object-oriented modeling, Object-oriented Programming(OOP), Object-oriented language and etc. OOP is actually a programming model that utilize object-oriented concept. With OOP, programs are organized by object and data, that is  the major different from tradition programming which focus on action and logic.

Why OO?

Let see why using  object-oriented in modern software development. We often use the  “divide and conquer" strategy to solve the difficult problem, it is also a basic technique for us to cope with the complexity of the reality. With this strategy, we break the problem into smaller and manageable pieces in a certain way, then we apply the specified solution to each piece.  This breakdown process can be refered to decomposition. However, the way of decomposition was a problem, we used technique of functional decomposition to solve problems in software development, the general procedure are as followings:

  • Break problem down into steps(procedure) or sub-problems
  • Find out or build program code to deal with each sub-problem, solutions are often come with the past experience or pre-built things.

Functional decomposition break down the complexity of the software into small pieces so that we can build the software in an easier and more formal way. It is the more straightforward way for software development, it just like a job break into different steps which carry out a series of task performed. It is also the most general way of our thinking model, that is why we usually decompose things into hierarchical structure only for simplicity.

The functional decomposition itself is not a problem, it does fit when the situation is simple, stationary and ready-made system; however the world tend to have much more changes during these decades. This traditional programming methodologies, named as procedural programming, bring a series of problems in software development. some of them are listed below.

  • Control and coordination can only be centered in the main method, data is separated from the function.
  • Any changes may cause big impacts.
  • Logic may be distributed within the programe as a side effect of the decomposition.
  • Data changes may affect many parts in the program, cascade of changes many be occurred.

Although problems did exist in the old software system, the technology limitations cover all other difficulties in software development during 90’s – 00’s, so software Modeling is quite deflect with the reality. The over-simplified or defective information system model also created issues on the software system. Software system became fragile, viscous, rigid, opaque and immobile. Moreover the software code was always complex and containing many unnecessary repetition.

Object-Oriented technologies came to solve problems mentioned; they focus on how to deal with complexity and changes. OOP can promote code reuse and decrease code maintenance effort. OO improve software development by building “Modules", modules encapsulate data to maintain integrity and give a control for data access. Furthermore, with OO we can encapsulating operations on data so that functionalities are bound to data. In short, modules in OO define operation, data and responsibilities.

Object Oriented programming(OOP) is programming which is oriented around objects, thus taking advantages of

  • Encapsulation,
  • Polymorphism, and
  • Inheritance

Basic OOP Concepts

Some basic OOP concepts are necessary for Java programming, they are as followings

Objects

An object is a unique programming entity that has attributes to describe it (somethings similar to  Adjectives in English), methods to get/set attribute values (somethings similar to Verbs in English). That is to say, an object have its own data(sometimes refer to “state") and identity, data structure and behavior (what action can be performed). In OO, objects are “physical", they are the basic run-time entities and occupy memory space in the computer system.
Object = state + behavior

Attributes

An object’s data, also named as properties, are stored in attributes. Attributes provide a way to describe an object. In OOP, programmers can read property values, change properties, and assign values in an object.

Methods

Methods provide a way to access or even manipulate object’s data stored in their attributes. Method is the Java term for subroutine, C/C++ calls “function".

Events

Interaction is an important aspect in the OOP, events are so defined as a record of interaction which are usually changes in the environment.

Abstraction

The concept of simplification is called abstraction in OO.  By abstraction programmers can focus on the “big picture” only and ignore some of the specific details regarding the internal structure or behavior of an object.

Classes

In OOP, we use a programming structure called a class to implement abstraction, it represents a blueprint of an object. A class contains properties and methods to show the internal structure and behavior.  A class can be treated as a set of objects that share common structure and behavior, in here the word “set" is something like “set" in modern mathematics. Classes provide a defination of attributes and methods, and usually provide the implementation of methods. In Java, classes usually contain constructors that allow initializing attributes at creation time, that is the assginment of value to the attribute.

Instantiation

The object of a class is called an instance of that class and the process of creation is called instantiation. In Java, a special method called a constructor is used to create an instance of an object. There is no de-constructor or destroyer in Java.

Encapsulation

Encapsulation is the idea about information hiding and isolation, it is closely related to the concept of Abstraction. Data and the ways to get at that data are wrapped in a single package, that is a class. With encapsulation data and code are bound together so that there is no external interference and misuse. Java use class as a basic unit of encapsulation.

Inheritance

One of the major aspects of OOP. Inheritance allows creating new classes from extending the existing class. A child class inherits its properties and attributes from parents, with the ability to overriding. The main advantage for inheritance is that an object need not define all its characteristics explicitly. Java only supports single inheritance for simplicity.

Polymorphism

It is the ability to create things, variable, function or even object that more than one form. Polymorphism let us describe how to write methods to do some general purpose function. Different objects might perform polymorphic methods differently in run-time. Polymorphism involves concept of type.  A variable can be assigned to different types of objects while different method implementations can have for different types.

Association

The relationship of the objects or classes are known as association, an association contains the semantic meaning of roles that each class play, multiplicity and the direction of the relationship.

Interface

A computer system is actually a collection of different parts, the part need to communication or interact with others in order to compete tasks. For instance, an operating system may interface with pieces of hardware. An interface is the place of interaction with software, hardware or peripheral devices conceptually. The implemenation of the interface may be in different format even it a differtent level in a computer system.  In OO, object need to interact with each other via method.

In object-oriented languages, “interface" is often used to define an abstract type that contains no data but methods to define the behaviour. So, an interface is a type definition. A class having all the methods corresponding to that interface is said to implement that interface. Furthermore, a class can implement multiple interfaces, and hence can be of different types at the same time.

Althrough using an interface in the interaction is not a must. A key principle of design is to prohibit access to all resources by default, allowing access only through well-defined entry points, i.e. interfaces. By using interafce, the type of the object to be exchanged in the interaction can be defined in terms of an interface instead of a specific class, the code developed can be more generic and reusable as long no changes have been occur with the interfeace( the defination).

Java as an OOP

Java is a well-developed object-oriented programming language, it follows a strict object programming model. Most of the object-oriented features are almost transparent to the programmer, so almost no additional effort  needed in software development to adopt OO when using Java. However, Using Java alone is not enough for building a good software,  we also need a good software design. In order to have a good software design, we have to perform the followings

  • Apply Object-Oriented principle appropriately.
  • Develop a suitable architecture and software model.
  • Apply software patterns.
  • Using a suitable development methodology
  • Adopt corresponding best practices.

we will have discuss on those mentioned above.

發表留言