Day 10 – How is abstraction achieved in java?

Abstraction permits us to cover our implementation. It is likely one of the key properties of Object-Oriented Programming (OOP). In java, it’s applied utilizing summary class & Interface.



Summary class

We will merely create an summary class by together with the summary key phrase in entrance of it. An summary class can’t be instantiated. So no objects of an summary class or constructors are allowed.


summary class Animal {
    int kind;
    summary void transfer();
}

Enter fullscreen mode

Exit fullscreen mode



Interface

The interface is a blueprint of a category that’s used to specify its habits. Once more an interface can’t be instantiated. It will probably have solely summary capabilities. It can not have any operate physique. So we needn’t specify the summary key phrase.


interface Form{
    String colour;
    void draw();
}

Enter fullscreen mode

Exit fullscreen mode



When to make use of Summary Class?

If we’ve got a use case the place “A is a B” think about using an summary class.

Eg- Lion is an Animal , Neem is a Tree



When to make use of interface?

If we’ve got a use case the place “A can do some exercise of B” think about using an summary class.

Eg- Scream can provide the sound of an Animal , Blossom can provide a flower of a Tree

Add a Comment

Your email address will not be published. Required fields are marked *