DEV Community

silambarasan rajendran
silambarasan rajendran

Posted on

3

day-26: Encapsulation and Abstraction in Java

Encapsulation:
In Java that involves bundling the data (variables) and the methods (functions) that operate on the data into a single unit, known as a class. It restricts direct access to some of an object's components and can prevent the accidental modification of data.

1) private    - Accessible within the same class only
2) default    - Accessible within the same package (no keyword used)
3) protected  - Accessible within the same package AND subclasses in other packages
4) public     - Accessible from anywhere
Enter fullscreen mode Exit fullscreen mode

Use this When you...
--> import Want to use a class from a different package
--> extends Want to inherit functionality from another class

Example code for protected:

package housingloandpackage;

public class HDFC {
    protected void education_load () {
        System.out.println("Educational load");
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }
}


package educationloanpackage;
import housingloandpackage.HDFC;

public class IOB extends HDFC {

    public static void main(String[] args) {
        IOB iob = new IOB();
        iob.education_load();
    }
}


package educationloanpackage;
import housingloandpackage.HDFC;

public class SBI {

    public static void main(String[] args) {
        HDFC hdfc = new HDFC();
        hdfc.emp_salary();
    }
}
Enter fullscreen mode Exit fullscreen mode

IS-A Relationship only we can use extends key.
Note:
--> If we extent the class we can use our own object
or else we need to create new object for the class.

Abstraction
Abstraction in Java is a fundamental object-oriented programming concept that involves hiding complex implementation details and presenting only the essential features to the user.

public abstract class Parents {

    public abstract void study();

    public void grow() {
        System.out.println("Take Care");
    }

    public static void main(String[] args) {
        //Parents parents = new Parents(); 
        //parents.study(); Can't do that. 
    }
}
Enter fullscreen mode Exit fullscreen mode

Here:
1). Class and Method can be abstract.
2). It cannot be instantiated directly. - Can't create Object.
3). If at least one method is abstract in a class, then the entire class should be abstract
4). Abstract classes cannot be instantiated, but they can be subclassed.

-------------------------- End of the Blog ------------------------

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server ⏰

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

Top comments (1)

Collapse
 
payilagam_135383b867ea296 profile image
Payilagam

Good! Keep it up! Consistency is the key to Success and you already have it!

Dev Diairies image

User Feedback & The Pivot That Saved The Project

🔥 Check out Episode 3 of Dev Diairies, following a successful Hackathon project turned startup.

Watch full video 🎥

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay