{"id":19252,"date":"2020-11-24T19:16:26","date_gmt":"2020-11-24T13:46:26","guid":{"rendered":"https:\/\/internshala.com\/blog\/?p=19252"},"modified":"2021-01-09T22:16:08","modified_gmt":"2021-01-09T16:46:08","slug":"oop-concepts-in-java-the-essence-of-java-programming","status":"publish","type":"post","link":"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/","title":{"rendered":"OOP concepts in Java: The essence of Java programming"},"content":{"rendered":"<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19254 aligncenter\" src=\"https:\/\/internshala.com\/wp-content\/uploads\/2020\/11\/What-is-OOP-in-Java.jpg\" alt=\"Object-oriented programming in Java\" width=\"595\" height=\"372\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Imagine that you are a librarian, and you want to create a Java application to keep a track of all library resources such as books, journals, and newspapers. Would you prefer to write separate code for each one of these or would you like to use reusable code? If you chose the latter, then OOP can help you with that.<\/span><\/p>\n<p style=\"text-align: justify;\"><b>What is OOP?<\/b><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Object Oriented Programming (OOP) is a programming paradigm that uses the real-world concepts of objects and classes.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Objects are modelled on real-life entities such as a book, a house, or a scooter. They have attributes such as color, model, and price, that is known as the <\/span><i><span style=\"font-weight: 400;\">state<\/span><\/i><span style=\"font-weight: 400;\"> of the object. These objects are capable of performing certain functions such as driving, accelerating, etc. that are referred to as <\/span><i><span style=\"font-weight: 400;\">methods<\/span><\/i><span style=\"font-weight: 400;\"> in Java terminology.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Different objects can share similar attributes and functions. For example, all scooters have some common properties such as they all have a color and price, and all of them can perform functions like driving, stopping, etc. This means that they can be created using a similar template. This template or blueprint is called a class in Java. So, for this example, we can create a class called Scooter.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Now that you have a basic understanding of classes and objects, let\u2019s dig deeper into all the OOP concepts.<\/span><\/p>\n<p style=\"text-align: justify;\"><b>OOP concepts in Java<\/b><\/p>\n<p style=\"text-align: justify;\"><b>1. Classes &#8211; <\/b><span style=\"font-weight: 400;\">Every Java program begins as a class that can contain fields, constructors, and methods. Here is an example of how to create a class in Java &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">class College { \/\/ class open<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">public String department; \/\/these are called fields<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">public int noOfStudents;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">public College (String departmentName, int strength) {<br \/>\n<\/span><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">} \/\/ class closed<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In the example above, the class has been declared by using the keyword <\/span><i><span style=\"font-weight: 400;\">class<\/span><\/i><span style=\"font-weight: 400;\">. The first letter of the class name <\/span><i><span style=\"font-weight: 400;\">College<\/span><\/i><span style=\"font-weight: 400;\"> has been capitalised, which is a naming convention in Java. The name is followed by a curly brace that is closed when the class is closed. The variables <\/span><i><span style=\"font-weight: 400;\">department<\/span><\/i><span style=\"font-weight: 400;\"> and <\/span><i><span style=\"font-weight: 400;\">noOfStudents<\/span><\/i><span style=\"font-weight: 400;\"> are called <\/span><i><span style=\"font-weight: 400;\">fields <\/span><\/i><span style=\"font-weight: 400;\">or<\/span><i><span style=\"font-weight: 400;\"> field variables<\/span><\/i><span style=\"font-weight: 400;\"> because they have been declared outside a method.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><b>2. Object &#8211;<\/b><span style=\"font-weight: 400;\"> An object is an instance of a class, which is created using the keyword <\/span><i><span style=\"font-weight: 400;\">new<\/span><\/i><span style=\"font-weight: 400;\">. For example &#8211;\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">College jamia = new College();<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The <\/span><i><span style=\"font-weight: 400;\">new <\/span><\/i><span style=\"font-weight: 400;\">keyword allocates memory to the object during runtime. <\/span><i><span style=\"font-weight: 400;\">jamia<\/span><\/i><span style=\"font-weight: 400;\"> is an instance\/reference variable that stores the location of this newly created object in heap memory.<\/span><\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-19255\" src=\"https:\/\/internshala.com\/wp-content\/uploads\/2020\/11\/Heap-memory.png\" alt=\"Objects in Java\" width=\"595\" height=\"372\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">If you do not create an instance variable for it, the object can still be created like this &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">new College();<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">However, you will not be able to use this object.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">To assign values to the variables <\/span><i><span style=\"font-weight: 400;\">department<\/span><\/i><span style=\"font-weight: 400;\"> and <\/span><i><span style=\"font-weight: 400;\">noOfStudents<\/span><\/i><span style=\"font-weight: 400;\">, you can follow the following syntax &#8211;\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">jamia.department = \u201cHindi\u201d;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">jamia.noOfStudents = 30;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">You can also assign these values through getter and setter methods, which is the preferred way because it makes the data more secure. The setter method is used to set the value. For example &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">public void setDepartment (String d) {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">this.department = d;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Here, we have used the return type as <\/span><i><span style=\"font-weight: 400;\">void <\/span><\/i><span style=\"font-weight: 400;\">because the method does not return any value.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The getter method is used to return a value. For example &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">public String getDepartment () {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">return department;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">This method has the String return type, which means it returns a String value.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Now, if you want to initialize the variables again, you can do it in the following way &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">College jamia = new College();<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">jamia.setDepartment(\u201cHindi\u201d);<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">To print this value, you can use the getter method &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">System.out.print(jamia.getDepartment() );<\/span><\/p>\n<p style=\"text-align: justify;\"><b>3. Constructors &#8211; <\/b><span style=\"font-weight: 400;\">While declaring an object, the <\/span><i><span style=\"font-weight: 400;\">new<\/span><\/i><span style=\"font-weight: 400;\"> keyword is followed by a call to the constructor.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">College jamia = new College();<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In the above example, new College() is the constructor. A class can have any number of constructors. The most basic is the default constructor that does not return any value. Here\u2019s how it looks like inside a class &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">class College {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">String department;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">int noOfStudents;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">public College() { \/\/default constructor<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">To create a constructor, you must follow some ground rules &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">i. Class and constructor(s) share the same name.<br \/>\n<\/span><span style=\"font-weight: 400;\">ii. A constructor is like that friend who never returns anything. You can, however, add values inside parameters with a constructor. For example &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Scooter activa = new Scooter(60000);<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">This is called a parameterized constructor. It will assign a value of 60000 to an instance variable. Here\u2019s how the constructor looks like inside the class &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">class Scooter {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">int price;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">String model;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">public Scooter(int p) {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">this.price = p; \/\/ <\/span><i><span style=\"font-weight: 400;\">this<\/span><\/i><span style=\"font-weight: 400;\"> is a keyword that refers to the current object. So, you are assigning value to the <\/span><i><span style=\"font-weight: 400;\">price<\/span><\/i><span style=\"font-weight: 400;\"> instance variable for the current object<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">You can also create multiple constructors with different parameters. This concept is called constructor overloading. So, in the above example of class Scooter, you can write another constructor &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">pubic Scooter(int p, String m) {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">this.price = p;\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">this.model = m;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The java compiler is a smart cookie which will figure out which constructor you have called depending on the values you pass during object declaration. For instance &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Scooter activa = new Scooter(60000, \u201cblack\u201d); \/\/ this will call the constructor with two parameters<\/span><\/p>\n<p style=\"text-align: justify;\"><b>iii.<\/b><span style=\"font-weight: 400;\"> If you are not creating a parameterized constructor, you do not have to create a default constructor.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">As you can see, you can use both constructors and setter methods to assign values to instance variables. However, the constructor is used when the values are known to you at the time of object declaration. Setter methods, on the other hand, allow you to set a value at a later stage as well.<\/span><\/p>\n<p style=\"text-align: justify;\"><b>4. Inheritance &#8211; <\/b><span style=\"font-weight: 400;\">Have you ever been grateful about a trait that you got from your parents like having your mom\u2019s hair or your dad\u2019s eyes? Because OOP is a paradigm that is largely based on the real-world, this is an important concept in Java as well!<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Inheritance is a concept which allows a class to inherit properties of another class. For example, let\u2019s take two classes called Batcycle and Batmobile. Although the two are different in many ways, they have some common properties such as engine, color, and price, and both of them are used for commuting. So, you can create a common class called Vehicle that has these properties and behaviour.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Once you have created the Vehicle class, you can use the keyword <\/span><i><span style=\"font-weight: 400;\">extends<\/span><\/i><span style=\"font-weight: 400;\"> to inherit the properties of this class. For example &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">class Batcycle extends Vehicle<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">class Batmobile extends Vehicle<\/span><\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-19256\" src=\"https:\/\/internshala.com\/wp-content\/uploads\/2020\/11\/Inheritance.png\" alt=\"Inheritance in Java\" width=\"595\" height=\"372\" \/><span style=\"font-weight: 400;\">You can use the inherited properties in the following way &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Batcycle bat = new Batcycle;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">bat.color = \u201cblack\u201d;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">bat.engine = \u201cV4\u201d;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Batmobile cat = new Batmobile;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">cat.color = \u201cred\u201d;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">cat.engine = \u201cV8\u201d;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In the above example, the Vehicle is called the parent or super class whereas Batcycle and Batmobile are called child classes or sub classes. This is called hierarchical inheritance. Other types of inheritance include single inheritance where one class inherits properties of another, and multilevel inheritance, which is multistep inheritance. For example, class BellBottomPants inherits properties of class Pants which has inherited the properties of class Clothes.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Inheritance helps in code optimisation by allowing us to reuse code.<\/span><\/p>\n<p style=\"text-align: justify;\"><b>5. Polymorphism &#8211; <\/b><span style=\"font-weight: 400;\">While inheritance allows class(es) to inherit fields and methods of another class, polymorphism makes it possible for one class to use the methods of another class. This is called runtime polymorphism, and it takes place in the form of method overriding. This means that all the classes use the same method signature, but can differ in their code. For example &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">class Vehicle {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">String color;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">String model;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">public void commute() {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">System.out.println(\u201cAll vehicles are used for commuting\u201d);<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">class Batcycle extends Vehicle {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">String ownerName;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">public void commute() { \/\/using the same method signature as the Vehicle class<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">System.out.println(\u201cOnly two passengers can commute on a Batcycle\u201d);<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">When the program is executed, the JVM figures out which method to run. In the example below, it will run the commute() method of the Batcycle class.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Batcycle b = new Batcycle;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">b.commute();<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The other type of polymorphism in Java is called compile time polymorphism, and you can use this through method overloading. In this type, methods with the same name can take many forms. For example &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">public void calculateStipend() { \/\/it does not return a value<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">\/\/code<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">public int calculateStipend(int a, int b) { \/\/it returns an integer value<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">\/\/code<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The compiler decides which function to call. Compile time polymorphism is primarily used to enhance the readability of the code.<\/span><\/p>\n<p style=\"text-align: justify;\"><b>6. Encapsulation &#8211; <\/b><span style=\"font-weight: 400;\">The subtle art of data hiding in Java is called encapsulation. It involves wrapping the data in class variables with its functions. This can be achieved by declaring the class variables as <\/span><i><span style=\"font-weight: 400;\">private<\/span><\/i><span style=\"font-weight: 400;\"> and using getter and setter methods to initialize them. For example &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">class JavaBasics {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">private int num1;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">private int num2;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">private int difference;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">public int getTheDifference() {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">this.difference = num1 &#8211; num2;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">return difference;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">public void setValues(int n1, int n2) {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">if(n1&gt;n2) { \/\/a setter method can also help you in controlling the values the variables accept<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">this.num1 = n1;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">this.num2 = n2;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">else {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">System.out.println(\u201cThe first number should be greater than the second\u201d);<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Now, let\u2019s create an object of class JavaBasics and call these methods.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">class Test {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">JavaBasics obj1 = new JavaBasics();<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">obj1.setValues(50, 40); \/\/you cannot use obj1.num1 = 50 or obj1.num2 = 40 because they are not public variables that can be accessed directly outside the class<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">obj1.getTheDifference();<br \/>\n<\/span><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In the example above, the <\/span><i><span style=\"font-weight: 400;\">private<\/span><\/i><span style=\"font-weight: 400;\"> access modifier and the getter and setter methods control the scope of the data stored and used in the class. This makes the data more secure which makes encapsulation a common practise among Java developers.<\/span><\/p>\n<p style=\"text-align: justify;\"><b>6. Abstraction &#8211; <\/b><span style=\"font-weight: 400;\">To implement inheritance in Java, we can create a superclass whose objects cannot be created. These are called abstract classes, and they can also contain methods with no body, which are called abstract methods.This process is called abstraction, and it gives a structure to the objects of the subclasses without showing unnecessary information. For example &#8211;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">abstract class Language {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">int numberOfSpeakers;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">public abstract void printNumber(); \/\/abstract methods are closed with a semicolon<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">public void getNumber(int n) {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">this.numberOfSpeakers = n;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">These were the key OOP concepts in Java. Now that you know the basics of OOPs, you can put them into practise by creating an app in <\/span><a href=\"https:\/\/trainings.internshala.com\/java-training?utm_source=IS_Blog&amp;referral=BLOG10&amp;utm_medium=OOP-concepts-in-Java\"><span style=\"font-weight: 400;\">Internshala\u2019s Core Java training<\/span><\/a><span style=\"font-weight: 400;\">! You can use the coupon code BLOG10 to get a discount of 10%.<\/span><\/p>\n<aside class=\"mashsb-container mashsb-main \"><div class=\"mashsb-box\"><div class=\"mashsb-count mash-medium\" style=\"float:left\"><div class=\"counts mashsbcount\">284<\/div><span class=\"mashsb-sharetext\">SHARES<\/span><\/div><div class=\"mashsb-buttons\"><a class=\"mashicon-facebook mash-medium mashsb-noshadow\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Finternshala.com%2Fblog%2Foop-concepts-in-java-the-essence-of-java-programming%2F\" target=\"_top\" rel=\"nofollow\"><span class=\"icon\"><\/span><span class=\"text\">Share&nbsp;on&nbsp;Facebook<\/span><\/a><a class=\"mashicon-subscribe mash-medium mashsb-noshadow\" href=\"#\" target=\"_top\" rel=\"nofollow\"><span class=\"icon\"><\/span><span class=\"text\">Get&nbsp;Your&nbsp;Dream&nbsp;Internship<\/span><\/a><div class=\"onoffswitch2 mash-medium mashsb-noshadow\" style=\"display:none\"><\/div><\/div>\n            <\/div>\n                <div style=\"clear:both\"><\/div><\/aside>\n            <!-- Share buttons by mashshare.net - Version: 4.0.42-->","protected":false},"excerpt":{"rendered":"<p>Imagine that you are a librarian, and you want to create a Java application to keep a track of all library resources such as books, journals, and newspapers. Would you<\/p>\n","protected":false},"author":5355,"featured_media":19253,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[675,3943,4134,3889,1594,4171],"tags":[343,4156],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>OOP concepts in Java: The essence of Java programming - Internshala blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"OOP concepts in Java: The essence of Java programming - Internshala blog\" \/>\n<meta property=\"og:description\" content=\"Imagine that you are a librarian, and you want to create a Java application to keep a track of all library resources such as books, journals, and newspapers. Would you\" \/>\n<meta property=\"og:url\" content=\"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/\" \/>\n<meta property=\"og:site_name\" content=\"Internshala blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-24T13:46:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-09T16:46:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/internshala.com\/blog\/wp-content\/uploads\/2020\/11\/OOP-concepts-in-Java.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"390\" \/>\n\t<meta property=\"og:image:height\" content=\"205\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Internshala\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Internshala\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/\"},\"author\":{\"name\":\"Internshala\",\"@id\":\"https:\/\/internshala.com\/blog\/#\/schema\/person\/f0be3a17aa62a7b75486919fd00ecb20\"},\"headline\":\"OOP concepts in Java: The essence of Java programming\",\"datePublished\":\"2020-11-24T13:46:26+00:00\",\"dateModified\":\"2021-01-09T16:46:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/\"},\"wordCount\":1729,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/internshala.com\/blog\/#organization\"},\"keywords\":[\"Java\",\"Programming\"],\"articleSection\":[\"Available Trainings\",\"e-Learning\",\"Learning Tracks\",\"Online Training\",\"Programming\",\"Training SEO articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/\",\"url\":\"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/\",\"name\":\"OOP concepts in Java: The essence of Java programming - Internshala blog\",\"isPartOf\":{\"@id\":\"https:\/\/internshala.com\/blog\/#website\"},\"datePublished\":\"2020-11-24T13:46:26+00:00\",\"dateModified\":\"2021-01-09T16:46:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/internshala.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Learning Tracks\",\"item\":\"https:\/\/internshala.com\/blog\/learning-tracks\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Programming\",\"item\":\"https:\/\/internshala.com\/blog\/learning-tracks\/programming\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"OOP concepts in Java: The essence of Java programming\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/internshala.com\/blog\/#website\",\"url\":\"https:\/\/internshala.com\/blog\/\",\"name\":\"Internshala blog\",\"description\":\"Your favourite senior outside college\",\"publisher\":{\"@id\":\"https:\/\/internshala.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/internshala.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/internshala.com\/blog\/#organization\",\"name\":\"Internshala blog\",\"url\":\"https:\/\/internshala.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/internshala.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/internshala.com\/blog\/wp-content\/uploads\/2023\/08\/LOGO-1.png\",\"contentUrl\":\"https:\/\/internshala.com\/blog\/wp-content\/uploads\/2023\/08\/LOGO-1.png\",\"width\":112,\"height\":31,\"caption\":\"Internshala blog\"},\"image\":{\"@id\":\"https:\/\/internshala.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/internshala.com\/blog\/#\/schema\/person\/f0be3a17aa62a7b75486919fd00ecb20\",\"name\":\"Internshala\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/internshala.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/internshala.com\/blog\/wp-content\/uploads\/2025\/10\/IS-Logo-96x96.png\",\"contentUrl\":\"https:\/\/internshala.com\/blog\/wp-content\/uploads\/2025\/10\/IS-Logo-96x96.png\",\"caption\":\"Internshala\"},\"description\":\"Internshala is a career tech platform dedicated to building India\u2019s first early-career infrastructure at scale. With a mission to ensure that every individual\u2019s first opportunity aligns with their potential, Internshala bridges the gap between education and employability. It empowers students and graduates to discover their interests, gain practical skills, and access internships, and fresher job opportunities.\",\"sameAs\":[\"http:\/\/blog.internshala.com\",\"https:\/\/www.linkedin.com\/company\/internshala\/\"],\"url\":\"https:\/\/internshala.com\/blog\/author\/internshala\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"OOP concepts in Java: The essence of Java programming - Internshala blog","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:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/","og_locale":"en_US","og_type":"article","og_title":"OOP concepts in Java: The essence of Java programming - Internshala blog","og_description":"Imagine that you are a librarian, and you want to create a Java application to keep a track of all library resources such as books, journals, and newspapers. Would you","og_url":"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/","og_site_name":"Internshala blog","article_published_time":"2020-11-24T13:46:26+00:00","article_modified_time":"2021-01-09T16:46:08+00:00","og_image":[{"width":390,"height":205,"url":"https:\/\/internshala.com\/blog\/wp-content\/uploads\/2020\/11\/OOP-concepts-in-Java.jpg","type":"image\/jpeg"}],"author":"Internshala","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Internshala","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/#article","isPartOf":{"@id":"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/"},"author":{"name":"Internshala","@id":"https:\/\/internshala.com\/blog\/#\/schema\/person\/f0be3a17aa62a7b75486919fd00ecb20"},"headline":"OOP concepts in Java: The essence of Java programming","datePublished":"2020-11-24T13:46:26+00:00","dateModified":"2021-01-09T16:46:08+00:00","mainEntityOfPage":{"@id":"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/"},"wordCount":1729,"commentCount":2,"publisher":{"@id":"https:\/\/internshala.com\/blog\/#organization"},"keywords":["Java","Programming"],"articleSection":["Available Trainings","e-Learning","Learning Tracks","Online Training","Programming","Training SEO articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/","url":"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/","name":"OOP concepts in Java: The essence of Java programming - Internshala blog","isPartOf":{"@id":"https:\/\/internshala.com\/blog\/#website"},"datePublished":"2020-11-24T13:46:26+00:00","dateModified":"2021-01-09T16:46:08+00:00","breadcrumb":{"@id":"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/internshala.com\/blog\/oop-concepts-in-java-the-essence-of-java-programming\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/internshala.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Learning Tracks","item":"https:\/\/internshala.com\/blog\/learning-tracks\/"},{"@type":"ListItem","position":3,"name":"Programming","item":"https:\/\/internshala.com\/blog\/learning-tracks\/programming\/"},{"@type":"ListItem","position":4,"name":"OOP concepts in Java: The essence of Java programming"}]},{"@type":"WebSite","@id":"https:\/\/internshala.com\/blog\/#website","url":"https:\/\/internshala.com\/blog\/","name":"Internshala blog","description":"Your favourite senior outside college","publisher":{"@id":"https:\/\/internshala.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/internshala.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/internshala.com\/blog\/#organization","name":"Internshala blog","url":"https:\/\/internshala.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/internshala.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/internshala.com\/blog\/wp-content\/uploads\/2023\/08\/LOGO-1.png","contentUrl":"https:\/\/internshala.com\/blog\/wp-content\/uploads\/2023\/08\/LOGO-1.png","width":112,"height":31,"caption":"Internshala blog"},"image":{"@id":"https:\/\/internshala.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/internshala.com\/blog\/#\/schema\/person\/f0be3a17aa62a7b75486919fd00ecb20","name":"Internshala","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/internshala.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/internshala.com\/blog\/wp-content\/uploads\/2025\/10\/IS-Logo-96x96.png","contentUrl":"https:\/\/internshala.com\/blog\/wp-content\/uploads\/2025\/10\/IS-Logo-96x96.png","caption":"Internshala"},"description":"Internshala is a career tech platform dedicated to building India\u2019s first early-career infrastructure at scale. With a mission to ensure that every individual\u2019s first opportunity aligns with their potential, Internshala bridges the gap between education and employability. It empowers students and graduates to discover their interests, gain practical skills, and access internships, and fresher job opportunities.","sameAs":["http:\/\/blog.internshala.com","https:\/\/www.linkedin.com\/company\/internshala\/"],"url":"https:\/\/internshala.com\/blog\/author\/internshala\/"}]}},"_links":{"self":[{"href":"https:\/\/internshala.com\/blog\/wp-json\/wp\/v2\/posts\/19252"}],"collection":[{"href":"https:\/\/internshala.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/internshala.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/internshala.com\/blog\/wp-json\/wp\/v2\/users\/5355"}],"replies":[{"embeddable":true,"href":"https:\/\/internshala.com\/blog\/wp-json\/wp\/v2\/comments?post=19252"}],"version-history":[{"count":0,"href":"https:\/\/internshala.com\/blog\/wp-json\/wp\/v2\/posts\/19252\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/internshala.com\/blog\/wp-json\/wp\/v2\/media\/19253"}],"wp:attachment":[{"href":"https:\/\/internshala.com\/blog\/wp-json\/wp\/v2\/media?parent=19252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/internshala.com\/blog\/wp-json\/wp\/v2\/categories?post=19252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/internshala.com\/blog\/wp-json\/wp\/v2\/tags?post=19252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}