Java OOP Concepts Repository
This repository contains examples demonstrating core Object-Oriented Programming (OOP) concepts in Java. Each file covers a specific concept, showing how it can be implemented and used. This is intended for beginners and intermediate Java developers looking to understand or reinforce OOP principles.
Table of Contents
Classes and Objects
Constructors
Encapsulation
Inheritance and Constructor Chaining
Polymorphism
Abstraction
Interfaces
- Classes and Objects
This example demonstrates the basic structure of a class and how to create objects from a class. It includes defining instance variables, methods, and creating an object to call these methods.
Concepts Covered:
Defining a class
Creating an object
Accessing methods and variables
- Constructors
This example covers the concept of constructors in Java. It explains how to initialize objects with a constructor, including parameterized and default constructors.
Concepts Covered:
Default constructor
Parameterized constructor
- Encapsulation
This example demonstrates how to achieve encapsulation by using private fields and providing public getter and setter methods to control access to these fields.
Concepts Covered:
Data hiding using private access
Getters and setters
Encapsulating class members
- Inheritance and Constructor Chaining
This example shows how inheritance works in Java and how constructor chaining allows parent and child classes to initialize their data in a structured manner.
Concepts Covered:
Extending a class (inheritance)
Super keyword
Constructor chaining between parent and child classes
- Polymorphism
This example demonstrates both compile-time and run-time polymorphism in Java. Compile-time polymorphism is achieved through method overloading, and run-time polymorphism is achieved through method overriding.
Concepts Covered:
Method overloading (compile-time polymorphism)
Method overriding (run-time polymorphism)
- Abstraction
This example illustrates abstraction by using an abstract class and implementing its abstract methods in concrete subclasses.
Concepts Covered:
Abstract classes
Abstract methods
Concrete implementations
- Interfaces
This example shows how interfaces allow unrelated classes to implement common methods without being in a hierarchical relationship. A USBDevice interface is implemented by both a Printer and a Scanner class.
Concepts Covered:
Interface definition
Interface implementation in unrelated classes
How to Run
Clone the repository:
git clone https://github.com/your-username/java-oop-concepts.git
Navigate to the project folder:
cd java-oop-concepts
Compile and run each Java file using:
javac .java
java <filename without .java>
Contributing
If you'd like to contribute, feel free to fork this repository, make your changes, and create a pull request. Contributions are welcome!
License
This repository is licensed under the MIT License.
Author- DevsInProgress