{"id":244509,"date":"2021-06-05T09:33:12","date_gmt":"2021-06-05T09:33:12","guid":{"rendered":"https:\/\/imarticus.org\/?p=244509"},"modified":"2024-04-02T07:43:51","modified_gmt":"2024-04-02T07:43:51","slug":"object-oriented-features-of-java","status":"publish","type":"post","link":"https:\/\/imarticus.org\/blog\/object-oriented-features-of-java\/","title":{"rendered":"Complete Guide: Object Oriented Features of Java!"},"content":{"rendered":"<h2>What is OOPs?<\/h2>\n<p>OOPs, the concept brings this <strong>data\u00a0<\/strong>and\u00a0<strong>behavior\u00a0<\/strong>in a single place called\u00a0<strong class=\"pink-class\">\u201cclass\u201d<\/strong>\u00a0and we can\u00a0<strong>create any number of objects<\/strong> to represent the different states for each object.<\/p>\n<p><strong><a href=\"https:\/\/imarticus.org\/postgraduate-program-in-data-science-analytics\/\">Object-oriented programming training<\/a><\/strong> (OOPs) is a programming paradigm based on the concept of \u201cobjects\u201d that contain data and methods. The primary purpose of object-oriented programming is to increase the flexibility and maintainability of programs.<\/p>\n<p>Object-oriented programming brings together data and its behavior (methods) in a single location(object) makes it easier to understand how a program works. We will cover each and every feature of OOPs in detail so that you won\u2019t face any difficultly understanding <strong>OOPs Concepts.<\/strong><\/p>\n<figure id=\"attachment_3898\" class=\"wp-caption alignleft\"><figcaption class=\"wp-caption-text\"><strong>Object-Oriented Features in Java<\/strong><\/figcaption><\/figure>\n<h2><\/h2>\n<ol>\n<li>Classes<\/li>\n<li>Objects<\/li>\n<li>Data Abstraction<\/li>\n<li>Encapsulation<\/li>\n<li>Inheritance<\/li>\n<li>Polymorphism<\/li>\n<\/ol>\n<h2>What is Class?<\/h2>\n<p>The class represents a real-world entity that acts as a blueprint for all the objects.<\/p>\n<p>We can create as many objects as we need using Class.<\/p>\n<p>Example:<br \/>\nWe create a class for \u201c Student \u201d entity as below<\/p>\n<p>Student.java<\/p>\n<p>Class Student{<br \/>\nString id;<br \/>\nint age;<br \/>\nString course;<br \/>\nvoid enroll(){<br \/>\nSystem.out.println(\u201cStudent enrolled\u201d);<br \/>\n}<br \/>\n}<\/p>\n<p>The above definition of the class contains 3 fields id, age, and course, and also it contains behavior or a method called \u201c enroll \u201d.<\/p>\n<h2>What is an Object?<\/h2>\n<p>Object-Oriented Programming System(OOPS) is designed based on the concept of <strong class=\"blue-class\">\u201cObject\u201d.\u00a0<\/strong>It\u00a0contains both\u00a0<strong class=\"pink-class\">variables<\/strong>\u00a0(used for holding the data) and\u00a0<strong class=\"light-green-class\">methods<\/strong>(used for defining the behaviors).<\/p>\n<p>We can\u00a0<strong>create any number of objects<\/strong>\u00a0using this class and all those objects will get the same fields and behavior.<\/p>\n<div class=\"bwp-syntax-block clearfix\">\n<div class=\"bwp-syntax-toolbar\">\n<div class=\"bwp-syntax-control\">Student s1\u00a0<span class=\"sy0\">=<\/span>\u00a0<span class=\"kw1\">new<\/span>\u00a0Student<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/div>\n<\/div>\n<\/div>\n<p>Now we have created 3 objects\u00a0<strong>s1,s2,<\/strong>\u00a0and\u00a0<strong>s3\u00a0<\/strong>for the same class\u00a0<strong class=\"blue-class\">\u201c Student \u201d<\/strong>.We can create as many objects as required in the same way.<\/p>\n<p>We can set the value for each field of an object as below,<\/p>\n<div class=\"bwp-syntax-block clearfix\">\n<div class=\"bwp-syntax-toolbar\">\n<div class=\"bwp-syntax-control\">s1.<span class=\"me1\">id<\/span><span class=\"sy0\">=<\/span><span class=\"nu0\">123<\/span><span class=\"sy0\">;<\/span><\/div>\n<div class=\"bwp-syntax-control\">s2.<span class=\"me1\">age<\/span><span class=\"sy0\">=<\/span><span class=\"nu0\">18<\/span><span class=\"sy0\">;<\/span><\/div>\n<div class=\"bwp-syntax-control\">s3.<span class=\"me1\">course<\/span><span class=\"sy0\">=<\/span>\u201dcomputers\u201d<span class=\"sy0\">;<\/span><\/div>\n<\/div>\n<\/div>\n<div class=\"bwp-syntax-control\"><\/div>\n<h2 class=\"bwp-syntax-control\">What is Abstraction?<\/h2>\n<p>Abstraction is a process where you show only \u201crelevant\u201d data and \u201chide\u201d unnecessary details of an object from the user.<\/p>\n<p>For example, when you log in to your bank account online, you enter your user_id and password and press login, what happens when you press login, how the input data sent to the server, how it gets verified is all abstracted away from you.<\/p>\n<p>We can achieve \u201c\u00a0<strong class=\"pink-class\">abstraction\u00a0<\/strong>\u201d in Java using\u00a0<strong class=\"blue-class\">2 ways<\/strong><\/p>\n<p><strong>1. Abstract class<\/strong><\/p>\n<p><strong>2. Interface<\/strong><\/p>\n<p>1. Abstract Class<\/p>\n<ul>\n<li>Abstract class in Java can be created using the \u201c abstract \u201d keyword.<\/li>\n<li>If we make any class abstract then it can\u2019t be instantiated which means we are not able to create the object of an abstract class.<\/li>\n<li>Inside Abstract class, we can declare abstract methods as well as concrete methods.<\/li>\n<li>So using abstract class, we can achieve 0 to 100 % abstraction.<\/li>\n<\/ul>\n<p>Example:<br \/>\nAbstract class Phone{<br \/>\nvoid receive all();<br \/>\nAbstract void sendMessage();<br \/>\n}<br \/>\nAnyone who needs to access this functionality has to call the method using the Phone object pointing to its subclass.<\/p>\n<p>2. Interface<\/p>\n<ul>\n<li>The interface is used to achieve pure or complete abstraction.<\/li>\n<li>We will have all the methods declared inside Interface as abstract only.<\/li>\n<li>So, we call interface 100% abstraction.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><br \/>\nWe can define interface for Car functionality abstraction as below<br \/>\nInterface Car{<br \/>\npublic void changeGear( int gearNumber);<br \/>\npublic void applyBrakes();<br \/>\n}<\/p>\n<p>Now, these functionalities like changing gear and applying brake are abstracted using this interface.<\/p>\n<h2>What is Encapsulation?<\/h2>\n<ul>\n<li>Encapsulation is the process of binding object state(fields) and behaviors(methods) together in a single entity called \u201cClass\u201d.<\/li>\n<li>Since it wraps both fields and methods in a class, it will be secured from outside access.<\/li>\n<li>We can restrict access to the members of a class using access modifiers such as private, protected, and public keywords.<\/li>\n<li>When we create a class in Java, it means we are doing encapsulation.<\/li>\n<li>Encapsulation helps us to achieve the re-usability of code without compromising security.<\/li>\n<\/ul>\n<p>Example:<br \/>\nclass EmployeeCount<br \/>\n{<br \/>\nprivate int numOfEmployees = 0;<br \/>\npublic void setNoOfEmployees (int count)<br \/>\n{<br \/>\nnumOfEmployees = count;<br \/>\n}<br \/>\npublic double getNoOfEmployees ()<br \/>\n{<br \/>\nreturn numOfEmployees;<br \/>\n}<br \/>\n}<br \/>\npublic class EncapsulationExample<br \/>\n{<br \/>\npublic static void main(String args[])<br \/>\n{<br \/>\nEmployeeCount obj = new EmployeeCount ();<br \/>\nobj.setNoOfEmployees(5613);<br \/>\nSystem.out.println(\u201cNo Of Employees: \u201c+(int)obj.getNoOfEmployees());<br \/>\n}<br \/>\n}<\/p>\n<p><strong>\u00a0What is the benefit of encapsulation in java programming<\/strong><br \/>\nWell, at some point in time, if you want to change the implementation details of the class EmployeeCount, you can freely do so without affecting the classes that are using it. For more information learn,<\/p>\n<div class=\"su-button-center\">Start Learning Java Programming<\/div>\n<h2>What is Inheritance?<\/h2>\n<ul>\n<li>One class inherits or acquires the properties of another class.<\/li>\n<li>Inheritance provides the idea of reusability of code and each sub-class defines only those features that are unique to it <span style=\"color: #3d4459;\"><a style=\"color: #3d4459;\" href=\"https:\/\/diplomarbeit-schreiben-lassen.com\/\">ghostwriter diplomarbeit<\/a><\/span>, the rest of the features can be inherited from the parent class.<\/li>\n<\/ul>\n<ol>\n<li>Inheritance is the process of defining a new class based on an existing class by extending its common data members and methods.<\/li>\n<li>It allows us to reuse code <span style=\"color: #3d4459;\"><a style=\"color: #3d4459;\" href=\"https:\/\/bachelorarbeit-ghostwriter.com\">ghostwriter bachelorarbeit<\/a><\/span>, it improves reusability in your java application.<\/li>\n<li>The parent class is called the\u00a0<strong>base class<\/strong>\u00a0or\u00a0<strong>superclass<\/strong>. The child class that extends the base class is called the derived class or\u00a0<strong>subclass<\/strong>\u00a0or\u00a0<strong>child class<\/strong>.<\/li>\n<\/ol>\n<p>To inherit a class we use extends keyword. Here class A is child class and class B is parent class.<\/p>\n<p>class A extends B<br \/>\n{<br \/>\n}<\/p>\n<p><strong>Types Of Inheritance:<\/strong><br \/>\n<strong>Single Inheritance<\/strong>: refers to a child and parent class relationship where a class extends another class.<\/p>\n<p><strong>Multilevel inheritance<\/strong>:\u00a0 a child and parent class relationship where a class extends the child class <span style=\"color: #3d4459;\"><a style=\"color: #3d4459;\" href=\"https:\/\/ghost-factory.ch\/\">Ghostwriter<\/a><\/span>. For example, class A extends class B and class B extends class C.<\/p>\n<p><strong>Hierarchical inheritance<\/strong>:\u00a0 where more than one class extends the same class. For example, class B extends class A and class C extends class A.<\/p>\n<h2>What is Polymorphism?<\/h2>\n<ul>\n<li>It is the concept where an object behaves differently in different situations.<\/li>\n<li>Since the object takes multiple forms <span style=\"color: #3d4459;\"><a style=\"color: #3d4459;\" href=\"https:\/\/ghost-factory.de\/\">ghostwriter agentur<\/a><\/span>, it is called Polymorphism.<\/li>\n<li>In java, we can achieve it using method overloading and method overriding.<\/li>\n<li>There are 2 types of Polymorphism available in Java,<\/li>\n<\/ul>\n<p><strong>Method overloading<\/strong><\/p>\n<p>In this case, which method to call will be decided at the compile time itself based on the number or type of the parameters <span style=\"color: #3d4459;\"><a style=\"color: #3d4459;\" href=\"https:\/\/ghostwriter-deutschland.com\/\">ghostwriter deutschland<\/a><\/span>. Static\/Compile Time polymorphism is an example of method overloading.<\/p>\n<p><strong>Method overriding<\/strong><\/p>\n<p>In this case, which method to call will be decided at the run time based on what object is actually pointed to by the reference variable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is OOPs? OOPs, the concept brings this data\u00a0and\u00a0behavior\u00a0in a single place called\u00a0\u201cclass\u201d\u00a0and we can\u00a0create any number of objects to represent the different states for each object. Object-oriented programming training (OOPs) is a programming paradigm based on the concept of \u201cobjects\u201d that contain data and methods. The primary purpose of object-oriented programming is to increase [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":244510,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_mo_disable_npp":"","_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[23],"tags":[866,1854,1905,2295,2311,2312],"class_list":["post-244509","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-analytics","tag-data-science-career","tag-data-science-online-training","tag-java-programming-course","tag-data-science-course-with-placement-in-india","tag-object-oriented-programming-language","tag-c-courses"],"acf":[],"aioseo_notices":[],"modified_by":"Imarticus Learning","_links":{"self":[{"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/posts\/244509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/comments?post=244509"}],"version-history":[{"count":2,"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/posts\/244509\/revisions"}],"predecessor-version":[{"id":262497,"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/posts\/244509\/revisions\/262497"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/media\/244510"}],"wp:attachment":[{"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/media?parent=244509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/categories?post=244509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imarticus.org\/blog\/wp-json\/wp\/v2\/tags?post=244509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}