Introduction
In this article, we will take a look into why Java is not a 100% purely or fully OOP (Object-Oriented Programming) language. Java is widely recognized as an object-oriented programming (OOP) language, offering powerful features such as inheritance, polymorphism, encapsulation, and abstraction. However, it is often debated whether Java is purely object-oriented.
Seven Qualities of a Purely Object-Oriented Language
There are seven qualities to be satisfied for a programming language to be purely Object Oriented:
- Encapsulation/Data Hiding
- Inheritance
- Polymorphism
- Abstraction
- All predefined types are objects
- All user-defined types are objects
- All operations performed on objects must be only through methods exposed to the objects.
Java supports qualities 1, 2, 3, 4, and 6 but fails to support qualities 5 and 7 as shown in the diagram below:
Why Java is not a Pure or Fully Object-Oriented Programming Language?
Java is not a pure OOP language due to two main reasons:
Primitive Data Types
The first reason is that the Object-oriented programming language should only have objects, whereas Java contains 8 primitive data types like char
, boolean
, byte
, short
, int
, long
, float
, and double
which are not objects. These primitive data types can be used without the use of any object.
Example:
public class IsJavaFullyOOPS {
public static void main(String[] args) {
int i = 10;
byte b = 20;
short s = 30;
long l = 100L;
double d = 100.10;
float f = 200f;
boolean flag = true;
char c = 'R';
System.out.println(i);
System.out.println(b);
System.out.println(s);
System.out.println(l);
System.out.println(d);
System.out.println(f);
System.out.println(flag);
System.out.println(c);
}
}
Output:
10
20
30
100
100.1
200.0
true
R
Even using Wrapper classes does not make Java a pure OOP language, as internally, it will use operations like Unboxing and Autoboxing. So, if you create use Integer instead of int and do any mathematical operation on it, under the hood, Java is going to use primitive type int
only.
Static Keyword
The second reason is related to the static
keyword. In a pure object-oriented language, we should access everything through objects. However, Java contains static variables and methods that can be accessed directly without using objects.
Example:
public class IsJavaFullyOOPS {
private static String message = "hello";
public static void main(String[] args) {
// calling message instance variable without object
System.out.println(message);
// calling demo static method without object
demo();
}
private static String demo(){
return "hello from method";
}
}
Conclusion
Java is a powerful and versatile object-oriented programming language, but it is not purely object-oriented. The inclusion of primitive data types and static methods and variables breaks some of the core principles of a purely object-oriented language. These features are designed to provide performance benefits and practical functionality but deviate from the strict principles of a purely object-oriented language.
Despite these deviations, Java remains a robust and widely used language that effectively supports object-oriented programming principles. This makes it a favorite among developers for building a wide range of applications.
Further Reading:
- What is a Class in Java with Programming Examples
- Abstraction in Java with Example
- Encapsulation in Java with Example
- Inheritance in Java with Example
- Polymorphism in Java with Example
YouTube Video
Watch the YouTube video below to understand more about why Java is not a Pure or Fully Object-Oriented Programming Language:
Comments
Post a Comment
Leave Comment