OOPs Concepts in Java refers to the object oriented programming style which tries to implement the real world entities as objects, it intends to improve code readability and reusability. Object-oriented programming is a method used for designing a program using classes and objects.
OOPs Concepts in Java uses the following concepts.
- Objects
- Classes
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Objects:
Objects are abstract data types that have an identity, state and behaviour which mimic the real world entities. For example train, car, fan, etc., which have different states or properties like passenger train, superfast or fast train and behaviour like moving, stopping etc.
- Identity: A unique name to identify an object.
- State: It defines the properties of an object.
- Behavior: Implemented by methods.
Objects are created from the class using a new keyword in front of the class constructor.
Eg:
class Train {
string type = “superfast”;
string destination= “delhi”;
public static void main(String args[])
{
Train mytrain = new Train();
system.out.println(mytrain.type);
system.out.println(mytrain.destination);
}
}
In the above example, we have created an object mytrain of train class and printed the type and destination property of this object.
- Class:
A class is a blueprint from which objects are created. It consists of properties and methods that are common to all objects of that class.
Class declarations include.
- Modifiers: which defines the accessibility of a class. It can be public, private, protected or has default access.
- Class keyword: It is used to create a class.
- Class name: The name of the class should begin with an initial letter.
Example:
public class xyz extends abc implements p,q,r
{
}
In the above example, we have defined a public class xyz which inherits class abc and implements multiple interfaces p, q, r.
- Abstraction:
Abstraction is one of the important OOPs Concepts in Java. It simplifies complexity by hiding irrelevant details from the user. That enables the user to implement more complex logic on top of the provided abstraction without understanding or even thinking about all the hidden complexity.
Abstraction hides the details and complexity from the users and shows them only the relevant information. For example, if you want to travel in an aeroplane, you don’t need to know about its internal workings. The same is true about Java classes.
Ways to achieve abstraction in java:
- Abstract class (0 to 100%)
- Interface (100%)
- Abstract class in Java
When we declare a class as abstract, it is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method should be implemented. It can’t be instantiated.
Example:
//abstract parent class
Abstract class train {
//abstract method
public abstract void type ( ) ;
}
Public class vehicle extends train {
Public void type ( ) {
System.out.println (“ superfast train “ );
}
public Static void main ( String args [ ] ) {
train obj = new vehicle ( );
obj.type ();
}
}
output: superfast train
Encapsulation:
Encapsulation club together code and the data.
In encapsulation, there is a concept called data hiding which is defined as hiding the data of current class from other classes which is achieved by declaring the data as private and providing public getter setter methods to access the data.
Example:
class train {
private string Type ;
public setType(String type)
{
Type = type;
}
public getType()
{
return type;
}
}
class main {
public static void main(string args[])
{
train mytrain = new train();
mytrain.setType(“Superfast”)
system.out.println(“Train type is :” + mytrain.getType());
}
}
output: Train type is: Superfast
In this example, we declared a private field called Type that cannot be accessed outside of the class.
To access Type, we used public methods. These methods are called getter and setter methods. Making Type private allows us to restrict unauthorized access from outside the class. Hence this is called data hiding.
Inheritance:
Inheritance is defined as the process where one class acquires the properties (methods and fields) of another. It also supports hierarchical classification. The code and fields which are already present in the inherited class( base class or super class) can be used in the inheriting class.
Example:
class a extends b{ }
In the above example the super class is b which is inherited by the class a using extends keyword.
Various types of inheritance:
Single, multiple, hierarchical and multilevel inheritance.
Polymorphism:
It is derived from greek words poly (many) and morphs (many forms) which means many forms.
polymorphism in Java is of two types: compile-time polymorphism and runtime polymorphism. Compile time polymorphism is performed by method overloading and runtime by method overriding.
Method Overloading: many functions having the same name but different parameters.
In Runtime polymorphism overridden method is resolved at runtime rather than compile-time through the reference variable of a superclass.
This brings us to the end of the blog on OOPs concepts in Java. We hope that you were able to gain valuable insights from the same.