{"id":518,"date":"2016-01-04T13:51:54","date_gmt":"2016-01-04T13:51:54","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=518"},"modified":"2016-02-24T12:14:10","modified_gmt":"2016-02-24T12:14:10","slug":"abstraction","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/abstraction","title":{"rendered":"Abstraction Tutorial With Example In JAVA"},"content":{"rendered":"<p>In Java, Abstraction is one of the major building block. It is a process of hiding internal working and showing only necessary details. In simple form abstraction means:<\/p>\n<ul>\n<li>Show Functionality<\/li>\n<li>Hide Complexity<\/li>\n<\/ul>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> Interface is used to achieve 100% Abstraction in JAVA. Here we only discuss Abstraction in full details but we recommend you to learn Abstraction and Interface topic simultaneously as both topics are similar with little bit difference.<\/p>\n<hr \/>\n<h4><strong>Explanation Of Abstraction:<\/strong><\/h4>\n<p>The best explanation of Abstraction is using the common example of sending SMS. When a user send SMS, he simply enter the address, type message in the text box and send it. He doesn\u2019t know what is happening behind the scene when this message is getting send to the other user. So here implementation details has been hided from the user and only functionality is presented to user. This is achieved in JAVA either through Abstraction or Interface.<\/p>\n<hr \/>\n<h4><strong>Key Learning Points On Abstraction:<\/strong><\/h4>\n<p>Before we share syntax you first need to understand abstract class, abstract methods and why\/how to extend Abstract class:<\/p>\n<p><span style=\"color: #008000;\"><strong>What Is Abstract Class:<\/strong><\/span><\/p>\n<p>An abstract class is declared with abstract keyword which may contain methods without body, with body or mixture of both. If a class have at least one method without body then it has to be declared abstract.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> Method without body means method without any functionality or implementation.<\/p>\n<p>In the below example you can see one method calling is without body. So here class has to be declared abstract because it has one method without body.<\/p>\n<pre>abstract class Mobile{\r\nabstract void calling();\r\nabstract void messaging()\r\n{\r\nSystem.out.println(\"Messaging\");\r\n}\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>What Is Abstract Method:<\/strong><\/span><\/p>\n<p>The methods without body or methods with only signatures are called abstract methods. The abstract method doesn\u2019t have any implementation details. It is declared using abstract keyword before method name. For example, calling method is an abstract method.<\/p>\n<pre>abstract void calling();<\/pre>\n<p><strong>Syntax: of Abstract Method <\/strong><\/p>\n<pre>abstract void method_name();<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> In order to use abstract method we first need to override that method in subclass (i.e. child class).<\/p>\n<p><span style=\"color: #008000;\"><strong>Why And How To Extend Abstract Class:<\/strong><\/span><\/p>\n<p>As now you already know Abstract class may contains abstract method without any implementation and thus we cannot instantiate abstract class. In case we try instantiating it, the object might be useless because methods inside abstract class may not have any implementation at all. So to avoid this situation, JAVA doesn\u2019t allow us to instantiate abstract class.<\/p>\n<p>We first need to extend abstract class where we will do the implementation of those abstract method and then we instantiate this new class.<\/p>\n<p><strong>So let\u2019s see how to extend Abstract class:<\/strong><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> First we define abstract class with abstract method:<\/p>\n<pre>abstract class Mobile{\r\nabstract calling(); \/\/Method without body\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Now we extends abstract class and implement abstract methods:<\/p>\n<pre>class Samsung extends Mobile{\r\nvoid calling();\r\n{\r\nSystem.out.println(\"Start Calling\");\r\n}\r\n}<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>Pro Note:<\/strong><\/span> An example of abstract class is AbstractMap which is part of collection framework include TreeMap, HashMap and ConcurrentHashMap and share many methods like isEmpty(), put() , get(), containValue() , containKey() etc that AbstractMap defines.<\/p>\n<hr \/>\n<h4><strong>Syntax Of Abstraction:<\/strong><\/h4>\n<p>The syntax of abstraction start with abstract keyword before class name. It then contains mixture of abstract and non-abstract methods.<\/p>\n<pre>abstract class &lt;ClassName&gt;{\r\n\/\/Mixture of abstract methods(Without body\/implementation) and non-abstract methods(with body\/implementation)\r\n\/\/If it contains atleast one abstract method then a class has to be declared abstract\r\n}<\/pre>\n<hr \/>\n<h4><strong>Examples Of Abstraction:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>Example 1:<\/strong><\/span> Lets now understand abstraction concept using real life examples of different sounds created by animals. For example Cat does Meow and Lion Does Roar. We will display different sounds using Abstraction in JAVA.<\/p>\n<p><strong>Step 1:<\/strong> First create a new project in Eclipse and create a abstract class Sound. Below is the code of Sound.java<\/p>\n<pre>abstract class Sound {\r\n\u00a0\u00a0 \u00a0\r\n\/\/Non-abstract method i.e. method with implementation\r\n\u00a0\u00a0 \u00a0public void soundmessage(){\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.print(\"Animal Sound\");\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\r\n\/\/Abstract method i.e. without body\/implementation\r\n\u00a0\u00a0 \u00a0abstract void sound();\r\n\r\n}<\/pre>\n<p><strong>Step 2:<\/strong> Now create another class name Cat which extends Sound class. Here we will implement abstract sound() method for Cat. Below is the code of Cat.java<\/p>\n<pre>class Cat extends Sound{\r\n\r\nvoid sound(){\r\n\tsoundmessage();\r\n\tSystem.out.println(\" of Cat: Meow\");\r\n}\r\n\r\n}<\/pre>\n<p><strong>Step 3:<\/strong> In the same way we are creating another Lion class which extends Sound class. Here also we will implement the sound() method but for Lion. Below is the code of Lion.java:<\/p>\n<pre>class Lion extends Sound {\r\n\t\r\n\tvoid sound(){\r\n\t\tsoundmessage();\r\n\t\tSystem.out.println(\" of Lion: Roar\");\r\n\t}\r\n\r\n}<\/pre>\n<p><strong>Step 4:<\/strong> Now create AnimalSound main class. Here first we will create object of Cat &amp; Lion class and then we will call sound() method on these objects. Below is the code of AnimalSound.java<\/p>\n<pre>public class AnimalSound {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t\r\n\t\tCat cat = new Cat();\r\n\t\tcat.sound();\r\n\t\t\r\n\t\tLion lion = new Lion();\r\n\t\tlion.sound();\r\n\t}\r\n\r\n}<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now run the program and you will see sounds of Cat and Lion printed.<\/p>\n<pre>Animal Sound of Cat: Meow\r\nAnimal Sound of Lion: Roar<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>Conclusion:<\/strong><\/span> So you can see how useful is Abstraction. We just define mixture of abstract and non-abstract methods in Abstract class and then implement abstract method in child class (i.e. sub class) according to requirement. In the end same method gives different result depending on Objects of which sub-class. Remember we can&#8217;t instantiate Abstract class as discussed earlier.<\/p>\n<p><span style=\"color: #008000;\"><strong>Example 2:<\/strong><\/span> In second Abstraction example we simply display a text &#8220;AbhiAndroid&#8221; by using an abstract method of an abstract Base class. In this, Child class extends the Base class and override the display method and then prints the text as shown in below example.<\/p>\n<p>Here in this example we have provided lots of explanation in the code itself as comments.<\/p>\n<p><strong>Below is the code of Base.java<\/strong><\/p>\n<pre>abstract class Base \/\/abstract class\r\n{\r\nabstract void display(); \/\/abstract method\r\n}<\/pre>\n<p><strong>Below is the code of Child.java and also main function of class<\/strong><\/p>\n<pre>class Child extends Base  \/\/child class which extends the property of  base class\r\n{\r\nvoid   display() \/\/ override method of  base class\r\n {\r\n   System.out.println(\"AbhiAndroid\");  \/\/prints the text \u201cAbhiAndroid\u201d as an output\r\n}\r\npublic static void main(String args[]) \/\/main function of class\r\n{\r\n  Child c=new Child(); \/\/create an object of child class\r\n  c.display(); \/\/call the display method of child class with the help of  object\r\n}\r\n}<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>OUTPUT:<\/strong><\/span><\/p>\n<pre>AbhiAndroid<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>Conclusion:<\/strong><\/span> In the above example, Base is abstract class that contain abstract method display(). Its implementation is provided by the Child class.<\/p>\n<hr \/>\n<h4><strong>Importance Of Abstraction<\/strong><\/h4>\n<p>Below is the some major important points of abstraction in java.<\/p>\n<ul>\n<li>Abstraction helps to reduce the complexity and also improves the maintainability of the system.<\/li>\n<li>Abstraction gives more power to the object oriented programming languages when used with the concepts like encapsulation and polymorphism.<\/li>\n<li>Abstraction is used to provide solution to real world problem.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Difference Between Abstract Class And Interface:<\/strong><\/h4>\n<p>Abstract class is quite similar to interface like both contain abstract method and also we can\u2019t initiate both of them. But below are the some key differences between\u00a0 both the topics:<\/p>\n<p><span style=\"color: #008000;\"><strong><u>Abstract class:<\/u><\/strong><\/span><\/p>\n<ol>\n<li>Abstract class can extend only one class at a time.<\/li>\n<li>Abstract class can have both abstract (method without implementation) and concrete methods (method with implementation).<\/li>\n<li>In abstract class \u2018abstract\u2019 keyword is used to declare a method as abstract.<\/li>\n<li>Abstract class can have protected, public and public abstract methods.<\/li>\n<li>Abstract class can have static, final or static final variables with any access specifier.<\/li>\n<\/ol>\n<p><span style=\"color: #008000;\"><strong><u>Interface:<\/u><\/strong><\/span><\/p>\n<ol>\n<li>Interface can extend any number of interfaces at a time.<\/li>\n<li>Interface can only have abstract methods, they cannot have concrete methods.<\/li>\n<li>In interfaces, \u201cabstract\u201d keyword is optional to declare a method as an abstract because all the methods are abstract by default.<\/li>\n<li>Interfaces can have only public abstract methods i.e by default.<\/li>\n<li>Interfaces can have only static final variable i.e by default.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In Java, Abstraction is one of the major building block. It is a process of hiding internal working and showing only necessary details. In simple form abstraction means: Show Functionality Hide Complexity Important Note: Interface is used to achieve 100% Abstraction in JAVA. Here we only discuss Abstraction in full details but we recommend you &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/abstraction\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Abstraction Tutorial With Example In JAVA<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-518","page","type-page","status-publish","hentry"],"psp_head":"<title>Abstraction Tutorial With Example In JAVA \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Complete tutorial on Abstraction with lots of examples in JAVA. Learn about abstract class, abstract method and importance. Also find Interface difference with Abstraction.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/abstraction\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=518"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/518\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}