
Hello Guys! Are you searching for OOPs Concepts in Java? Have you visited several websites, referred to many books, and you still are not able to grasp OOPs Concepts? Don’t worry! You have landed on the perfect page. We will end your search for OOPs Concepts in Java. Having perfect knowledge of oops concepts can help you to clear your viva or to crack your interview. Do you Know? OOPs Concepts in Java is one of the most asked interview questions. So if you have perfect knowledge of this then you can easily clear your interview.
OOPs Concepts in Java:
- Objects
- Class
- Abstraction
- Inheritance
- Polymorphism
- Encapsulation
Objects:
An Object is an instance of the class. For example, if the class is a human being then we all Humans are objects of this class. Real-world objects have two characteristics State and Behaviour.
Let’s understand what state and behavior is:
- The state of an object can be related to variables. For example, if there is a class named “Student”, then the state of an object of this class can be:
- Student’s name,
- Roll number of the students,
- Class of the student,
- Div of the student, etc.
- The behavior is referred as the different operation that can be performed by the object if the class is “Student”, then the example of the behavior of the object of this class is:
- Attending classes,
- Completing homework,
- Submitting assignments, etc.
Class:
Class is like a constructor for creating objects, it is also called a template for objects it consists of state and behaviors for objects. When we create an object from the class we can access the state and behaviors which are there inside of the class. Refer below example it will clear your concepts of class and object.
Example:
| public class Main{ public static void main(String[] args) { human h1 = new human(); human h2 = new human(); h1.name = “William”; h2.name = “James”; h1.web_developer(); h2.app_developer(); } }class human{ String name; int age; void web_developer(){ System.out.println(name+” is a web developer”); } void app_developer(){ System.out.println(name+” is a app developer”); } } |
In the above example, there is the class named human which has state and behavior name and age. These states and behaviors are web_developer and app_developer. From human class, we have created or constructed our objects named h1 and h2.
human h1 = new human();
human h2 = new human();
With these statements, we have constructed our new objects h1 and h2 from human class.
h1.name = “William”;
h2.name = “James”;
With these statements, we have access state from class for our objects h1 and h2 and assigned name for object h1 and h2 in a similar way we can assign age as well.
h1.web_developer();
h2.app_developer();
With these statements, we have accessed behaviors or operations that our objects can perform.
Now by running this code we get output as following:
| William is a web developer James is a app developer |
Abstraction:
Abstraction is the process where we hide unnecessary data and show only relevant data. For example, when we withdraw money from our bank account through ATM we just enter our ATM card and pin after this we receive our money but we don’t know what is happening in the backend how our data is transmitted to the server how they checked our authenticity this all data is hidden from the user so it called as an abstraction.
Example:
| public class Main{ public static void main(String[] args) { bike1 b1 = new bike1(); b1.price(); }} class bike1 extends bike{ void price(){ System.out.println(“price is 100000”); } } abstract class bike{ abstract void price(); } |
In the above example, there is the class bike which is an abstract class that means we can’t able to create an object of these class we can only extend the properties of the abstract class so here we have created another class bike1 which extends the class bike, Now we can create objects of class bike1 as you can see in above example that we have created object b1.
Inheritance:
Inheritance refers to the process by which one class inherits the properties and functions of another. Inheritance promotes code reuse by requiring each subclass to describe only those features that are unique to it, with the rest of the features inherited from the parent class.
- Inheritance is the process of creating a new class by extending the common data members and methods of an existing class.
- Inheritance helps us to reuse code, which increases java application reusability.
- The base class or super class is the parent class. The derived class, subclass or child class is a class that extends the base class.
- The most significant benefit of inheritance is that the base class’s code does not need to be rewritten in the child class.
Types of inheritance:
Single Inheritance:
A child and parent class relationship in which one class extends the other is referred to as single inheritance.
Example:
| Class a {…}Class b extends class a {…} |
Multilevel inheritance:
A child and parent class relationship in which a class extends the child class is referred to as multilevel inheritance. for example, Class A extends class B, while class B extends class C.
Example:
| Class a {….}Class b extends class a {….}Class c extends class b {… } |
Hierarchical inheritance:
When more than one class extends the same class, this is referred to as a child and parent class relationship. for example, Class B and class c extends class A.
Example:
| Class a {…} Class b extends class a {..}Class c extends class a {..} |
Multiple Inheritance:
It refers to the idea of a single class expanding to multiple classes, implying that a child class has two parents. Multiple inheritance is not supported in Java.
Polymorphism:
Polymorphism is a feature of object-oriented programming that allows us to perform the same action in multiple ways. Let’s assume we have a class Animals with a method Sound(); we can’t make this method an implementation because we don’t know which Animal class will expand Animal. As a result, we abstract this approach as follows:
Example:
| public class Animals {…Public void sound ( ) {System.out.println ( “Animals sounds“ );}}public class Lion extends Animals {…@overridepublic void sound ( ) {System.out.println( “roar” ) ;}}public class tiger extends Animals ( ) {….@overridePublic void sound ( ){System.out.println( “growl” ) ;}} |
Types of polymorphism:
Static polymorphism:
Static polymorphism is a polymorphism that is resolved during the compilation process.
Dynamic polymorphism:
Dynamic polymorphism refers to a procedure in which a call to an overridden method is resolved at runtime rather than at compile time. It is also called Runtime polymorphism.
Encapsulation:
Encapsulation is the process of tying together an object’s state (fields) and behavior (methods). Encapsulation is what you do when you create a class. Encapsulation is a protective wrapper that keeps outsiders out of the code and data. A well-defined interface is used to control them.
Example:
| class person {// private field private int age;//getter method Public int getage ( ) {return age;}//setter method public void setAge ( int age ) {this. Age = age;}}class Main {public static void main (String args []);//create an object of person person p1 = new person ();//change age using setter A1. setAge (12);// access age using getter System.out.println(“person age is” + a1. getage ( ) );}} |
We declared a private field called age that can only be accessed within the class in this example.
This brings us to the end of the blog on OOPs Concepts in Java. We hope you were able to gain some valuable knowledge from the same.










