Differentiating Java OOP Concepts
The Object Oriented Programming (OOP) is basically a computer programming design philosophy or methodology that organizes/ models software design around data, or objects rather than functions and logic. It enables developers to model real-world objects as software components. In Java, OOP concepts are critical to creating robust, modular, and maintainable code. In this blog, we’ll dive into some of the most crucial Java OOP concepts, including abstract classes, interfaces, overloading, overriding, static, and final.
Get Kshitij Kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
Abstract Class vs Interface:
- Implementation: An abstract class can have both implemented and unimplemented methods, while an interface can only have unimplemented methods.
- Multiple Inheritance: A class can inherit from only one abstract class, but can implement multiple interfaces.
- Access Modifiers: Methods in an abstract class can have different access modifiers, while all methods in an interface are public by default.
- Constructor: An abstract class can have a constructor, while an interface cannot.
- Variables: An abstract class can have instance variables, while an interface can only have constants.
Overloading vs Overriding:
- Definition: Overloading is defining multiple methods with the same name but different parameters in the same class, while overriding is defining a method in a subclass that already exists in the superclass.
- Inheritance: Overloading is resolved at compile-time, while overriding is resolved at runtime.
- Access Modifiers: Overriding methods cannot have a lower access level than the overridden method, while overloaded methods can have a different access level.
- Return Type: Overloaded methods can have different return types, while overriding methods must have the same return type or a subtype.
- Static Methods: Overloaded methods can be static, while overriding methods cannot be static.
Super vs This:
- Definition: “super” refers to the parent class instance, while “this” refers to the current class instance.
- Use: “super” is used to call a method or constructor of the parent class, while “this” is used to refer to an instance variable or invoke a method of the current class.
- Keyword: “super” is a keyword in Java, while “this” is a keyword in both Java and C++.
- Context: “super” can be used in both static and non-static contexts, while “this” can only be used in non-static contexts.
- Constructor: “super” must be the first statement in a constructor, while “this” must be the first statement in a constructor or a method.
Static vs Final:
- Definition: “static” is used to define a class-level variable or method, while “final” is used to define a constant value or a variable that cannot be reassigned.
- Usage: “static” is used to create shared resources that can be accessed by all instances of a class, while “final” is used to create a constant value that cannot be changed.
- Initialization: “static” variables are initialized only once when the class is loaded, while “final” variables must be initialized when they are declared.
- Inheritance: “static” variables and methods are inherited by subclasses, while “final” variables and methods are not inherited.
- Modifier: “static” can be used with other modifiers like public, private, and protected, while “final” cannot be used with access modifiers.
Can we override static methods ?
No, we cannot override static methods in Java. When a method is declared as static, it belongs to the class rather than to any instance of the class. Therefore, the concept of overriding does not apply to static methods. However, it is possible to declare a static method with the same name as an instance method in the same class. In this case, the static method will hide the instance method, and the method called will depend on the reference used to invoke it.






