Skip to main content

Command Palette

Search for a command to run...

Java Series #2a: Understanding OOPs in Java

Java OOP Core Concepts: Objects, Methods, Constructors, and OOP Pillars

Updated
3 min read
Java Series #2a: Understanding OOPs in Java
A

I am a Full-stack dev turning ideas into sleek, functional experiences 🚀. I am passionate about AI, intuitive UI/UX, and crafting user-friendly platforms . I am always curious – from building websites to diving into machine learning and under the hood workings ✨. Next.js, Node.js, MongoDB, and Tailwind are my daily tools. I am here to share dev experiments, lessons learned, and the occasional late-night code breakthroughs. Always evolving, always building.

1. Objects and Classes

A class is a blueprint. An object is the actual instance created from that blueprint.
Each object has:

  • State (values)

  • Behavior (methods)

  • Identity (reference in memory)

Java provides a default constructor if no constructor is defined.


2. Ways to Create Objects

2.1 Using new keyword

The most common method.

Test t = new Test();

2.2 Using Reflection

Load class dynamically, then create an instance.

Class cls = Class.forName("Test");
Object obj = cls.getConstructor().newInstance();

2.3 Using clone()

Create a new object by copying an existing one.

Test t1 = new Test();
Test t2 = (Test)t1.clone();

2.4 Serialization & Deserialization

Object → File → Object. Used for deep copying or transferring objects.

2.5 Anonymous Object

Created without reference, used once.

new Test().display();

2.6 Multiple Objects via Upcasting

A single parent reference can point to different child objects.
Old child reference becomes unreachable → eligible for garbage collection.


3. Methods in Java

Method structure:
modifier returnType methodName(parameters) { }

Types of methods

  • Predefined methods (from Java libraries)

  • User-defined methods

Based on binding

  • Instance methods → require object

  • Static methods → class-level, no object needed

Method Signature

Defined by:

  • Parameter count

  • Parameter types

  • Parameter order

Return type is not part of the method signature.

Calling Methods

  • Instance methods via object

  • Static methods via class name

  • Abstract methods must be implemented by child classes


4. Constructors

Constructors initialize objects. They have no return type and share the class name.

Types

  • Default constructor (provided by compiler)

  • Parameterized constructor

  • Copy constructor (manually created in Java)

  • Private constructor (controls object creation)

Constructor Overloading

Multiple constructors with different parameter lists.

this keyword

Used to:

  • Access current object's variables

  • Call current class constructors

  • Improve naming clarity


5. Encapsulation

Bundling data and methods.
Achieved by:

  • Private variables

  • Public getters and setters

Ensures controlled access.


6. Inheritance

A class acquires properties and behaviors of another class.

Types:

  • Single

  • Multilevel

  • Hierarchical

  • Hybrid (combination)

  • Multiple inheritance is only allowed via interfaces

Inheritance represents an **"is-a" relationship".

Upcasting

Parent reference → Child object

Parent p = new Child();

Useful for polymorphism.

super keyword

Used to access:

  • Parent class variables

  • Parent class methods

  • Parent constructor (must be first statement)

Parent class must have the constructor being called, or the compiler provides default ones accordingly.


7. Polymorphism

Compile-time polymorphism (Method Overloading)

Same method name with different parameters.

Runtime polymorphism (Method Overriding)

Child class provides its own implementation of a parent method.
Decided at runtime based on object type.
Enabled by dynamic method dispatch.

Parent p = new Child();  
p.show(); // executes Child's show()

Return type does not matter in runtime polymorphism; method signature must match.


8. Abstraction

Hiding implementation details and exposing only essential features.

Abstract Class

  • Can have abstract and concrete methods

  • Can contain variables

  • Cannot be instantiated

  • Extended by child classes

Represents partial abstraction.


9. Interface

A completely abstract structure (before Java 8).
Used to achieve multiple inheritance.

Modern Interface capabilities

  • Abstract methods

  • Default methods

  • Static methods

  • Private methods (Java 9+)

  • Variables are allowed but must be:

    • public static final

A class implementing an interface must implement all abstract methods unless declared abstract itself.

Read more about 4 pillers of OOPS from 4-pillars-of-object-oriented-programming