{"id":66369,"date":"2019-03-19T15:00:32","date_gmt":"2019-03-19T13:00:32","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=66369"},"modified":"2021-03-01T12:24:05","modified_gmt":"2021-03-01T10:24:05","slug":"java-oops-concepts-tutorial","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/","title":{"rendered":"OOPS Concepts Java Tutorial"},"content":{"rendered":"<p>In this post, we feature a comprehensive OOPS Concepts <a href=\"https:\/\/www.javacodegeeks.com\/java-tutorials\">Java Tutorial<\/a>. We will take a look at the <strong>OOPS concepts<\/strong> in Java and provide code examples for each one of them. <\/p>\n<h2 class=\"wp-block-heading\" id=\"h-1-introduction\"><a name=\"introduction\"><\/a>1. Introduction<\/h2>\n<p><strong>Object-Oriented Programming System<\/strong> in Java, also known as OOPS, is a programming paradigm where the main concept of a program is based on objects that communicate with each other. OOPS has become the most popular programming paradigm for large and complex programs. Java is one of the most widely used OOPS languages.<\/p>\n<p>To run the examples we will use the following technologies:<\/p>\n<ul class=\"wp-block-list\">\n<li>Java 8<\/li>\n<li>Eclipse 4.10.0<\/li>\n<\/ul>\n<p>Before we dive into the concepts, let\u2019s see first the definition of the Object in OOPS.<\/p>\n<div class=\"toc\">\n<h3>Table Of Contents<\/h3>\n<dl>\n<dt><a href=\"#introduction\">1. Introduction<\/a><\/dt>\n<dt><a href=\"#what_is_an_object\">2. What is an Object?<\/a><\/dt>\n<dt><a href=\"#oops_main_concepts\">3. OOPS Main Concepts<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#oops_encapsulation\">3.1 Encapsulation<\/a><\/dt>\n<dt><a href=\"#oops_inheritance\">3.2 Inheritance<\/a><\/dt>\n<dt><a href=\"#oops_polymorphism\">3.3 Polymorphism<\/a><\/dt>\n<dt><a href=\"#oops_abstraction\">3.4 Abstraction<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#oops_other_concepts\">4. OOPS Other Concepts<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#oops_association\">4.1 Association<\/a><\/dt>\n<dt><a href=\"#oops_aggregation\">4.2 Aggregation<\/a><\/dt>\n<dt><a href=\"#oops_composition\">4.3 Composition<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#oops_advantages\">5. Advantages of OOPS<\/a><\/dt>\n<dt><a href=\"#eclipse_source_actions\">6. Eclipse Source Actions<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#generate_constructor\">6.1 Generate Constructor<\/a><\/dt>\n<dt><a href=\"#generate_getters_setters\">6.2 Generate Getters and Setters<br \/>\n<\/a><\/dt>\n<dt><a href=\"#generate_toString\">6.3 Generate toString()<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#conclusion\">7. Conclusion<\/a><\/dt>\n<dt><a href=\"#download_eclipse_project\">8. Download the Eclipse project<\/a><\/dt>\n<\/dl>\n<\/div>\n<p>&nbsp;<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-2-what-is-an-object\"><a name=\"what_is_an_object\"><\/a>2. What is an Object?<\/h2>\n<p>In OOPS concepts in Java, an object is an entity that contains data and functions. Objects are created by <strong>classes<\/strong> and the association is that one class can create as many objects as we like. A class is defined as the blueprint from which different objects are created. Objects are also called instances of the class, so the process of creating an object is also called instantiation. In Java, the data and functions of an object are usually called fields and methods respectively. <\/p>\n<p>Let&#8217; see an example to better understand the notion of the object and class. If we want to design a program to store the personal data of all the employees of a company, then the term Employee is the class and the actual people (employees) are the objects. Below we find an example of such a class.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Employee.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">public class Employee {\n\n    String name;\n    int salary;\n    int bonus;\n    String department;\n    \n}\n<\/pre>\n<p>Above we created a class called <code>Employee<\/code> which contains 4 fields, the <em>name<\/em>, <em>salary<\/em>, <em>bonus<\/em> and <em>department<\/em>. So far we haven&#8217;t assigned values for those fields, as this will be done when we create the objects from the <code>Employee<\/code> class. Below we see how to do that.<\/p>\n<p><span style=\"text-decoration: underline\"><em>EmployeeExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">Employee emp = new Employee();\nemp.name = \"Keith Henderson\";\nemp.salary = 50000;\nemp.bonus = 3000;\nemp.department = \"Engineering\";\n<\/pre>\n<p>The code above creates one object called <strong>emp<\/strong> from the <code>Employee<\/code> class and assigns values to all of its fields. If we output this object we can see its fields:<\/p>\n<p><span style=\"text-decoration: underline\"><em>EmployeeExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">System.out.printf(\"%s belongs to the %s department earning an annual salary of $%d with bonus $%d\",\n                emp.name, emp.department, emp.salary, emp.bonus);\n<\/pre>\n<p>The above prints all the fields of the <strong>emp<\/strong> object in a nicely formatted output:<\/p>\n<p><span style=\"text-decoration: underline\"><em>Output<\/em><\/span><\/p>\n<pre class=\"brush:bash\">Keith Henderson belongs to the Engineering department earning an annual salary of $50000 with bonus $3000\n<\/pre>\n<p>We successfully printed all the fields of the <strong>emp<\/strong> object in the above output.<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-3-oops-in-java-main-concepts\"><a name=\"oops_main_concepts\"><\/a>3. OOPS in Java Main Concepts<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"503\" height=\"504\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_diagram.jpg\" alt=\"Java OOPS Concepts - OOPS Main Concepts\" class=\"wp-image-67463\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_diagram.jpg 503w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_diagram-150x150.jpg 150w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_diagram-300x300.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_diagram-70x70.jpg 70w\" sizes=\"(max-width: 503px) 100vw, 503px\" \/><figcaption>OOPS Main Concepts<\/figcaption><\/figure>\n<\/div>\n<p>OOPS in Java consists of four main concepts:<\/p>\n<ul class=\"wp-block-list\">\n<li>Encapsulation<\/li>\n<li>Inheritance<\/li>\n<li>Polymorphism<\/li>\n<li>Abstraction<\/li>\n<\/ul>\n<p>We will look into each OOPS Concept in the following sections and provide code examples.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-3-1-encapsulation\"><a name=\"oops_encapsulation\"><\/a>3.1 Encapsulation<\/h3>\n<p>Encapsulation is about restricting the access of the fields of an object to the outside world and making them available only through methods. In Java, we can restrict the access of the fields of an object by declaring them <strong>private<\/strong> and expose them through methods that we call the <strong>getters<\/strong> and <strong>setters<\/strong>. Let&#8217;s see below an updated version of the <code>Employee<\/code> class we created in the previous section which hides all of its fields.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.encapsulation.Employee<\/em><\/span><\/p>\n<pre class=\"brush:java\">public class Employee {\n\n    private String name;\n    private int salary;\n    private int bonus;\n    private String department;\n    \n    public String getName() {\n        return name;\n    }\n    \n    public void setName(String name) {\n        this.name = name;\n    }\n    \n    public int getSalary() {\n        return salary;\n    }\n    \n    public void setSalary(int salary) {\n        this.salary = salary;\n    }\n    \n    public int getBonus() {\n        return bonus;\n    }\n    \n    public void setBonus(int bonus) {\n        this.bonus = bonus;\n    }\n    \n    public String getDepartment() {\n        return department;\n    }\n    \n    public void setDepartment(String department) {\n        this.department = department;\n    }\n\n}\n<\/pre>\n<p>In the above <code>Employee<\/code> class, we declared all the fields <strong>private<\/strong> to restrict the direct access to the fields from any other object. Now each field can only be accessed through its <strong>set<\/strong> and <strong>get<\/strong> method. For example, the <code>name<\/code> field can&#8217;t be accessed anymore from outside the class and it can only be set by the <code>setName<\/code> method and retrieved by the <code>getName<\/code> method. Let&#8217;s create a new object below and see how to access its fields:<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.encapsulation.EmployeeExample<\/em><\/span><\/p>\n<pre class=\"brush:java\">Employee emp = new Employee();\nemp.setName(\"Keith Henderson\");\nemp.setSalary(50000);\nemp.setBonus(3000);\nemp.setDepartment(\"Engineering\");\n\nSystem.out.printf(\"%s belongs to the %s department earning an annual salary of $%d with bonus $%d\",\n        emp.getName(), emp.getDepartment(), emp.getSalary(), emp.getBonus());\n<\/pre>\n<p>In the code above, we create the exact same object we created in the previous example, but now its fields are set by the <strong>setter<\/strong> methods. Similarly, we print the exact same output of the object we did before, using its <strong>getter<\/strong> methods.<\/p>\n<p>The question that arises now is why we should use getters and setters and not access the fields of an object directly, as it is easier and reduces a lot of code. Below we will see why encapsulation is so important.<\/p>\n<h4 class=\"wp-block-heading\" id=\"h-the-importance-of-encapsulation\">The importance of Encapsulation<\/h4>\n<p>As we said before, encapsulation is about hiding the fields of an object and making them available only through setters and getters. There are use cases where we would want to restrict the access of a field to specific objects using the <strong>protected<\/strong> or <strong>package<\/strong> access modifiers in the setters. Alternatively, we might not want to ever change the field of an object after it is initialized in the <strong>constructor<\/strong> of the class.<\/p>\n<p>As an example, we will take our favourite <code>Employee<\/code> class and restrict the update of the <em>name<\/em> field.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.encapsulation.hidenamesetter.Employee<\/em><\/span><\/p>\n<pre class=\"brush:java\">public class EmployeeHideNameSetter {\n\n    private final String name;\n    private int salary;\n    private int bonus;\n    private String department;\n    \n    public EmployeeHideNameSetter(String name) {\n        this.name = name;\n    }\n    \n    public String getName() {\n        return name;\n    }\n    \n    public int getSalary() {\n        return salary;\n    }\n    \n    public void setSalary(int salary) {\n        this.salary = salary;\n    }\n    \n    public int getBonus() {\n        return bonus;\n    }\n    \n    public void setBonus(int bonus) {\n        this.bonus = bonus;\n    }\n    \n    public String getDepartment() {\n        return department;\n    }\n    \n    public void setDepartment(String department) {\n        this.department = department;\n    }\n\n}\n<\/pre>\n<p>As we see from the above class, the <code>setName<\/code> method has been removed. There is no way to change the <em>name<\/em> field after it is initialized in the constructor. We have declared the <em>name<\/em> field <strong>final<\/strong>, which means that it will never change as such we must provide a value during the instantiation of  object. That is a way to never forget to assign values to important fields. Find below the instantiation of an object for the above class.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.encapsulation.hidenamesetter.EmployeeExample<\/em><\/span><\/p>\n<pre class=\"brush:java\">Employee emp = new Employee(\"Keith Henderson\");\nemp.setSalary(50000);\nemp.setBonus(3000);\nemp.setDepartment(\"Engineering\");\n<\/pre>\n<p>In the code above, during the instantiation of the <strong>emp<\/strong> object we assigned a value to its <em>name<\/em> field and then we set the remaining fields using the setters. There isn&#8217;t a no-argument constructor anymore and during the creation of objects, we must pass a value for the <em>name<\/em> field.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-3-2-inheritance\"><a name=\"oops_inheritance\"><\/a>3.2 Inheritance<\/h3>\n<p>Inheritance is the ability of a class to inherit the characteristics of another class. In Java, that is achieved when the child class, also called the <strong>sublass<\/strong>, inherits the fields and methods of the parent class, also called the <strong>superclass<\/strong>, using the <strong>extends<\/strong> keyword. When a class inherits and implements a method of a superclass, we also say that it <strong>overrides<\/strong> the method. In order for the inheritance to work, the fields or methods of a superclass must not be declared <strong>private<\/strong>, as this will restrict the access from the outside world. Usually, they are declared  <strong>protected<\/strong>, a keyword that is used to access the field and method of a class only from:<\/p>\n<ul class=\"wp-block-list\">\n<li>Within the class itself<\/li>\n<li>Any class that resides in the same package of the superclass<\/li>\n<li>Any class that extends it regardless of the package it resides<\/li>\n<\/ul>\n<p>In Java, there is a class called <strong>Object<\/strong> which is the root of the class hierarchy. Every class that we create has Object as a superclass, even if we don\u2019t explicitly specify it with the <strong>extends<\/strong> keyword. Every object inherits and implements the methods of the Object class. The most common method from the Object class that every class should implement, is the <code>toString<\/code> method which returns a string representation of the object. Finally, Java supports <em>Single Inheritance<\/em> when it comes to class implementation, meaning that a class can extend one and only one class.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>In the example below, the <code>Director<\/code> class extends an updated version of the <code>Employee<\/code> class.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.inheritance.Employee<\/em><\/span><\/p>\n<pre class=\"brush:java\">public class Employee {\n\n    private final String name;\n    protected double salary;\n    protected double bonus;\n    protected String department;\n    private final String role;\n\n    public Employee(String name, String role) {\n        this.name = name;\n        this.role = role;\n    }\n\n    public String getName() {\n        return name;\n    }\n\n    public double getSalary() {\n        return salary;\n    }\n\n    public void setSalary(double salary) {\n        this.salary = salary;\n    }\n\n    public String getDepartment() {\n        return department;\n    }\n\n    public void setDepartment(String department) {\n        this.department = department;\n    }\n\n    public String getRole() {\n        return role;\n    }\n\n    public double getBonus() {\n        return bonus;\n    }\n\n    public void giveBonus() {\n        this.bonus = salary * 0.2;\n    }\n\n    @Override\n    public String toString() {\n        return getName() + \" is a \" + getRole() + \" at the \" + getDepartment()\n                + \" department earning an annual salary of $\" + salary + \" with bonus $\" + getBonus();\n    }\n\n}\n<\/pre>\n<p>We have declared all the fields of the above <code>Employee<\/code> class <strong>protected<\/strong>, apart from the <strong>name<\/strong> and <strong>role<\/strong> fields which are initialized in the constructor. We also added a <code>giveBonus<\/code> method, which sets the <em>bonus<\/em> field to a percentage of the salary, and a <code>toString<\/code> method which returns a nicely formatted representation of the object. Let&#8217;s see below the subclass <code>Director<\/code> which <strong>extends<\/strong> the <code>Employee<\/code> class.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.inheritance.Director<\/em><\/span><\/p>\n<pre class=\"brush:java\">public class Director extends Employee {\n\n    public Director(String name) {\n        super(name, \"Director\");\n    }\n\n    @Override\n    public void giveBonus() {\n        this.bonus = getSalary() * 0.5;\n    }\n\n}\n<\/pre>\n<p>As we see from the above code, the <code>Director<\/code> class is the <strong>subclass<\/strong> and it inherits the fields and methods of the <code>Employee<\/code> class which is the <strong>superclass<\/strong>. The <code>Director<\/code> class cannot inherit the <em>name<\/em> and <em>role<\/em> fields as these are declared <strong>private<\/strong>. The <em>role<\/em> field is initalized from within the class and cannot change. Also we override the <code>giveBonus<\/code> method which sets the bonus to a different value from its superclass method. The <code>toString<\/code> method is not overriden in the <code>Director<\/code> class, so when we invoke it the <code>toString<\/code> method of the <code>Employee<\/code> class will be called.<\/p>\n<p>Below, we create one <code>Employee<\/code> and one <code>Director<\/code> object.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.inheritance.DirectorExample<\/em><\/span><\/p>\n<pre class=\"brush:java\">Employee emp = new Employee(\"Keith Henderson\", \"Backend Developer\");\nemp.setSalary(50000);\nemp.setDepartment(\"Engineering\");\nemp.giveBonus();\n\nEmployee dir = new Director(\"Tori Hicks\");\ndir.setSalary(150000);\ndir.setDepartment(\"Engineering\");\ndir.giveBonus();\n\nSystem.out.println(emp);\nSystem.out.println(dir);\n<\/pre>\n<p>In the above example, we created 2 different object that are both of type <code>Employee<\/code>. Here we don&#8217;t print anything in the <code>System.out.println<\/code> method but we only pass the objects. When this method gets invoked it will call the <code>toString<\/code> method of the objects which contains the nicely formatted output. Let&#8217;s run the example and check the output.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Output<\/em><\/span><\/p>\n<pre class=\"brush:bash\">Keith Henderson is a Backend Developer at the Engineering department earning an annual salary of $50000 with bonus $10000.0\nTori Hicks is a Director at the Engineering department earning an annual salary of $150000 with bonus $75000.0\n<\/pre>\n<p>In the output above, we see that the <code>toString<\/code> method is invoked when printing the objects.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-3-3-polymorphism\"><a name=\"oops_polymorphism\"><\/a>3.3 Polymorphism<\/h3>\n<p>Polymorphism is the ability of a field or a method to have multiple forms. In Java, we achieve polymorphism by method <strong>overloading<\/strong> or <strong>overriding<\/strong>. We saw how to override a method in the previous example, where we managed to override the <code>giveBonus<\/code> and <code>toString<\/code> methods. Overloading is achieved by having the same method name with different arguments. The example below overloads the <code>giveBonus<\/code> method of the <code>Employee<\/code> class.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.polymorphism.Employee<\/em><\/span><\/p>\n<pre class=\"brush:java\">public void giveBonus() {\n    this.bonus = salary * 0.2;\n}\n\npublic void giveBonus(double multiplier) {\n    this.bonus = salary * multiplier;\n}\n<\/pre>\n<p>In the code above, we see 2 methods with the name <code>giveBonus<\/code> but with different arguments. The <code>giveBonus(double multiplier)<\/code> method is used when we want to give a different bonus to the employee, than the basic one we give in the no-argument method.<\/p>\n<p>Below we create 2 <code>Employee<\/code>instances, like we did before, but now we give a different bonus to the director. <\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.polymorphism.DirectorExample<\/em><\/span><\/p>\n<pre class=\"brush:java\">Employee emp = new Employee(\"Keith Henderson\", \"Backend Developer\");\nemp.setSalary(50000);\nemp.setDepartment(\"Engineering\");\nemp.giveBonus();\n\nEmployee dir = new Director(\"Tori Hicks\");\ndir.setSalary(150000);\ndir.setDepartment(\"Engineering\");\ndir.giveBonus(0.8);\n\nSystem.out.println(emp);\nSystem.out.println(dir);\n<\/pre>\n<p>In the code above, the <strong>emp<\/strong> object gets a basic bonus from the <code>giveBonus<\/code> method, whereas the <strong>dir<\/strong> object gets a better bonus using the <code>giveBonus(double multiplier)<\/code> method. As you have already guessed the output would look like:<\/p>\n<p><span style=\"text-decoration: underline\"><em>Output<\/em><\/span><\/p>\n<pre class=\"brush:bash\">Keith Henderson is a Backend Developer at the Engineering department earning an annual salary of $50000.0 with bonus $10000.0\nTori Hicks is a Director at the Engineering department earning an annual salary of $150000.0 with bonus $120000.0\n<\/pre>\n<p>We do confirm from the output above, that the director got a better bonus than the other employee.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-3-4-abstraction\"><a name=\"oops_abstraction\"><\/a>3.4 Abstraction<\/h3>\n<p>Abstraction is about hiding the implementation details of a method to the outside world. In Java, abstraction can be achieved in two ways:<\/p>\n<ul class=\"wp-block-list\">\n<li>Interfaces<\/li>\n<li>Abstract Classes<\/li>\n<\/ul>\n<p>Let&#8217;s see some examples for both ways of abstraction.<\/p>\n<h4 class=\"wp-block-heading\" id=\"h-3-4-1-interface\">3.4.1 Interface<\/h4>\n<p>An <strong>interface<\/strong> contains abstract methods that have an empty body and no implementation at all. We can create an interface using the <code>interface<\/code> keyword and provide only the signature for its methods. An interface cannot be instantiated, as such, we need to create a class which implements the methods of an interface, using the <strong>implements<\/strong> keyword. Java supports <em>Multiple Inheritance<\/em> of interfaces, as opposed to the <em>Single Inheritance<\/em> of classes, as we saw in the previous section. That means that a class can implement multiple interfaces. Additionally, an interface can also <strong>extend<\/strong> multiple interfaces. Below we create an example of an interface.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.abstractinterface.Employee<\/em><\/span><\/p>\n<pre class=\"brush:java\">public interface EmployeeInterface {\n\n    String getName();\n    double getSalary();\n    double getBonus();\n    String getDepartment();\n    String getRole();\n    void giveBonus();\n\n}\n<\/pre>\n<p>The above interface has methods with no implementation. One thing to notice here is that all the methods of an interface are <strong>public<\/strong> even if we don&#8217;t explicitly add this access modifier. Let&#8217;s see below how a class can implement this interface.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.abstractinterface.Employee<\/em><\/span><\/p>\n<pre class=\"brush:java\">public class Employee implements EmployeeInterface {\n\n    private String name;\n    private double salary;\n    private double bonus;\n    private String department;\n    private String role;\n\n    public Employee(String name, double salary, String department, String role) {\n        this.name = name;\n        this.salary = salary;\n        this.department = department;\n        this.role = role;\n    }\n\n    @Override\n    public String getName() {\n        return name;\n    }\n\n    @Override\n    public double getSalary() {\n        return salary;\n    }\n\n    @Override\n    public double getBonus() {\n        return bonus;\n    }\n\n    @Override\n    public String getDepartment() {\n        return department;\n    }\n\n    @Override\n    public String getRole() {\n        return role;\n    }\n\n    @Override\n    public void giveBonus() {\n        this.bonus = salary * 0.2;\n    }\n    \n    @Override\n    public String toString() {\n        return getName() + \" is a \" + getRole() + \" at the \" + getDepartment()\n                + \" department earning an annual salary of $\" + salary + \" with bonus $\" + getBonus();\n    }\n\n}\n<\/pre>\n<p>The above <code>Employee<\/code> class implements all the methods of the <code>EmployeeInterface<\/code> by providing implementation details. For the sake of the example, we initialise the fields in the constructor and do not provide any setter. Now any <code>Employee<\/code> object is also a <code>EmployeeInterface<\/code> as well. This is confirmed in the below instantiation of a new object.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.abstractinterface.EmployeeExample<\/em><\/span><\/p>\n<pre class=\"brush:java\">EmployeeInterface emp = new Employee(\"Keith Henderson\", 50000, \"Engineering\", \"Backend Developer\");\nemp.giveBonus();\n<\/pre>\n<h4 class=\"wp-block-heading\" id=\"h-default-method-in-java8\">Default Method in Java8<\/h4>\n<p>Java 8 introduced the option to add a default implementation for a method of an interface, without having to provide implementation details for it, in any class that implements the interface. Below we see how this is achieved.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.abstractdefaultinterface.EmployeeInterface<\/em><\/span><\/p>\n<pre class=\"brush:java\">public interface EmployeeInterface {\n\n    String getName();\n\n    double getSalary();\n\n    default double getBonus() {\n        return getSalary() * 0.2;\n    }\n\n    String getDepartment();\n\n    default String getRole() {\n        return \"Employee\";\n    }\n\n}\n<\/pre>\n<p>In the above interface, we see that the <code>getBonus<\/code> and <code>getRole<\/code> methods have a default implementation. Let&#8217;s create a class that implements this interface and provide implementation only for one of those default method.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.abstractdefaultinterface.Employee<\/em><\/span><\/p>\n<pre class=\"brush:java\">public class Employee implements EmployeeInterface {\n\n    private String name;\n    private double salary;\n    private String department;\n    private String role;\n\n    public Employee(String name, double salary, String department, String role) {\n        this.name = name;\n        this.salary = salary;\n        this.department = department;\n        this.role = role;\n    }\n\n    @Override\n    public String getName() {\n        return name;\n    }\n\n    @Override\n    public double getSalary() {\n        return salary;\n    }\n\n    @Override\n    public String getDepartment() {\n        return department;\n    }\n\n    @Override\n    public String getRole() {\n        return role;\n    }\n    \n    @Override\n    public String toString() {\n        return getName() + \" is a \" + getRole() + \" at the \" + getDepartment()\n                + \" department earning an annual salary of $\" + salary + \" with bonus $\" + getBonus();\n    }\n\n}\n<\/pre>\n<p>In the above class, we do not provide any implementation for the <code>getBonus<\/code> method, as it is not required anymore, due to the default implementation that we provided in the interface. We do provide an implementation of the <code>getRole<\/code> method though, which means that the default implementation of this method in the interface will be ignored. Below we create an object and print its output.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.abstractdefaultinterface.EmployeeExample<\/em><\/span><\/p>\n<pre class=\"brush:java\">EmployeeInterface emp = new Employee(\"Keith Henderson\", 50000, \"Engineering\", \"Backend Developer\");\nSystem.out.println(emp);\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Output<\/em><\/span><\/p>\n<pre class=\"brush:bash\">Keith Henderson is a Backend Developer at the Engineering department earning an annual salary of $50000.0 with bonus $10000.0\n<\/pre>\n<p>From the above output, we see that the default implementation of the <code>getRole<\/code> method is ignored, whereas the default implementation of the <code>getBonus<\/code> method is the one that was invoked.<\/p>\n<h4 class=\"wp-block-heading\" id=\"h-3-4-2-abstract-class\">3.4.2 Abstract Class<\/h4>\n<p>The second way to achieve abstraction in Java is the abstract classes. An <strong>abstract<\/strong> class is a class which is declared <code>abstract<\/code> and can contain either abstract methods without implementation details or methods that have implementation details. Similar to an interface, an abstract class cannot be instantiated, as such, we need to create a class and extend the abstract class. A class that extends an abstract class must provide implementation details for the abstract methods and can optionally do the same for the non-abstract methods. Below we find an example of a class that extends an abstract class.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.abstractclass.AbstractEmployee<\/em><\/span><\/p>\n<pre class=\"brush:java\">public abstract class AbstractEmployee {\n    \n    private static final double BONUS_PERCENTAGE = 0.2;\n\n    abstract String getName();\n\n    abstract double getSalary();\n\n    abstract String getDepartment();\n\n    double getBonus() {\n        return calcBonus();\n    }\n\n    String getRole() {\n        return \"Employee\";\n    }\n    \n    private double calcBonus() {\n        return getSalary() * BONUS_PERCENTAGE;\n    }\n\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.abstractclass.Employee<\/em><\/span><\/p>\n<pre class=\"brush:java\">public class Employee extends AbstractEmployee {\n\n    private String name;\n    private double salary;\n    private String department;\n    private String role;\n\n    public Employee(String name, double salary, String department, String role) {\n        this.name = name;\n        this.salary = salary;\n        this.department = department;\n        this.role = role;\n    }\n\n    @Override\n    public String getName() {\n        return name;\n    }\n\n    @Override\n    public double getSalary() {\n        return salary;\n    }\n\n    @Override\n    public String getDepartment() {\n        return department;\n    }\n\n    @Override\n    public String getRole() {\n        return role;\n    }\n\n    @Override\n    public String toString() {\n        return getName() + \" is a \" + getRole() + \" at the \" + getDepartment()\n                + \" department earning an annual salary of $\" + salary + \" with bonus $\" + getBonus();\n    }\n\n}\n<\/pre>\n<p>In the above code, we create the abstract class <code>AbstractEmployee<\/code> which provides abstract methods without any implementation details, using the <code>abstract<\/code> prefix on the methods. We also provide implementation for the <code>getBonus<\/code> and <code>getRole<\/code> methods, like we did in a previous example. The main difference between this abstract class and the interface we created before, is that the abstract class also contains private fields and methods.<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-4-java-oops-other-concepts\"><a name=\"oops_other_concepts\"><\/a>4. Java OOPS Other Concepts<\/h2>\n<p>In the previous section we took a look at the four OOPS main concepts. In this section we will look at three other concepts which are very important to OOPS:<\/p>\n<ul class=\"wp-block-list\">\n<li>Association<\/li>\n<li>Aggregation<\/li>\n<li>Composition<\/li>\n<\/ul>\n<h3 class=\"wp-block-heading\" id=\"h-4-1-association\"><a name=\"oops_association\"><\/a>4.1 Association<\/h3>\n<p>Association refers to the relationship between different objects and how they are related to each other. To achieve association, one object must hold a reference to another object. It is advised not to have a bi-directional association of two objects.<\/p>\n<p>The two types of association that we will look into are aggregation and composition.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-4-2-aggregation\"><a name=\"oops_aggregation\"><\/a>4.2 Aggregation<\/h3>\n<p>Aggregation is the weak type of association, where an object <strong>a<\/strong> that is referenced by an object <strong>b<\/strong> can exist even without <strong>b<\/strong>. For example, an employee can belong to a department but if a department holds no employees (all fired!), then the department can still exist. This is demonstrated in the example below.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.aggregation.Department<\/em><\/span><\/p>\n<pre class=\"brush:java\">public class Department {\n\n    private String name;\n\n    public void setName(String name) {\n        this.name = name;\n    }\n\n    public String getName() {\n        return name;\n    }\n\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.aggregation.Employee<\/em><\/span><\/p>\n<pre class=\"brush:java\">public class Employee {\n\n    private String name;\n    private int salary;\n    private Department department;\n    \n    public String getName() {\n        return name;\n    }\n    \n    public void setName(String name) {\n        this.name = name;\n    }\n    \n    public int getSalary() {\n        return salary;\n    }\n    \n    public void setSalary(int salary) {\n        this.salary = salary;\n    }\n    \n    public Department getDepartment() {\n        return department;\n    }\n    \n    public void setDepartment(Department department) {\n        this.department = department;\n    }\n\n}\n<\/pre>\n<p>In the example above, the <code>Employee<\/code> class holds a reference to the <code>Department<\/code> class, as an employee must always belong to a department. The department, on the other hand, can exist as an entity even without any employee in it.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-4-3-composition\"><a name=\"oops_composition\"><\/a>4.3 Composition<\/h3>\n<p>Composition is the strong type of association, where an object <strong>a<\/strong> that is referenced by an object <strong>b<\/strong> cannot exist without <strong>b<\/strong>. For example, an employee gets an identity in a company when hired. This identity is unique to each employee and cannot exist if the employee leaves the company. Let&#8217;s see an example of this below.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.composition.Identity<\/em><\/span><\/p>\n<pre class=\"brush:java\">public class Identity {\n\n    private String id;\n\n    public String getId() {\n        return id;\n    }\n    \n    public void setId(String id) {\n        this.id = id;\n    }\n\n}\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.composition.Employee<\/em><\/span><\/p>\n<pre class=\"brush:java\">public class Employee {\n\n    private Identity identity;\n    private String name;\n    private int salary;\n    private String department;\n\n    public Identity getIdentity() {\n        return identity;\n    }\n    \n    public void setIdentity(Identity identity) {\n        this.identity = identity;\n    }\n    \n    public String getName() {\n        return name;\n    }\n\n    public void setName(String name) {\n        this.name = name;\n    }\n\n    public int getSalary() {\n        return salary;\n    }\n\n    public void setSalary(int salary) {\n        this.salary = salary;\n    }\n\n    public String getDepartment() {\n        return department;\n    }\n    \n    public void setDepartment(String department) {\n        this.department = department;\n    }\n\n}\n<\/pre>\n<p>As we see from the above code, the <code>Employee<\/code> class holds a reference to the <code>Identity<\/code> class. The <code>Identity<\/code> objects cannot exist in the system if the <code>Employee<\/code> objects they are referenced to no longer exist.<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-5-advantages-of-oops-in-java\"><a name=\"oops_advantages\"><\/a>5. Advantages of OOPS in Java<\/h2>\n<p>After examining the concepts of OOPS, we will take a look at the advantages of using OOPS in large and complex programs:<\/p>\n<ul class=\"wp-block-list\">\n<li>Clear <strong>modular<\/strong> structure<\/li>\n<li>Objects can be <strong>reused<\/strong> by other modules<\/li>\n<li>Easy to <strong>maintain<\/strong> and change the existing code<\/li>\n<li>Implementation details are <strong>hidden<\/strong> from other modules<\/li>\n<li>Breaks down solution into smaller <strong>real-life<\/strong> models<\/li>\n<li>Program can be <strong>extended<\/strong> and support new features much easier<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"h-6-eclipse-source-actions\"><a name=\"eclipse_source_actions\"><\/a>6. Eclipse Source Actions<\/h2>\n<p>In the previous sections, we created several classes that all had <strong>constructors<\/strong>, <strong>getters, setters<\/strong>, and <strong>toString<\/strong> methods. Eclipse has built-in actions to make our life easier and generate all those for us without having to write a single line of code. Sounds exciting? Before we see those actions, let&#8217;s see first the class that we will be created after Eclipse generates all those.<\/p>\n<p><span style=\"text-decoration: underline\"><em>com.javacodegeeks.sourceactions.Employee<\/em><\/span><\/p>\n<pre class=\"brush:java\">public class Employee {\n\n    private String name;\n    private double salary;\n    private String department;\n    private String role;\n    \n    public Employee(String name, double salary, String department, String role) {\n        this.name = name;\n        this.salary = salary;\n        this.department = department;\n        this.role = role;\n    }\n\n    public String getName() {\n        return name;\n    }\n\n    public void setName(String name) {\n        this.name = name;\n    }\n\n    public double getSalary() {\n        return salary;\n    }\n\n    public void setSalary(double salary) {\n        this.salary = salary;\n    }\n\n    public String getDepartment() {\n        return department;\n    }\n\n    public void setDepartment(String department) {\n        this.department = department;\n    }\n\n    public String getRole() {\n        return role;\n    }\n\n    public void setRole(String role) {\n        this.role = role;\n    }\n    \n    @Override\n    public String toString() {\n        return \"Employee [name=\" + name + \", salary=\" + salary + \", department=\" + department + \", role=\" + role + \"]\";\n    }\n\n}\n<\/pre>\n<p>The above class does not differ from any class we have created so far. It has a constructor, setters, getters and toString methods.<\/p>\n<p> Let\u2019s now generate all those, by starting with the <code>Employee<\/code> class in which we have added only the fields. To view the actions we open the class in Eclipse, we right click on it and select <em>Source<\/em>, as shown below.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"820\" height=\"639\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_eclipse_source_actions.jpg\" alt=\"OOPS Concepts Java - Source Actions in Eclipse\" class=\"wp-image-67124\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_eclipse_source_actions.jpg 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_eclipse_source_actions-300x234.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_eclipse_source_actions-768x598.jpg 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><figcaption>Source Actions in Eclipse<\/figcaption><\/figure>\n<\/div>\n<p>In the following sections, we will select the actions we are interested in one by one.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-6-1-generate-constructor\"><a name=\"generate_constructor\"><\/a>6.1 Generate Constructor<\/h3>\n<p>To generate the constructor of the <code>Employee<\/code> class,  we select the <em>Generate Constructor using Fields<\/em> action. There we can select any field that we would want to be added to the constructor. We select all the fields and click <em>OK<\/em>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"508\" height=\"512\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_eclipse_generate_constructor.jpg\" alt=\"OOPS Concepts Java - Generate Constructor\" class=\"wp-image-67125\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_eclipse_generate_constructor.jpg 508w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_eclipse_generate_constructor-150x150.jpg 150w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_eclipse_generate_constructor-298x300.jpg 298w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_eclipse_generate_constructor-70x70.jpg 70w\" sizes=\"(max-width: 508px) 100vw, 508px\" \/><figcaption>Generate Constructor using Fields in Eclipse<\/figcaption><\/figure>\n<\/div>\n<p>When this action is finished it generates a public constructor with all the fields of our class.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-6-2-generate-getters-and-setters\"><a name=\"generate_getters_setters\"><\/a>6.2 Generate Getters and Setters<\/h3>\n<p>Similar to the previous action, the getters and setters can be generated by selecting <em>Generate Getters and Setters<\/em>. Most of the times we want to select all the fields so we do that and click <em>OK<\/em>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"517\" height=\"639\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_eclipse_generate_setters_getters.jpg\" alt=\"OOPS Concepts Java - Getters and Setters\" class=\"wp-image-67127\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_eclipse_generate_setters_getters.jpg 517w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_eclipse_generate_setters_getters-243x300.jpg 243w\" sizes=\"(max-width: 517px) 100vw, 517px\" \/><figcaption>Generate Getters and Setters in Eclipse<\/figcaption><\/figure>\n<\/div>\n<p>This action generates public getters and setters for all the fields of the class and places them after the constructor.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-6-3-generate-tostring\"><a name=\"generate_toString\"><\/a>6.3 Generate toString()<\/h3>\n<p>As we have mentioned the <code>toString()<\/code> method is a very popular method which should be implemented by almost all classes, as it&#8217;s very important to print the objects in a program. To generate this method select <em>Generate toString()<\/em>, select all the fields and click <em>OK<\/em>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"502\" height=\"639\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_generate_toString.jpg\" alt=\"OOPS Concepts Java - Generate toString()\" class=\"wp-image-67129\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_generate_toString.jpg 502w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/oops_concepts_generate_toString-236x300.jpg 236w\" sizes=\"(max-width: 502px) 100vw, 502px\" \/><figcaption>Generate toString() in Eclipse<\/figcaption><\/figure>\n<\/div>\n<p>This action generates the <code>toString<\/code> method which is overridden by the Object class. The default template of Eclipse is good enough but we can also provide a custom one.<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-7-java-oops-concepts-conclusion\"><a name=\"conclusion\"><\/a>7. Java OOPS Concepts &#8211; Conclusion<\/h2>\n<p>In this post, we examined the four main OOPS concepts in Java: Encapsulation, Inheritance, Polymorphism, and Abstraction. We saw other OOPS concepts in Java such as Association, Aggregation, and Composition. Also, we took a look at the advantages of using OOPS in large and complex programs and how to make our life easier in OOPS with Eclipse. <\/p>\n<h2 class=\"wp-block-heading\" id=\"h-8-download-the-eclipse-project\"><a name=\"download_eclipse_project\"><\/a>8. Download the Eclipse project<\/h2>\n<p>That was an OOPS Consepts Java Tutorial.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of the above examples here:  <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/03\/JavaOOPSConceptsTutorial.zip\"><strong>OOPS Concepts Java Tutorial<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we feature a comprehensive OOPS Concepts Java Tutorial. We will take a look at the OOPS concepts in Java and provide code examples for each one of them. 1. Introduction Object-Oriented Programming System in Java, also known as OOPS, is a programming paradigm where the main concept of a program is based &hellip;<\/p>\n","protected":false},"author":172,"featured_media":1204,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[478,1749],"class_list":["post-66369","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-java","tag-oops"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>OOPS Concepts Java Tutorial - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Interested to learn more about Java? Then check out our detailed OOPS Concepts Java tutorial! The main concept of a program is based on objects.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"OOPS Concepts Java Tutorial - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Interested to learn more about Java? Then check out our detailed OOPS Concepts Java tutorial! The main concept of a program is based on objects.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/lefteris.karageorgiou.5\" \/>\n<meta property=\"article:published_time\" content=\"2019-03-19T13:00:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-03-01T10:24:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Lefteris Karageorgiou\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@lefkos77\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Lefteris Karageorgiou\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"21 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/\"},\"author\":{\"name\":\"Lefteris Karageorgiou\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/777c6779bb34786b415482271b84c5bb\"},\"headline\":\"OOPS Concepts Java Tutorial\",\"datePublished\":\"2019-03-19T13:00:32+00:00\",\"dateModified\":\"2021-03-01T10:24:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/\"},\"wordCount\":3005,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"keywords\":[\"Java\",\"OOPS\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/\",\"name\":\"OOPS Concepts Java Tutorial - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"datePublished\":\"2019-03-19T13:00:32+00:00\",\"dateModified\":\"2021-03-01T10:24:05+00:00\",\"description\":\"Interested to learn more about Java? Then check out our detailed OOPS Concepts Java tutorial! The main concept of a program is based on objects.\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"Bipartite Graph\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"OOPS Concepts Java Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/777c6779bb34786b415482271b84c5bb\",\"name\":\"Lefteris Karageorgiou\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7c6f5f16615b972cea90bb51c2a673d4bfd52036ade7ca8936069dd8fd1a46ad?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7c6f5f16615b972cea90bb51c2a673d4bfd52036ade7ca8936069dd8fd1a46ad?s=96&d=mm&r=g\",\"caption\":\"Lefteris Karageorgiou\"},\"description\":\"Lefteris is a Lead Software Engineer at ZuluTrade and has been responsible for re-architecting the backend of the main website from a monolith to event-driven microservices using Java, Spring Boot\/Cloud, RabbitMQ, Redis. He has extensive work experience for over 10 years in Software Development, working mainly in the FinTech and Sports Betting industries. Prior to joining ZuluTrade, Lefteris worked as a Senior Java Developer at Inspired Gaming Group in London, building enterprise sports betting applications for William Hills and Paddy Power. He enjoys working with large-scalable, real-time and high-volume systems deployed into AWS and wants to combine his passion for technology and traveling by attending software conferences all over the world.\",\"sameAs\":[\"https:\/\/www.facebook.com\/lefteris.karageorgiou.5\",\"https:\/\/www.linkedin.com\/in\/lefteris-karageorgiou-ba1ab926\/\",\"https:\/\/x.com\/lefkos77\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/lefteris-karageorgiou\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"OOPS Concepts Java Tutorial - Java Code Geeks","description":"Interested to learn more about Java? Then check out our detailed OOPS Concepts Java tutorial! The main concept of a program is based on objects.","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:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"OOPS Concepts Java Tutorial - Java Code Geeks","og_description":"Interested to learn more about Java? Then check out our detailed OOPS Concepts Java tutorial! The main concept of a program is based on objects.","og_url":"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_author":"https:\/\/www.facebook.com\/lefteris.karageorgiou.5","article_published_time":"2019-03-19T13:00:32+00:00","article_modified_time":"2021-03-01T10:24:05+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","type":"image\/jpeg"}],"author":"Lefteris Karageorgiou","twitter_card":"summary_large_image","twitter_creator":"@lefkos77","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Lefteris Karageorgiou","Est. reading time":"21 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/"},"author":{"name":"Lefteris Karageorgiou","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/777c6779bb34786b415482271b84c5bb"},"headline":"OOPS Concepts Java Tutorial","datePublished":"2019-03-19T13:00:32+00:00","dateModified":"2021-03-01T10:24:05+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/"},"wordCount":3005,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","keywords":["Java","OOPS"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/","url":"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/","name":"OOPS Concepts Java Tutorial - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","datePublished":"2019-03-19T13:00:32+00:00","dateModified":"2021-03-01T10:24:05+00:00","description":"Interested to learn more about Java? Then check out our detailed OOPS Concepts Java tutorial! The main concept of a program is based on objects.","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/oops-concepts-java-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"},{"@type":"ListItem","position":4,"name":"OOPS Concepts Java Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/777c6779bb34786b415482271b84c5bb","name":"Lefteris Karageorgiou","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7c6f5f16615b972cea90bb51c2a673d4bfd52036ade7ca8936069dd8fd1a46ad?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7c6f5f16615b972cea90bb51c2a673d4bfd52036ade7ca8936069dd8fd1a46ad?s=96&d=mm&r=g","caption":"Lefteris Karageorgiou"},"description":"Lefteris is a Lead Software Engineer at ZuluTrade and has been responsible for re-architecting the backend of the main website from a monolith to event-driven microservices using Java, Spring Boot\/Cloud, RabbitMQ, Redis. He has extensive work experience for over 10 years in Software Development, working mainly in the FinTech and Sports Betting industries. Prior to joining ZuluTrade, Lefteris worked as a Senior Java Developer at Inspired Gaming Group in London, building enterprise sports betting applications for William Hills and Paddy Power. He enjoys working with large-scalable, real-time and high-volume systems deployed into AWS and wants to combine his passion for technology and traveling by attending software conferences all over the world.","sameAs":["https:\/\/www.facebook.com\/lefteris.karageorgiou.5","https:\/\/www.linkedin.com\/in\/lefteris-karageorgiou-ba1ab926\/","https:\/\/x.com\/lefkos77"],"url":"https:\/\/examples.javacodegeeks.com\/author\/lefteris-karageorgiou\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/66369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/172"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=66369"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/66369\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1204"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=66369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=66369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=66369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}