Witscale Test Center

2.4 Object Oriented features of Java


2.4 Object Oriented features of Java

Here is a quick summary of few important features of an object-oriented language and how java follows them closely-

2.4.1 Everything is an object.

When you try to solve any problem, first you try to figure out the components in that problem domain. Java allows you to take any conceptual component in the problem domain and represent it as an object in your program. However it is often not as simple as a simple mapping of an object per component. A component is often made up of many collaborating objects and an object as well is often composed of other simple objects[‡‡‡].

 

Not ‘everything’ is an object in Java. Certain things are not objects. For example, the primitives are not represented as objects in Java. Object creation is heavier operation in terms of resources (like memory), hence primitives are represented directly as values instead of objects in Java. This is mainly due to optimization for better performance. Note however that you can represent a primitive value as an object with the help of wrapper classes (see Chapter 13).

 

2.4.2 Every object has a type.

An object is an instance of a class. The other way to look at it is- A class represents a type of objects. Hence each object should have a type. Note that Java mandates that each object must have a type.

Java provides you with the syntax to define a class and instantiate it (to create objects). Now that class is called as the “type” of all objects that are created from it. For example, if you define an integer object as Integer i = new Integer(3); then object i is of type Integer.

 

Interestingly however object i is also of type Number (the class from which class Integer extends itself), Object (which is a superclass for all Java classes) and Comparable (which is the interface that the class Integer implements). You can check the type of an object using the instanceof operator.

 

2.4.3 Objects collaborate to achieve functionality.

A Java application is often a bunch of collaborating Java objects. Each java object is defined to implement certain responsibility. Objects collaborate by sending messages to each other. In Java, an object sends a message to another object by invoking its method.

Thus you can keep Java objects are simple yet achieve complex functionality through object collaboration.

2.4.4 Encapsulation in Java

Encapsulation is protecting sensitive data and at the same time providing appropriate access to it. You can achieve encapsulation in Java by simply defining a Java class using appropriate access modifiers for its members. Simply put, you should encapsulate the state by defining member variables as private and providing appropriate public accessing methods.

2.4.5 Inheritance in Java

Java allows you to define a class and reuse its definition by inheriting another class (AKA subclass) from it[§§§]. Access modifiers (Chapter 3)  help you to fine tune the inheritance. It means you can use the access modifiers such as protected to label whether the members of a class (methods as well as variables) should be inherited by the subclass.

2.4.5 Polymorphism in Java

Java has several features that manifests polymorphism. We have already discussed how (and why) method overloading, method overriding and interfaces should be used in Java.