Posts

Showing posts with the label Java OOP Concepts

Anonymous Class in Java – Definition, Syntax, and Example

Image
Singleton Class in Java – Definition, Syntax, and Example Singleton Class in Java – Definition, Syntax, and Example Definition: A Singleton Class in Java is a design pattern that ensures only one instance of the class exists in the JVM. It provides a global point of access to that instance. Key Points: Only one instance is created during the entire lifecycle. Private constructor to prevent external instantiation. Static method to provide access to the single instance. Syntax Example: public class Singleton { private static Singleton instance; private Singleton() { // private constructor } public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } public class Main { public static void main(String[] args) { Singleton obj1 = Singleton.getInstance(); Singleton obj2 = Singleton.getInstance(); System.out....

Object-Oriented Programming (OOP) in Java

Image
Chapter 7: Object-Oriented Programming (OOP) in Java Chapter 7: Object-Oriented Programming (OOP) in Java Java is a powerful object-oriented programming language. It emphasizes designing programs using real-world entities like objects and classes. OOP helps create modular, maintainable, reusable, and scalable software systems. 🔹 Four Main Principles of OOP Encapsulation Inheritance Polymorphism Abstraction 🛡️ 1. Encapsulation in Java Encapsulation is the concept of wrapping data (variables) and methods (code) into a single unit called a class. It helps hide the internal implementation of the object from the outside world. Example: class Person { private String name; private int age; public void setName(String name) { this.name = name; } public String getName() { return name; } } Benefits: Data hiding, code maintainability, security, and flexibility. 🧬 2. Inheritance in Java ...