Java Object-Oriented Programming Concepts
3 min readDec 26, 2020
This article is a part of the Java Programming Language article series.
Object-Oriented
- A programming language or a technique that supports classes and objects
- Java is an object-oriented programming language
Class
- Is a blueprint
- Has properties, methods, and nested classes
Object
- Instance that is created from class
- Instance is an executable copy of the class
- Models the real-world objects
- Two characteristic: state and behavior
- State: name, color, speed, on, off, volume
- Behavior: changing gear, turn on, turn off, increase volume, decrease volume
- state = fields
- behavior = methods
Class Types
- Nested Classes
- Anonymous Classes
Nested Classes
- Types: Static Nested Classes, Inner Classes
- Logically grouping classes that are only used in one place
- Increases encapsulation
Anonymous Classes
- Declare and instantiate a class at the same time
Enum Types
- A set of predefined constants
- Compass directions, the days of the week
Object-Oriented Programming Features
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Abstraction
- Provided by interface and abstract class
Interface
- Interaction with the outside world through the methods
- Forms a contract between the class and the outside world
- All methods must be implemented in the class
- public, static, final properties (fields)
- public methods
Abstract Class
- Abstract classes cannot be instantiated, but they can be subclassed
- Not static and final properties (fields) can be defined
- public, protected, private methods can be defined
Abstract Method
- An abstract method is a method that is declared without an implementation
- If a class includes abstract methods, then the class itself must be declared abstract
Abstract Classes vs Interfaces
Abstract classes
- Share code with related classes
- Have many common methods or fields
Interfaces
- Implementation with unrelated classes
- Multiple inheritance
When an Abstract Class Implements an Interface
- An abstract class does not need to implement all of the interface’s methods
Encapsulation
- Hiding properties and methods
Access level modifiers
- public: Full access
- protected: Subclass access
- no modifier: Package access
- private: Class access
Data Encapsulation
- Hiding internal state and interact through methods
Inheritance
- Using common states and behaviors from other classes
- All classes inherits from the java.lang.Object class
- Interface is used for multiple inheritance
- superclass: base, parent class
- subclass: derived, extended, child class
Polymorphism
- A subclass can define its own unique behaviors while inheriting superclass features
- Override parent class methods with different definitions in subclasses
The this Keyword
- Within an instance method or a constructor, this is a reference to the current object
The final keyword
- The methods declared final cannot be overridden by subclasses
- Methods called from constructors should generally be declared final to avoid surprising or undesirable results
- A class that is declared final cannot be subclassed
Static Method
Method Overloading
- Declaring methods with different method signatures
Overriding and Hiding Methods
Instance Methods
- A subclass method with the same signature and return type overrides the superclass method
Static Methods
- A static subclass method with the same signature hides the static superclass method
Packages
- Namespace that organizes a set of related classes and interfaces
- Similar to folders on a computer