{"id":1208,"date":"2022-02-25T16:50:26","date_gmt":"2022-02-25T16:50:26","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1208"},"modified":"2022-02-25T16:50:26","modified_gmt":"2022-02-25T16:50:26","slug":"abstract-class-and-abstract-methods","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/25\/abstract-class-and-abstract-methods\/","title":{"rendered":"Abstract Class and Abstract Methods"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java abstract classes and methods with the help of examples. We will also learn about abstraction in Java.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"abstract-class\">Java Abstract Class<\/h2>\n\n\n\n<p>The abstract class in Java cannot be instantiated (we cannot create objects of abstract classes). We use the&nbsp;<code>abstract<\/code>&nbsp;keyword to declare an abstract class. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ create an abstract class\nabstract class Language {\n  \/\/ fields and methods\n}\n...\n\n\/\/ try to create an object Language\n\/\/ throws an error\nLanguage obj = new Language(); <\/code><\/pre>\n\n\n\n<p>An abstract class can have both the regular methods and abstract methods. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>abstract class Language {\n\n  \/\/ abstract method\n  abstract void method1();\n\n  \/\/ regular method\n  void method2() {\n    System.out.println(\"This is regular method\");\n  }\n}<\/code><\/pre>\n\n\n\n<p>To know about the non-abstract methods, visit\u00a0Java methods. Here, we will learn about abstract methods.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"abstract-method\">Java Abstract Method<\/h2>\n\n\n\n<p>A method that doesn&#8217;t have its body is known as an abstract method. We use the same&nbsp;<code>abstract<\/code>&nbsp;keyword to create abstract methods. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>abstract void display();<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<code>display()<\/code>&nbsp;is an abstract method. The body of&nbsp;<code>display()<\/code>&nbsp;is replaced by&nbsp;<code>;<\/code>.<\/p>\n\n\n\n<p>If a class contains an abstract method, then the class should be declared abstract. Otherwise, it will generate an error. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ error\n\/\/ class should be abstract\nclass Language {\n\n  \/\/ abstract method\n  abstract void method1();\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example: Java Abstract Class and Method<\/h3>\n\n\n\n<p>Though abstract classes cannot be instantiated, we can create subclasses from it. We can then access members of the abstract class using the object of the subclass. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>abstract class Language {\n\n  \/\/ method of abstract class\n  public void display() {\n    System.out.println(\"This is Java Programming\");\n  }\n}\n\nclass Main extends Language {\n\n  public static void main(String&#91;] args) {\n    \n    \/\/ create an object of Main\n    Main obj = new Main();\n\n    \/\/ access method of abstract class\n    \/\/ using object of Main class\n    obj.display();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>This is Java programming<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an abstract class named&nbsp;<var>Language<\/var>. The class contains a regular method&nbsp;<code>display()<\/code>.<\/p>\n\n\n\n<p>We have created the Main class that inherits the abstract class. Notice the statement,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>obj.display();<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<var>obj<\/var>&nbsp;is the object of the child class&nbsp;<var>Main<\/var>. We are calling the method of the abstract class using the object&nbsp;<var>obj<\/var>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implement\">Implementing Abstract Methods<\/h2>\n\n\n\n<p>If the abstract class includes any abstract method, then all the child classes inherited from the abstract superclass must provide the implementation of the abstract method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>abstract class Animal {\n  abstract void makeSound();\n\n  public void eat() {\n    System.out.println(\"I can eat.\");\n  }\n}\n\nclass Dog extends Animal {\n\n  \/\/ provide implementation of abstract method\n  public void makeSound() {\n    System.out.println(\"Bark bark\");\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create an object of Dog class\n    Dog d1 = new Dog();\n\n    d1.makeSound();\n    d1.eat();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Bark bark\nI can eat.<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an abstract class&nbsp;<var>Animal<\/var>. The class contains an abstract method&nbsp;<code>makeSound()<\/code>&nbsp;and a non-abstract method&nbsp;<code>eat()<\/code>.<\/p>\n\n\n\n<p>We have inherited a subclass&nbsp;<var>Dog<\/var>&nbsp;from the superclass&nbsp;<var>Animal<\/var>. Here, the subclass&nbsp;<var>Dog<\/var>&nbsp;provides the implementation for the abstract method&nbsp;<code>makeSound()<\/code>.<\/p>\n\n\n\n<p>We then used the object&nbsp;<var>d1<\/var>&nbsp;of the&nbsp;<var>Dog<\/var>&nbsp;class to call methods&nbsp;<code>makeSound()<\/code>&nbsp;and&nbsp;<code>eat()<\/code>.<\/p>\n\n\n\n<p><strong>Note<\/strong>: If the&nbsp;<var>Dog<\/var>&nbsp;class doesn&#8217;t provide the implementation of the abstract method&nbsp;<code>makeSound()<\/code>,&nbsp;<var>Dog<\/var>&nbsp;should also be declared as abstract. This is because the subclass&nbsp;<var>Dog<\/var>&nbsp;inherits&nbsp;<code>makeSound()<\/code>&nbsp;from&nbsp;<var>Animal<\/var>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"access-constructor\">Accesses Constructor of Abstract Classes<\/h3>\n\n\n\n<p>An abstract class can have constructors like the regular class. And, we can access the constructor of an abstract class from the subclass using the&nbsp;<code>super<\/code>&nbsp;keyword. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>abstract class Animal {\n   Animal() {\n      \u2026.\n   }\n}\n\nclass Dog extends Animal {\n   Dog() {\n      super();\n      ...\n   }\n}<\/code><\/pre>\n\n\n\n<p>Here, we have used the&nbsp;<code>super()<\/code>&nbsp;inside the constructor of&nbsp;<var>Dog<\/var>&nbsp;to access the constructor of the&nbsp;<var>Animal<\/var>.<\/p>\n\n\n\n<p>Note that the\u00a0<code>super<\/code>\u00a0should always be the first statement of the subclass constructor. <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"abstraction\">Java Abstraction<\/h2>\n\n\n\n<p>The major use of abstract classes and methods is to achieve abstraction in Java.<\/p>\n\n\n\n<p>Abstraction is an important concept of object-oriented programming that allows us to hide unnecessary details and only show the needed information.<\/p>\n\n\n\n<p>This allows us to manage complexity by omitting or hiding details with a simpler, higher-level idea.<\/p>\n\n\n\n<p>A practical example of abstraction can be motorbike brakes. We know what brake does. When we apply the brake, the motorbike will stop. However, the working of the brake is kept hidden from us.<\/p>\n\n\n\n<p>The major advantage of hiding the working of the brake is that now the manufacturer can implement brake differently for different motorbikes, however, what brake does will be the same.<\/p>\n\n\n\n<p>Let&#8217;s take an example that helps us to better understand Java abstraction.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: Java Abstraction<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>abstract class MotorBike {\n  abstract void brake();\n}\n\nclass SportsBike extends MotorBike {\n    \n  \/\/ implementation of abstract method\n  public void brake() {\n    System.out.println(\"SportsBike Brake\");\n  }\n}\n\nclass MountainBike extends MotorBike {\n    \n  \/\/ implementation of abstract method\n  public void brake() {\n    System.out.println(\"MountainBike Brake\");\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n    MountainBike m1 = new MountainBike();\n    m1.brake();\n    SportsBike s1 = new SportsBike();\n    s1.brake();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>MountainBike Brake\nSportsBike Brake<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an abstract super class&nbsp;<var>MotorBike<\/var>. The superclass&nbsp;<var>MotorBike<\/var>&nbsp;has an abstract method&nbsp;<code>brake()<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<code>brake()<\/code>&nbsp;method cannot be implemented inside&nbsp;<var>MotorBike<\/var>. It is because every bike has different implementation of brakes. So, all the subclasses of&nbsp;<var>MotorBike<\/var>&nbsp;would have different implementation of&nbsp;<code>brake()<\/code>.<\/p>\n\n\n\n<p>So, the implementation of&nbsp;<code>brake()<\/code>&nbsp;in&nbsp;<var>MotorBike<\/var>&nbsp;is kept hidden.<\/p>\n\n\n\n<p>Here,&nbsp;<var>MountainBike<\/var>&nbsp;makes its own implementation of&nbsp;<code>brake()<\/code>&nbsp;and&nbsp;<var>SportsBike<\/var>&nbsp;makes its own implementation of&nbsp;<code>brake()<\/code>.<\/p>\n\n\n\n<p><strong>Note<\/strong>: We can also use interfaces to achieve abstraction in Java. <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"key-points\">Key Points to Remember<\/h2>\n\n\n\n<ul><li>We use the&nbsp;<code>abstract<\/code>&nbsp;keyword to create abstract classes and methods.<\/li><li>An abstract method doesn&#8217;t have any implementation (method body).<\/li><li>A class containing abstract methods should also be abstract.<\/li><li>We cannot create objects of an abstract class.<\/li><li>To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass.<\/li><li>A subclass must override all abstract methods of an abstract class. However, if the subclass is declared abstract, it&#8217;s not mandatory to override abstract methods.<\/li><li>We can access the static attributes and methods of an abstract class using the reference of the abstract class. For example,<code>Animal.staticMethod();<\/code><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java abstract classes and methods with the help of examples. We will also learn about abstraction in Java. Java Abstract Class The abstract class in Java cannot be instantiated (we cannot create objects of abstract classes). We use the&nbsp;abstract&nbsp;keyword to declare an abstract class. For example, An abstract [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[531],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1208"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1208"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1208\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}