{"id":34315,"date":"2021-05-21T11:41:56","date_gmt":"2021-05-21T06:11:56","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/"},"modified":"2024-09-03T14:24:03","modified_gmt":"2024-09-03T08:54:03","slug":"abstract-class-and-encapsulation-in-java","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/","title":{"rendered":"Abstract Class and Encapsulation in JAVA"},"content":{"rendered":"\n<p>Java is an object-oriented language. It enables us to organise our program into simpler logical units known as objects and offers abstraction, encapsulation, inheritance and polymorphism.<\/p>\n\n\n\n<p>OOP is a methodology by which one can design a program by implementing classes and their objects.<\/p>\n\n\n\n<p><strong>What is Abstract Class ?&nbsp;<\/strong><\/p>\n\n\n\n<p>An abstract class is a class that deals with the abstraction of our program. So, the question arises <strong>what abstraction is<\/strong>?<\/p>\n\n\n\n<p>In general terms, <strong>Abstraction<\/strong> is that the feature of object-oriented programming that \"<em>shows<\/em>\" only essential information and \"<em>hides\"<\/em> unnecessary information.&nbsp;The foremost<strong>&nbsp;<\/strong>purpose of abstraction is to hide the unnecessary details from the users. Abstraction is selecting data that is beneficial and relevant for the user from&nbsp;a much bigger<strong>&nbsp;<\/strong>pool of information.<strong>&nbsp;<\/strong><\/p>\n\n\n\n<p>In object-oriented programming, through abstraction, the programmer tries to ensure that only the functionality is provided to the user. All its implementation and other extraneous aspects are kept hidden to reduce complexity and increase the program\u2019s efficiency.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A class that is declared by using an <strong>abstract <\/strong>keyword is known as the Abstract class.\u00a0<\/li>\n\n\n\n<li>An abstract class cannot be instantiated, i.e., one cannot create an object (instance) from the class.\u00a0<\/li>\n\n\n\n<li>An abstract class is permitted to have both abstract and non-abstract methods.<\/li>\n\n\n\n<li>A class is needed to be declared as an abstract class if it contains abstract methods.<\/li>\n\n\n\n<li>In order to use an abstract class, one can <strong><em>extend<\/em><\/strong> its child class and provide implementation to all of the abstract methods in its parent class.<\/li>\n<\/ul>\n\n\n\n<p><strong>Declaring an Abstract Class in Java<\/strong><\/p>\n\n\n\n<p>In Java, we declare that a class is abstract just by adding the&nbsp;abstract&nbsp;keyword preceding the class declaration.&nbsp;<\/p>\n\n\n\n<p>Here is a Java abstract class example:<\/p>\n\n\n\n<p>public abstract class Person {<\/p>\n\n\n\n<p>&nbsp;&nbsp;}<\/p>\n\n\n\n<p>This is how an abstract class is meant to be declared in Java.&nbsp;<\/p>\n\n\n\n<p>Now, when we try to create the instance of the Person class and try to compile it, the Java compiler will generate an error saying that an Abstract class cannot be instantiated.<\/p>\n\n\n\n<p>Person personInstance = new Person();&nbsp; \/\/not valid<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-java-programming\" class=\"courses-cta-title-link\">Java Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Learn Java the right way! Our course teaches you essential programming skills, from coding basics to complex projects, setting you up for success in the tech industry.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>16.05 Hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>3 Projects<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-java-programming\" class=\"courses-cta-button\">\n                Learn Java Programming\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"abstract-methods\"><strong>Abstract Methods<\/strong><\/h2>\n\n\n\n<p>Abstract methods are meant to be used by abstract classes only. Abstract methods are methods without the body. An abstract class may have both abstract methods and regular methods.<\/p>\n\n\n\n<p>While declaring a method abstract, we add the&nbsp;<em>abstract<\/em>&nbsp;keyword in front of the method declaration and the method is ended with the semicolon <strong>(;)<\/strong>. <\/p>\n\n\n\n<p>Here is a Java abstract method example:<\/p>\n\n\n\n<p>public abstract class Person {<\/p>\n\n\n\n<p>public abstract void myJob();<\/p>\n\n\n\n<p>&nbsp;&nbsp;}<\/p>\n\n\n\n<p>An abstract method has no body or implementation. Only the signatures of the method which are going to be implemented by the subclasses are present.<\/p>\n\n\n\n<p>If a class contains an abstract method, the whole class must be declared as the abstract class. Not all methods in an abstract class are necessary to be abstract methods. <strong><em>An abstract class can have a mixture of both abstract and non-abstract methods.<\/em><\/strong><\/p>\n\n\n\n<p>Subclasses of an abstract class are bound to implement <strong>(override)<\/strong> all abstract methods of its corresponding abstract superclass. The non-abstract methods of the superclass are just inherited as they are with the help of the <strong><em>super<\/em><\/strong> keyword. They can also be overridden if required.<\/p>\n\n\n\n<p>Here is an example subclass \u201cTeacher\u201d of the abstract class&nbsp;\u201cPerson\u201d:<\/p>\n\n\n\n<p>public class Teacher extends Person {<\/p>\n\n\n\n<p>public abstract void myJob(){<\/p>\n\n\n\n<p>System.out.println(\u201cMy job is Teaching.\u201d);<\/p>\n\n\n\n<p>&nbsp;&nbsp;}}<\/p>\n\n\n\n<p>Notice how&nbsp;the subclass \u201cTeacher\u201d&nbsp;has to implement the abstract method&nbsp;myJob()&nbsp;from its abstract superclass&nbsp;\u201cPerson\u201d.<\/p>\n\n\n\n<p>When a subclass of an abstract class is not required to implement all abstract methods of its superclass, the only time the subclass is also an abstract class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"purpose-of-abstract-classes\"><strong>Purpose of Abstract Classes<\/strong><\/h2>\n\n\n\n<p>The main purpose of abstract classes is to function as the base classes, which are to be extended by their subclasses in order to create their full implementation.<\/p>\n\n\n\n<p>For instance, we have a superclass person with a method myJob(), and the subclasses are like Teacher, Painter, Singer etc. Since every person\u2019s job corresponds to different professions isn\u2019t the same, there is no point in implementing this method in the parent class. This is because every subclass\/child-class must override this method to give its implementation details like Teacher class will do \u201cTeaching\u201d in this method, and Painter class will do \u201cPainting\u201d, etc.<\/p>\n\n\n\n<p>So, when we are aware that all the Person child classes will and requisite to override this myJob() method, there is no point in implementing this method in the parent class. Thus, making this method abstract would be a decent choice. By making this method abstract, we have made it compulsory for all subclasses to implement this method; otherwise, we will encounter the compilation error. Whenever the method is abstract, we don\u2019t need to implement any method in the parent class.<\/p>\n\n\n\n<p>Since the Person class has an abstract method, you just need to declare this class abstract.<\/p>\n\n\n\n<p>Each person must have a job; hence by making this method abstract, we made it compulsory for the child class to give implementation details to this method. From this way we have ensured that every Person has a Job.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"abstract-class-example\"><strong>Abstract class Example<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/abstract parent class\npublic abstract class Person{ \n\/\/abstract method\npublic abstract void myJob();\n}\n\/\/Teacher class extending Person class\npublic class Teacher extends Person {\npublic abstract void myJob(){\nSystem.out.println(\u201cMy job is Teaching.\u201d);\n  }}\n\/\/Painter class extending Person class\npublic class Painter extends Person {\npublic abstract void myJob(){\nSystem.out.println(\u201cMy job is Painting.\u201d);        }}\nPublic static void main(String args&#091;]){\nPerson obj;\nobj = new Teacher();\nSystem.out.println(\u201cTEACHER-\u201d + obj.myJob());\nobj = new Painter();\nSystem.out.println(\u201cPAINTER-\u201d + obj.myJob());\n}\n<\/code><\/pre>\n\n\n\n<p><strong>OUTPUT:<\/strong><\/p>\n\n\n\n<p>TEACHER-My job is Teaching.<\/p>\n\n\n\n<p>PAINTER-My job is Painting.<\/p>\n\n\n\n<p>Hence, for such kinds of real-world scenarios, we generally declare the class as abstract, and later,&nbsp;concrete classes&nbsp;extend these classes and override the methods accordingly. They can have their methods as well.<\/p>\n\n\n\n<p><strong>What is Encapsulation?<\/strong><\/p>\n\n\n\n<p>Encapsulation is defined as the wrapping or bundling up of data and methods of a class into a single unit. The fundamental concept of encapsulation is to hide the internal representation of an object from the outside. This is also known as data hiding. In general, encapsulation restricts the outer classes to access and modify the fields and methods of a class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"data-hiding-in-java\"><strong>Data Hiding in Java<\/strong><\/h2>\n\n\n\n<p><strong>Data Hiding in Java <\/strong>is defined as the mechanism to hide the variables of a class from other classes. Access to these variables is only granted through the methods of the corresponding class. Apart from hiding the implementation details from the users, it also offers better management and grouping of related data.<\/p>\n\n\n\n<p>In order to achieve a lesser degree of encapsulation in Java, we can use the access modifiers like \"protected\" or \"public\".&nbsp;<\/p>\n\n\n\n<p>It allows us to modify a part of the code without affecting the other attributes.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"how-to-encapsulate-the-data\"><strong>How to Encapsulate the data?<\/strong><\/h5>\n\n\n\n<p>If we use the most restrictive access modifier, i.e. private, we can only access it within the same class with our attribute or method.<\/p>\n\n\n\n<p>Any other subclasses or classes within the same package will not be able to access the \u201cprivate\u201d variables or methods.<\/p>\n\n\n\n<p>And if we want to get information about the current state of the object, we need to declare all the getter and setter methods as public.<\/p>\n\n\n\n<p><strong>Steps to achieve encapsulation in Java are \u2212<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Firstly, declare the variables of a class as private so that no other class or object can access them.<\/li>\n\n\n\n<li>Secondly, we need to provide public setter and getter methods to modify(write-only) and view(read-only) the values of the private variables.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Learn Java Programming For Beginners | Core Java Tutorial | Java Basics | Great Learning\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/ZYwHJ1LiKZY?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"encapsulation-demo\"><strong>Encapsulation Demo:<\/strong><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"the-program-to-access-variables-of-the-class-employee-is-shown-below\">The program to access variables of the class Employee is shown below:&nbsp;&nbsp;<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Encapsulation Demo\nclass Employee {\n    \/\/ Declare all the variables as private\n    \/\/ these can only be accessed by\n    \/\/ public methods of Employee class\n    private String empName;\n    private int empID;\n    private int empSalary;\n \n    \/\/ Setting up getters \n    \/\/ get method for salary to access\n    \/\/ private variable empSalary\n    public int getSalary()     {\n             return empSalary;   }\n \n    \/\/ get method for name to access\n    \/\/ private variable empName\n    public String getName() {\n           return empName;   }\n \n    \/\/ get method for ID to access\n    \/\/ private variable empID\n    public int getID() { \n             return empID; }\n \n    \/\/ set method for employee salary to access\n    \/\/ private variable empSalary\n    public void setSalary(int newSalary) { \n             empSalary = newSalary; }\n \n    \/\/ set method for employee name to access\n    \/\/ private variable empName\n    public void setName(String newName)\n    {\n        empName = newName;\n    }\n \n    \/\/ set method for employee ID to access\n    \/\/ private variable empID\n    public void setID(int newID) { empID = newID; }\n}\n \npublic class TestEmployee {\n    public static void main(String&#091;] args)\n    {\n        Employee obj = new Employee();\n \n        \/\/ setting values of the variables\n        obj.setName(\"Sunny\");\n        obj.setSalary(10000);\n        obj.setID(20);\n \n        \/\/ Displaying values of the variables\n        System.out.println(\"Employee's name: \" + obj.getName());\n        System.out.println(\"Employee's salary: \" + obj.getSalary());\n        System.out.println(\"Employee's ID: \" + obj.getID());\n }}\n        \/\/ Direct access of empID is not possible\n        \/\/ due to encapsulation\n        \/\/ System.out.println(\"Employee's ID: \" + obj.empID);\n<\/code><\/pre>\n\n\n\n<p><strong>OUTPUT:<\/strong><\/p>\n\n\n\n<p>Employee's name: Sunny<\/p>\n\n\n\n<p>Employee's salary: 10000<\/p>\n\n\n\n<p>Employee's ID: 20<\/p>\n\n\n\n<p>Also Read: <a href=\"https:\/\/www.mygreatlearning.com\/blog\/java-interview-questions\/\" target=\"_blank\" rel=\"noreferrer noopener\">Top 160+ Java Interview Questions and Answers in 2021<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java is an object-oriented language. It enables us to organise our program into simpler logical units known as objects and offers abstraction, encapsulation, inheritance and polymorphism. OOP is a methodology by which one can design a program by implementing classes and their objects. What is Abstract Class ?&nbsp; An abstract class is a class that [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":34316,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36826],"content_type":[],"class_list":["post-34315","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-java"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Abstract Class and Encapsulation in JAVA<\/title>\n<meta name=\"description\" content=\"Java: An abstract class is a class that deals with the abstraction of our program and Encapsulation is defined as the wrapping or bundling up of data.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Abstract Class and Encapsulation in JAVA\" \/>\n<meta property=\"og:description\" content=\"Java: An abstract class is a class that deals with the abstraction of our program and Encapsulation is defined as the wrapping or bundling up of data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-21T06:11:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-03T08:54:03+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1192\" \/>\n\t<meta property=\"og:image:height\" content=\"880\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/abstract-class-and-encapsulation-in-java\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/abstract-class-and-encapsulation-in-java\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Abstract Class and Encapsulation in JAVA\",\"datePublished\":\"2021-05-21T06:11:56+00:00\",\"dateModified\":\"2024-09-03T08:54:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/abstract-class-and-encapsulation-in-java\\\/\"},\"wordCount\":1240,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/abstract-class-and-encapsulation-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/05\\\/iStock-1148960878.jpg\",\"keywords\":[\"java\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/abstract-class-and-encapsulation-in-java\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/abstract-class-and-encapsulation-in-java\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/abstract-class-and-encapsulation-in-java\\\/\",\"name\":\"Abstract Class and Encapsulation in JAVA\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/abstract-class-and-encapsulation-in-java\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/abstract-class-and-encapsulation-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/05\\\/iStock-1148960878.jpg\",\"datePublished\":\"2021-05-21T06:11:56+00:00\",\"dateModified\":\"2024-09-03T08:54:03+00:00\",\"description\":\"Java: An abstract class is a class that deals with the abstraction of our program and Encapsulation is defined as the wrapping or bundling up of data.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/abstract-class-and-encapsulation-in-java\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/abstract-class-and-encapsulation-in-java\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/abstract-class-and-encapsulation-in-java\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/05\\\/iStock-1148960878.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/05\\\/iStock-1148960878.jpg\",\"width\":1192,\"height\":880,\"caption\":\"Programming code abstract technology background of software developer and Computer script\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/abstract-class-and-encapsulation-in-java\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Abstract Class and Encapsulation in JAVA\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Abstract Class and Encapsulation in JAVA","description":"Java: An abstract class is a class that deals with the abstraction of our program and Encapsulation is defined as the wrapping or bundling up of data.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Abstract Class and Encapsulation in JAVA","og_description":"Java: An abstract class is a class that deals with the abstraction of our program and Encapsulation is defined as the wrapping or bundling up of data.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2021-05-21T06:11:56+00:00","article_modified_time":"2024-09-03T08:54:03+00:00","og_image":[{"width":1192,"height":880,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Abstract Class and Encapsulation in JAVA","datePublished":"2021-05-21T06:11:56+00:00","dateModified":"2024-09-03T08:54:03+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/"},"wordCount":1240,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878.jpg","keywords":["java"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/","url":"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/","name":"Abstract Class and Encapsulation in JAVA","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878.jpg","datePublished":"2021-05-21T06:11:56+00:00","dateModified":"2024-09-03T08:54:03+00:00","description":"Java: An abstract class is a class that deals with the abstraction of our program and Encapsulation is defined as the wrapping or bundling up of data.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878.jpg","width":1192,"height":880,"caption":"Programming code abstract technology background of software developer and Computer script"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Abstract Class and Encapsulation in JAVA"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878.jpg",1192,880,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878-300x221.jpg",300,221,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878-768x567.jpg",768,567,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878-1024x756.jpg",1024,756,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878.jpg",1192,880,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878.jpg",1192,880,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878-640x853.jpg",640,853,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878-96x96.jpg",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-1148960878-150x111.jpg",150,111,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"Java is an object-oriented language. It enables us to organise our program into simpler logical units known as objects and offers abstraction, encapsulation, inheritance and polymorphism. OOP is a methodology by which one can design a program by implementing classes and their objects. What is Abstract Class ?&nbsp; An abstract class is a class that&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/34315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=34315"}],"version-history":[{"count":7,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/34315\/revisions"}],"predecessor-version":[{"id":112426,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/34315\/revisions\/112426"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/34316"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=34315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=34315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=34315"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=34315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}