Java OOP

Java OOP (Object-Oriented Programming)

Object-Oriented Programming (OOP) in Java is a programming paradigm based on objects and classes. Java is a pure OOP language, and OOP concepts make code modular, reusable, and easier to maintain.


1. Key Concepts of Java OOP

  1. Class – Blueprint for creating objects.

  2. Object – Instance of a class.

  3. Encapsulation – Wrapping data (variables) and methods together.

  4. Abstraction – Hiding complex implementation and showing only functionality.

  5. Inheritance – One class can inherit properties and methods of another.

  6. Polymorphism – Ability of objects to take multiple forms.

    • Compile-time (Method Overloading)

    • Run-time (Method Overriding)


2. Class and Object Example


 

📌 Output:

Brand: Toyota, Year: 2022
Brand: Honda, Year: 2023

3. Encapsulation Example


 

📌 Output:

Name: Alice

Encapsulation protects data by using private variables and public getter/setter methods.


4. Inheritance Example


 

📌 Output:

Animal is eating
Dog is barking

5. Polymorphism Example

a) Compile-time Polymorphism (Method Overloading)


 

b) Run-time Polymorphism (Method Overriding)


 

📌 Output:

Cat meows

6. Abstraction Example

Option 1: Abstract Class


 

Option 2: Interface


 


🧠 Key Takeaways

OOP ConceptDescription
Class & ObjectBlueprint & instance
EncapsulationHide data, provide getter/setter
InheritanceReuse parent class properties
PolymorphismMultiple forms (overloading/overriding)
AbstractionHide complexity, show functionality

You may also like...