OOPs concepts in Java
Soresa
Last Update 4 years ago
- In this blog, we will go over the fundamentals of OOPs concepts in Java. Object-oriented programming is a model that includes various concepts such as inheritance, abstraction, polymorphism, and so on. These ideas seek to incorporate real-world entities into programs. They develop working methods and variables that can be reused without jeopardizing security. This emphasizes data over functions. Among the most popular and significant object-oriented programming languages are Java, C++, C#, JavaScript, Python, Ruby, Perl, Smalltalk, and others.
- What is OOPs Concept?
- What is OOPs in java?
- List of OOPs Concepts in Java
- Advantages of OOPs Concept
- Disadvantages of OOPs Concept
- Differences between Object-Oriented Programming, Procedural Oriented Programming?
- Difference between an object-oriented programming language and an object-based programming language?
What is OOPs Concept?
- Object-oriented programming is a core of Java Programming, which is used for designing a program using classes and objects. This can also be characterized as data controlling for accessing the code. In this type of approach, programmers define the data type of a data structure and the operations that are applied to the data structure.
What is OOPs in java?
- OOps in java is to improve code readability and reusability by defining a Java program efficiently. The main principles of object-oriented programming are abstraction, encapsulation, inheritance, and polymorphism. These concepts aim to implement real-world entities in programs.
List of OOPs Concepts in Java
- Objects
- Classes
- Object
- Class
- Abstraction
- Inheritance
- Polymorphism
- Encapsulation

What are Objects?
- Objects are always called instances of a class which are created from class in java or any other language. They have states and behaviour.
- These objects always correspond to things found in the real world, i.e., real entities. So, they are also called a run-time entity of the world. These are self–contained which consists of methods and properties which make data useful. Objects can be both physical and logical data. It contains addresses and takes up some space in memory. Some examples of objects are a dog, chair, tree etc.
- When we treat animals as objects, it has states like colour, name, breed etc., and behaviours such as eating, wagging the tail etc.
- Suppose, we have created a class called My book, we specify the class name followed by the object name, and we use the keyword new.
Object Example 1:Object Example 1:
In the above example, a new object is created, and it returns the value of x which may be the number of books.
Mybook Myobj= new Mybook ();
This is the statement used for creating objects.
System.out.println(Myobj.x);
This statement is used to return the value of x of an object.
We can also create multiple objects in the same class and we can create in one class and access it in another class. This method is used for better organization of classes and always remember that name of the java file and the class name remains the same.
Object Example 2:
The below example shows how multiple objects are created in the same class and how they are accessed from another class.
- Mybook.java
- Count.java
When this program is compiled, it gives the result as 10, and 8 respectively.
What are Classes?
- Classes are like object constructors for creating objects. The collection of objects is said to be a class. Classes are said to be logical quantities. Classes don’t consume any space in the memory. Class is also called a template of an object. Classes have members which can be fields, methods and constructors. A class has both static and instance initializers.
- A class declaration consists of:
- Modifiers: Can be public or default access.
- Class name: Initial letter.
- Superclass: A class can only extend (subclass) one parent.
- Interfaces: A class can implement more than one interface.
- Body: Body surrounded by braces, { }.
- A class keyword is used to create a class. A simplified general form of the class definition is given below:
The variables or data defined within a class are called instance variables. Code is always contained in the methods. Therefore, the methods and variables defined within a class are called members of the class. All the methods have the same form as main () these methods are not specified as static or public.
What is Abstraction?
- Abstraction is a process which displays only the information needed and hides the unnecessary information. We can say that the main purpose of abstraction is data hiding. Abstraction means selecting data from a large number of data to show the information needed, which helps in reducing programming complexity and efforts.
- There are also abstract class and abstract methods. An abstract class is a type of class that declares one or more abstract methods. An abstract method is a method that has a method definition but not implementation. Once we have modelled our object using data abstraction, the same sets of data can also be used in different applications—abstract classes, generic types of behaviours and object-oriented programming hierarchy. Abstract methods are used when two or more subclasses do the same task in different ways and through different implementations. An abstract class can have both the methods, i.e., abstract methods and regular methods.
Now let us see an example related to abstraction.
- Suppose we want to create a student application and ask to collect the information about the student.
We collect the following information.
- Name
- Class
- Address
- Dob
- Fathers name
- Mothers name and so on.
We may not require every information that we have collected to fill the application. So, we select the data that is required to fill the application. Hence, we have fetched, removed, and selected the data, the student information from large data. This process is known as abstraction in the oops concept.
Abstract class example:
Output:
Roar
What is Inheritance?
- Inheritance is a method in which one object acquires/inherits another object’s properties, and inheritance also supports hierarchical classification. The idea behind this is that we can create new classes built on existing classes, i.e., when you inherit from an existing class, we can reuse methods and fields of the parent class. Inheritance represents the parent-child relationship.
- For example, a whale is a part of the classification of marine animals, which is part of class mammal, which is under that class of animal. We use hierarchical classification, i.e., top-down classification. If we want to describe a more specific class of animals such as mammals, they would have more specific attributes such as teeth; cold-blooded, warm-blooded, etc. This comes under the subclass of animals whereas animals come under the superclass. The subclass is a class which inherits properties of the superclass. This is also called a derived class. A superclass is a base class or parental class from which a subclass inherits properties.
- To inherit a class, we use the extend keyword.

There are five types of inheritance single, multilevel, multiple, hybrid and hierarchical.
Single level
- In this one class i.e., the derived class inherits properties from its parental class. This enables code reusability and also adds new features to the code. Example: class b inherits properties from class a.
- Class A is the base or parental class and class b is the derived class.
Syntax:
Multilevel
- This one class is derived from another class which is also derived from another class i.e., this class has more than one parental class, hence it is called multilevel inheritance.
Syntax:
Hierarchical level
- In this one parental class has two or more derived classes or we can say that two or more child classes has one parental class.
Syntax:
Hybrid inheritance
- This is the combination of multiple and multilevel inheritance and in java multiple inheritance is not supported as it leads to ambiguity and this type of inheritance can only be achieved through interfaces.
- Consider that class a is the parental or base class of class b and class c and in turn class b and class c are parental or base class of class d. Class b and class c are derived classes from class a and class d is derived class from class b and class c.
- Following program creates a super class called add and a subclass called sub, uses extend keyword to create a subclass add.
It gives output as – total = 22
What is Polymorphism?
- Polymorphism refers to many forms, or it is a process that performs a single action in different ways. It occurs when we have many classes related to each other by inheritance. Polymorphism is of two different types, i.e., compile-time polymorphism and runtime polymorphism. One of the examples in Compile time polymorphism is that when we overload a static method in java. Run time polymorphism is also called a dynamic method dispatch is a method in which a call to an overridden method is resolved at run time rather than compile time. In this method, the overridden method is always called through the reference variable. By using method overloading and method overriding, we can perform polymorphism.
- Generally, the concept of polymorphism is often expressed as one interface, multiple methods. This reduces complexity by allowing the same interface to be used as a general class of action.
Example:
- In the above example, we can see common action sound () but there are different ways to do the same action. This is one of the examples which shows polymorphism.
Polymorphism in java can be classified into two types:
- Static / Compile-Time Polymorphism
- Dynamic / Runtime Polymorphism
What is Compile-Time Polymorphism in Java?
- Compile-Time polymorphism in java is also known as Static Polymorphism. to resolved at compile-time which is achieved through Method Overloading.
What is Runtime Polymorphism in Java?
- Runtime polymorphism in java is also known as Dynamic Binding which is used to call to an overridden method that is resolved dynamically at runtime rather than at compile-time.
What is Encapsulation?
- Encapsulation is one of the concepts in OOPs concepts; it is the process that binds together the data and code into a single unit and keeps both from being safe from outside interference and misuse. In this process, the data is hidden from other classes and can be accessed only through the current class’s methods. Hence, it is also known as data hiding. Encapsulation acts as a protective wrapper that prevents the code and data from being accessed by outsiders. These are controlled through a well-defined interface.
- Encapsulation is achieved by declaring the variables as private and providing public setter and getter methods to modify and view the variable values. In encapsulation, the fields of a class are made read-only or write-only. This method also improves the re-usability. Encapsulated code is also easy to test for unit testing.
Example:
output:
Animal age is 12
- In this example, we declared a private field called age that cannot be accessed outside of the class.
- To access age, we used public methods. These methods are called getter and setter methods. Making age private allows us to restrict unauthorized access from outside the class. Hence this is called data hiding.
Advantages of OOPs Concept
- Re-usability
- Data redundancy
- Code maintenance
- Security
- Design benefits
- Easy troubleshooting
Disadvantages of OOPs Concept
- Effort – Lot of work is put into creating these programs.
- Speed – These programs are slower compared to other programs.
- Size – OOPs programs are bigger when compared to other programs.
Differences between Object-Oriented Programming, Procedural Oriented Programming?
| Object-oriented programming | Procedure oriented programming |
|---|---|
| It is object-oriented. | It is structured and oriented. |
| It follows a bottom-up approach. | It is divided into small parts called functions. |
| These are divided into small parts called objects. | It follows a top-down approach. |
| These have specifiers like public, private, and protected. | There are no access specifiers. |
| Adding new functions or data is easy. | Adding new data and functions is not easy. |
| It provides data hiding and it is more secure. | This is less secure. |
| Overloading is possible. | Overloading is not possible. |
| Examples are c++, java, python etc. | Examples FORTRAN, Cobol etc. |
