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 ------------------------

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (1)

Collapse
 
payilagam_135383b867ea296 profile image
Payilagam

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

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay