Inheritance in Java
Definition:
Inheritance allows a new class (subclass) to inherit properties and behaviors (fields and methods) from an existing class (superclass), promoting code reusability and creating a hierarchy.
Key Concepts:
Superclass (Parent Class): The class whose properties and methods are inherited. Subclass (Child Class): The class that inherits from the superclass. extends Keyword: Indicates inheritance. Types of Inheritance:
Single Inheritance: A class inherits from one superclass. Multilevel Inheritance: A class inherits from a superclass, and another class inherits from that subclass. Diamond Problem:
Occurs in multiple inheritance when a class inherits from two classes that have a common superclass, leading to ambiguity. Java avoids this by not supporting multiple inheritance with classes.
Final Class:
A class declared as final cannot be inherited, ensuring integrity and security.
this and super Keywords in Java
this Keyword:
A reference to the current object, used to eliminate confusion between class attributes and parameters with the same name, call other constructors in the same class, and pass the current object as a parameter.
Uses of this Keyword:
Referencing Instance Variables: Differentiates between instance variables and parameters with the same name. Calling Another Constructor: Facilitates constructor chaining within the same class. Passing Current Object as a Parameter: Passes the current object to another method. super Keyword:
A reference to the parent class object, used to call superclass methods and constructors and access superclass variables.
Uses of super Keyword:
Calling Superclass Methods: Calls a method from the superclass that has been overridden in the subclass. Accessing Superclass Variables: Accesses variables from the superclass when they are shadowed by subclass variables. Calling Superclass Constructor: Initializes the superclass part of the subclass object. Variable Shadowing:
Occurs when a subclass declares a variable with the same name as a variable in the superclass. The superclass variable can be accessed using super.
Constructor Chaining:
Using this() to call another constructor in the same class and super() to call the superclass constructor.
Access Modifiers in Java
Types of Access Modifiers:
Public: Accessible from any other class. Protected: Accessible within the same package and by subclasses in different packages. Default (Package-private): Accessible only within the same package. Private: Accessible only within the class it is declared in. Scope of Access Modifiers:
Public: Accessible from any other class. Protected: Accessible within the same package and by subclasses in other packages. Default (Package-private): Accessible only within the same package. Private: Accessible only within the class it is declared in. Encapsulation in Java
Definition:
Encapsulation wraps data (variables) and methods (code) into a single unit (class), restricting direct access to some components and allowing controlled access through methods.
Benefits:
Data Hiding: Protects data from unauthorized access. Modularity: Groups related data and methods. Control: Provides controlled access to data. Maintenance: Enhances maintainability and flexibility. Getter and Setter Methods:
Methods used to access and update private variables, ensuring data validity.
POJO Class:
A Plain Old Java Object (POJO) is a simple class with private fields and public getter and setter methods, used to encapsulate data.
Encapsulation ensures controlled access to data and promotes data integrity, making Java applications more robust and maintainable.
Understanding these OOPs concepts helps in building efficient, modular, and maintainable software systems. For more detailed information, refer to the Notion link: