{"id":1879,"date":"2018-12-21T12:00:00","date_gmt":"2018-12-21T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/abstraction-in-java\/"},"modified":"2026-07-05T03:21:52","modified_gmt":"2026-07-05T01:21:52","slug":"abstraction-in-java","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/abstraction-in-java\/","title":{"rendered":"Abstraction in Java"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-265 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Abstraction-in-Java-300x165.jpg\" alt=\"\" width=\"300\" height=\"165\" \/><\/p>\n<p>We would cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">What is Abstraction?<\/a><\/li>\n<li><a href=\"#t2\">Abstract Classes and Methods in Java<\/a><\/li>\n<li><a href=\"#t3\">Example of Abstract Class in Java<\/a><\/li>\n<li><a href=\"#t4\">Inheriting an Abstract Class<\/a><\/li>\n<li><a href=\"#t5\">Instantiating an Inherited Abstract Class<\/a><\/li>\n<li><a href=\"#t6\">Abstract Methods in Java<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h5 id=\"t1\"><strong>1.\u00a0 What is Abstraction?<\/strong><\/h5>\n<p>Abstraction in Object Oriented Programming is the concept of making the user focus on the relevant aspects of the problem without getting much involved in the low-level details.<\/p>\n<p>For example, we all use the remote control to operate electronic devices. Here we focus on how to use the buttons to operate the device, but we don&#8217;t get involved in the electronic principles that guides the communication between the remote control and the device. This low level details is hidden from the user and in fact is not relevant to the user.<\/p>\n<p>In the same way,\u00a0 in OOP, abstraction hides the implementation details from the user while presenting only the relevant features. This is also referred to as Information Hiding.<\/p>\n<p>Abstraction in java is achieved by the use of abstract classes, abstract methods and interfaces.<\/p>\n<p>&nbsp;<\/p>\n<h5 id=\"t2\"><strong>2. Abstract Classes and Methods<\/strong><\/h5>\n<p>An abstract class is created using the abstract keyword in the class declaration. The following rules applies to abstract classes and methods:<\/p>\n<ul>\n<li>Abstract class is declared using the abstract keyword<\/li>\n<li>An abstract class may or may not contain an abstract method (method without a body)<\/li>\n<li>If a class contains an abstract method, then that class must be declared as an abstract class<\/li>\n<li>An abstract class cannot be instantiated i.e you cannot create an object from an abstract class<\/li>\n<li>To use an abstract class, you must create another class that inherits from the abstract class. This subclass would then provide implementation for the abstract methods<\/li>\n<li>Derived classes from abstract classed provide implementation for the abstract methods<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h5 id=\"t3\"><strong>3. Abstraction Example<\/strong><\/h5>\n<p>Here we would go through an example of an abstract class. Let&#8217;s create class called <strong>Person<\/strong>.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/*<\/span>\n<span style=\"color: #888888;\"> * Abstract class Person<\/span>\n<span style=\"color: #888888;\"> * Written by Kindson The Genius<\/span>\n<span style=\"color: #888888;\"> * Date : December 21, 2018<\/span>\n<span style=\"color: #888888;\"> *\/<\/span>\n\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.time.LocalDate<\/span><span style=\"color: #333333;\">;<\/span>\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.time.Period<\/span><span style=\"color: #333333;\">;<\/span>\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.Date<\/span><span style=\"color: #333333;\">;<\/span>\n\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">abstract<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Person<\/span> <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> String Firstname<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> String Lastname<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> String Address<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> LocalDate DateOfBirth<span style=\"color: #333333;\">;<\/span>\n\t\n\t<span style=\"color: #888888;\">\/\/Constructor with all four parameters<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Person<\/span><span style=\"color: #333333;\">(<\/span>String Firstname<span style=\"color: #333333;\">,<\/span> String Lastname<span style=\"color: #333333;\">,<\/span> String Address<span style=\"color: #333333;\">,<\/span> LocalDate DateOfBirth<span style=\"color: #333333;\">)<\/span>\n\t<span style=\"color: #333333;\">{<\/span>\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Creating a new person object\"<\/span><span style=\"color: #333333;\">);<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">Firstname<\/span> <span style=\"color: #333333;\">=<\/span> Firstname<span style=\"color: #333333;\">;<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">Lastname<\/span> <span style=\"color: #333333;\">=<\/span> Lastname<span style=\"color: #333333;\">;<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">Address<\/span> <span style=\"color: #333333;\">=<\/span> Address<span style=\"color: #333333;\">;<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">DateOfBirth<\/span> <span style=\"color: #333333;\">=<\/span> DateOfBirth<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n\t\n\t<span style=\"color: #888888;\">\/\/Constructor with no parameter<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Person<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\t\n\t<span style=\"color: #333333;\">}<\/span>\n\t\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> String <span style=\"color: #0066bb; font-weight: bold;\">Fullname<\/span><span style=\"color: #333333;\">()<\/span>\n\t<span style=\"color: #333333;\">{<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"Name: \"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">Firstname<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\", \"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">Lastname<\/span><span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n\t\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">setAddress<\/span><span style=\"color: #333333;\">(<\/span>String Address<span style=\"color: #333333;\">)<\/span>\n\t<span style=\"color: #333333;\">{<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">Address<\/span> <span style=\"color: #333333;\">=<\/span> Address<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n\t\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">GetAge<\/span><span style=\"color: #333333;\">()<\/span>\n\t<span style=\"color: #333333;\">{<\/span>\n\t\tLocalDate currentDate <span style=\"color: #333333;\">=<\/span> LocalDate<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">now<\/span><span style=\"color: #333333;\">();<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> Period<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">between<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">DateOfBirth<\/span><span style=\"color: #333333;\">,<\/span> currentDate<span style=\"color: #333333;\">).<\/span><span style=\"color: #0000cc;\">getYears<\/span><span style=\"color: #333333;\">();<\/span>\t\n\t<span style=\"color: #333333;\">}<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<\/div>\n<p>Listing 1.0: Abstract class for Person<\/p>\n<p>&nbsp;<\/p>\n<p>Copy this program in Listing 1.0 into your java IDE and run it. Make sure you add the necessary imports.\u00a0 Everything works fine. The only problem is that this class is abstract and so we cannot instantiate. You can try to instantiate it using the code below\u00a0 in listing 1.1 in the main method and check what happens.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">PersonTester<\/span> <span style=\"color: #333333;\">{<\/span>\n\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\t<span style=\"color: #888888;\">\/\/Creating and instance of the person class<\/span>\n\t\tPerson person <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Person<span style=\"color: #333333;\">();<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<\/div>\n<p>Listing 1.1: Creating an instance of an abstract class<\/p>\n<p>If you run to code above that tries to create an instance of the Person class, you will get the error below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\">Exception in thread <span style=\"color: #a31515;\">\"main\"<\/span> java.lang.Error: Unresolved compilation problem: \n\tCannot instantiate the type Person\n\n\tat abstractstuff.PersonTester.main(PersonTester.java:7)\n<\/pre>\n<\/div>\n<p>Listing 1.2: You cannot instantiate an abstract class<\/p>\n<p>&nbsp;<\/p>\n<h5 id=\"t4\"><strong>4. Inheriting an Abstract Class<\/strong><\/h5>\n<p>To use the abstract class, we need to create a subclass that inherits from it. In Listing 1.3, we create a class Student that extends the Person class.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Student class inherits from Person class<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Student<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span> Person <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> Level<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> String RegNo<span style=\"color: #333333;\">;<\/span>\n\t\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Student<\/span><span style=\"color: #333333;\">(<\/span>String RegNo<span style=\"color: #333333;\">)<\/span>\n\t<span style=\"color: #333333;\">{<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">RegNo<\/span> <span style=\"color: #333333;\">=<\/span> RegNo<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n\t\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> String <span style=\"color: #0066bb; font-weight: bold;\">getRegNO<\/span><span style=\"color: #333333;\">()<\/span>\n\t<span style=\"color: #333333;\">{<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> RegNo<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n\t\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getLevel<\/span><span style=\"color: #333333;\">()<\/span>\n\t<span style=\"color: #333333;\">{<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> Level<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\t\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<\/div>\n<p>Listing 1.3: Student Class inherits from the Person class<\/p>\n<p>&nbsp;<\/p>\n<h5 id=\"t5\"><strong>5. Instantiating the Inherited Class<\/strong><\/h5>\n<p>Now we can instantiate the student class.<\/p>\n<p>From the main method we are going a new Student object of type Student.<\/p>\n<p>We would also create a new Student object of type Person<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">PersonTester<\/span> <span style=\"color: #333333;\">{<\/span>\n\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\t<span style=\"color: #888888;\">\/\/Creating and instance of the person class<\/span>\n\t\tStudent student <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Student<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"KTH3948\"<\/span><span style=\"color: #333333;\">);<\/span>\n\t\tPerson person <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Student <span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"OSI9893\"<\/span><span style=\"color: #333333;\">);<\/span>\n\t\t\n\t\tString name <span style=\"color: #333333;\">=<\/span> person<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">Fullname<\/span><span style=\"color: #333333;\">();<\/span> <span style=\"color: #888888;\">\/\/Sub class calls the parent class method<\/span>\n\t\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> age <span style=\"color: #333333;\">=<\/span> person<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">GetAge<\/span><span style=\"color: #333333;\">();<\/span> <span style=\"color: #888888;\">\/\/Sub class calls the parent class method<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<\/div>\n<p>Listing 1.4: Inheriting an abstract class<\/p>\n<p>Try to run the above code and view the output<\/p>\n<p>&nbsp;<\/p>\n<h5 id=\"t6\"><strong>6. About Abstract Method<\/strong><\/h5>\n<p><span style=\"font-size: 1rem;\">A method can also be declared as abstract by using the abstract keyword.\u00a0 Take note of the following:<\/span><\/p>\n<ul>\n<li>If a method is abstract, then the class containing the method must also be abstract. An attempt to declare an abstract method is a class that is not abstract would produce and error (&#8220;abstract method can only be declared in an abstract class).<\/li>\n<li>An abstract method contains only the method declaration, no body is defined<\/li>\n<li>Abstract method declaration ends with a semicolon(;)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>We would cover the following: What is Abstraction? Abstract Classes and Methods in Java Example of Abstract Class in Java Inheriting an Abstract Class Instantiating &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[85],"tags":[],"class_list":["post-1879","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1879","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1879"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1879\/revisions"}],"predecessor-version":[{"id":2047,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1879\/revisions\/2047"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}