Showing posts with label abstraction. Show all posts
Showing posts with label abstraction. Show all posts

Jul 10, 2020

Abstraction in JAVA | Java Language | Coding Winds

ABSTRACTION

Hey guys, today we are back with another blog, so this far we have discussed the three pillars of OOPS namely, inheritance, polymorphism, encapsulation but today we will be discussing the last pillar of OOPs i.e., abstraction.

Suppose one day you make an app for a client, and after few days that client comes back to you and says that the app you made him has some bugs. You get that issue fixed in couple of minutes and then start telling the details about why the app was not working fine to your client, to this your client might get annoyed or might ask to leave because he being a non-tech guy, why would you talk about the technicalities of the app to him? So abstraction is kinda same thing, it gets the work done without telling the user non- essential details.

So before diving right into abstraction let us first discuss about the abstract keyword as well as abstract methods and classes:

·       So abstract keyword when used with classes make abstract classes.

·       Abstract classes can contain abstract  methods but an abstract method must be in an abstract class.

·       Abstract methods are only methods names (or methods without body ) and their implementation is written in their sub-classes or in the classes in which they are inherited.

·       One cannot create objects out of abstract classes.

 

Now let's see the example of abstraction in java.

abstract class Test{

     public abstract void John();

     public abstract void Hannah();

     public void Lisa() {

          System.out.println("Lisa scored highest in the Test");

      }

}

 

class Rank extends Test {

     public void John() {

         System.out.println("John scored third highest in the Test");

     }

     public void Hannah() {

         System.out.println("Hannah scored second highest in the Test ");

     }

}

public class Coding_Winds {

     public static void main(String[] args) {

          Rank obj = new Rank();

          obj.Lisa();

          obj.Hannah();

          obj.John();

          }

}

Output:

Lisa scored highest in the Test

Hannah scored second highest in the Test

John scored third highest in the Test


So by now you might be wondering that what could be the difference between encapsulation and abstraction since both of them are used for data hiding. Well considering the example at this beginning of the blog, you can learn that abstraction is used to achieve more simpler program and make available only the essential information . But encapsulation is used to make a part of the code private or hidden from everyone for security reasons , basically it used for data privacy and binds the code and data into single unit.

Abstraction in java can also be achieved with Interfaces .


Hope you are clear on this  topic do read our more articles on JAVA LANGUAGE.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


This article is SUBMITTED By : Pranjal Rai


Do subscribe to our daily blog update by clicking here.


Thank You!