Java is not fully an object-oriented language.
Let’s find out how?
Today we are going to discuss the most widespread argument about Java, over the last decade many Java developers have argued on whether Java a pure object-oriented language or not?
Many developers have posted their queries on discussion forums like Quora, Stack Overflow, and so on, but have received diverse answers from distinct developers.
This increasing diversity leads to no conclusion rather it creates ambiguity among the learners.
Object-oriented programming system concepts are the backbone of Java, the entire Java framework is based on the class-object system.
Nowadays, to test the promptness and knowledge of the candidate interviewers often ask this question in the face-to-face interview round.
Along, with the answer, we have also given a specific reason for supporting your answer.
Even though Java is recognized as one of the most successful and remarkable Object-oriented programming language across the globe, it also include few functionality like programming touches in Java 8, which is never considered complete or a pure object-oriented programming language.
The answer to the question, “Whether Java a pure object-oriented language or not?”
It is clearly NO (Java is not a pure object-oriented language).
This answer is concluded after carrying out detailed research on the Object-Oriented Programming Language concepts and principles.
The primary fact which is responsible for this inference is that in a pure object-oriented language, all the components are objects, while in Java there are many basic building blocks that are not considered to be objects such as the primitive data types like Boolean, char, short, float, double, int, long, and the various sorts of arithmetic, logical, and bitwise operator like +, -,*, /, &&, ||,! ^ and so on.
Object-Oriented programming languages
Pure Object-Oriented Languages, also known as Complete Object-Oriented Languages are the Fully Object-Oriented Languages that support or have strict features such that they treat everything inside the source-code as objects.
Such languages do not support the primitive data types, for example, int, char, float, bool, and so on.
Although, most of the programming languages are based on object-oriented programming system concepts, yet only a handful of them are considered to be a pure Object-oriented programming language. Only Smalltalk and Self are recognised as purely object-oriented languages.
Furthermore, Self relies on object prototypes, while Smalltalk is conventionally considered to be object-oriented with classes and implementation inheritance. Hence specifically, there is only a single pure object-oriented programming language, which is Smalltalk.
Smalltalk/Squeak/Pharo, Erlang, Eiffel, Self, and CLOS (Common Lisp Object System) are a few languages that are pure object-oriented programming language or very close to being one of them.
Smalltalk is remarkably recognized as the purest form of an object-oriented programming language.
When it comes to system requirements, Smalltalk variants run on many different platforms, so does CLOS (on any platform where you can run the Common Lisp).
But the platform-independent property of Java is the most superior.
There are seven object-oriented based features that need to be satisfied with, for considering any programming language to be a pure Object-Oriented language.
They are mentioned below:
- 1.Encapsulation & Data Hiding
Apart from inheritance, polymorphism, and abstraction, encapsulation is one of the four fundamental OOP concepts.
Encapsulation can be defined as the wrapping up or combining of data into a single unit, the word comes from a capsule that holds different compositions together.
In OOPs Encapsulation is the mechanism that links together the source code and the data it manipulates.
Encapsulation binds the data and covers it with an imaginary shield, any function or code outside the shield (class) cannot access the encapsulated data members, code, and functions.
The class needs to be declared as a private class for being encapsulated; by default, all the classes are private only.
The class can be used by the end-user or the world without providing any details behind implementation using the abstraction concept, so technically encapsulation is a combination of data-hiding and abstraction.
Steps for achieving encapsulation in Java :
- Declare the member variables of a class as private.
- Create public setter and getter methods to modify or view the values of the variables.
Advantages of using Encapsulation:
- The fields of a class can be set as read-only or write-only using getters and setters.
- A class can provide selective access to the data contained in its fields.
Encapsulation makes the code concise and modularised for unit testing.
- Encapsulation increases the reusability of the code and makes it easily adaptable to new requirements.
- The user has no idea about the inner implementation of the class because of data hiding.
- 2. Inheritance
The mechanism through which an object can acquire all the behaviours and properties of its parent object is known as Inheritance.
It is one of the most essential features of OOPs (Object Oriented programming system). The main object of Inheritance is to increase code reusability and reduce code redundancy.
This can be done by creating new classes by building upon the pre-existing classes.
During inheritance, you inherit from a pre-existing class, so that you can re-use its methods and fields to avoid boilerplate code.
Furthermore, you can even add new methods and fields in the new class.
A subclass is a class that inherits other classes, which is also known as a derived class, extended class, or child class, whereas a superclass is a class from where a subclass inherits the features.
Commonly also known as a base class or a parent class.
Inheritance demonstrates the IS-A relationship which is popularly also known as a parent-child relationship.
Advantages of Inheritance in Java :
- It facilitates runtime polymorphism by Method Overriding.
- For facilitating Code Reusability.
- 3. Polymorphism
Polymorphism is one of the seven essential object-oriented programming concepts, by which we can perform a single task in multiple ways. It is the combination of two Greek words : poly and morphs.
The word “poly” signifies many, while “morphs” signifies forms. Therefore, polymorphism refers to many forms.
There are two variants of polymorphism in Java known as compile-time polymorphism and runtime polymorphism.
Method overloading and method overriding are the two ways of carrying out polymorphism in Java. Overloading a static method in Java is considered as an example of compile-time polymorphism
Runtime polymorphism or Dynamic Method Dispatch is the process that allows a call to an overridden method to be resolved at runtime instead of the compile-time.
In runtime polymorphism, an overridden method is called via the reference variable of any of its superclass.
- 4. Abstraction
In Java, abstraction is the process of Data hiding, such that only the essential information is displayed to the user. The trivial or the non-essentials sections are not visible to the user.
The properties and behaviours of an object are used to differentiate it from other objects of a similar kind and also assist in classifying/grouping the objects.
Abstraction can be achieved by adding abstract classes; almost 100% abstraction can be achieved with the help of interfaces.
The abstract keyword is a non-access modifier, used for declaring classes and methods as abstract ones.
Abstract methods are used only in abstract classes, and they do not have a body. They depend on the subclass for providing the body, this makes overloading mandatory in abstract classes.
An abstract class is a restricted class, which cannot be used directly for creating an object; you must inherit it from another class in order to create objects. A few methods of an abstract class can be concrete/pre-defined; all the methods should not be abstract.
An abstract class is allowed to have parameterised constructors and the default constructor is necessarily present in an abstract class.
For Example, A car is viewed as a car rather than its individual internal components, which are not visible to the user.
- 5. All pre-defined types should be objects
Java fails the fifth necessary condition of being an object-oriented programming language as the primitive Data Type such as int, long, bool, float, char, and so on are predefined in it.
The predefined data types are not treated as objects in Java. While in a “pure” object-oriented programming language like Smalltalk both objects and primitive types are treated similarly.
int s = 50; System.out.print(s);
- 6. All user-defined types should be objects
User-defined data types are defined by the programmers/users themselves by availing of efficient features of the language.
They are related variables that store multiple values of the same or different kind. If you want a variable to hold dissimilar values user-defined data types must be used.
For example classes and interfaces are the user-defined data types of Java.
- 7. Operations performed on any object should be carried out strictly through methods invoked by the objects
A. The static keyword in Java
In Java, we can use a class declared as static without creating an object.
If you are using a static variable or a static class, you cannot call that function or variable using a dot(.) or class object, this leads to Java defying the seventh necessary object-oriented feature.
B. Wrapper Class in Java
Java wrapper class allows us to convert primitives into objects and vice-versa. Furthermore, you can also use Integer, Float, etc. rather than int, float, etc.
You can even communicate with objects without calling their methods. For example: via arithmetic operators.
A fully object-oriented language should include all the basic seven programming concepts, which are mentioned above.
And along with that, it is compulsory to be pre-defined and user-defined types, it should be treated as objects while writing the source code and, the entire range of operations should be performed strictly by invoking the methods of a class, post creating their objects.
Java supports most of the above-mentioned properties 1, 2, 3, 4, and 6 but it disagrees with properties 5 and 7.
Hence, we can conclude by saying it is not an entirely object-oriented language.
We hope that you liked reading this article, to explore more blogs on Java visit our reader‘s block and to enroll in a Java programming course, you can visit our website.
By Vanshika Singolia