Java – Encapsulation, Inheritance, Abstraction , Polymorphism

Encapsulation:

  • The concept of protecting the data with in the class itself.
  • Implementation rules: (POJO rules)
    • Class is Public (to make visible to other classes).
    • Variables are Private (other objects cannot access the data directly).
    • Methods are public (to send and receive the data).

Inheritance:

  • Defining a new class by re-using the members of other class.
  • We can implement inheritance using “extends” keyword.
  • Terminology:
    • Parent/Super class: The class from which members are re-used.
    • Child/Sub class: The class which is using the members

Abstraction:

  • Abstraction is a concept of hiding implementations and shows functionality.
  • Abstraction describes “What an object can do instead how it does it?”

Polymorphism:

  • Polymorphism is the concept where object behaves differently in different situations.

Types of Relations between Classes/Objects in Java: There are three most common relationships among classes in Java that are as follows:

  1. Use-A relation: When we create an object of a class inside a method of another class, this relationship is called dependence relationship in Java, or simply Uses-A relationship.
  2. Has-A relation: When an object of one class is created as data member inside another class, it is called association relationship in java or simply Has-A relationship.
  3. Is-A relation: Is-A relationship defines the relationship between two classes in which one class extends another class.
Scroll to Top