top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Let’s be strong in Java OOPS Concepts

Introduction

Object-Oriented Programming is one of the most important programming concepts in Java. It mainly focuses on implementing real-world entities. It is based on the concept of data (variables) and logic (methods). Here the data is encapsulated within objects so that no other part of the code can access the data except methods.


Object and Class

Object : It is an instance of a class that has properties and functions. They are real-world entities.

Example: Car

Class: It is a blueprint of objects. They define the properties and functionalities of the objects.

Example: BMW


Advantages of OOPS

1. Modularity: We can compartment the large program into smaller parts for easy understanding. It is similar to having many partitions like a kitchen, study room, etc. in a house, which provides security and reusability.

2. Reusability: Reusing the same function again and again in order to avoid duplication and reusing objects in different programs.

3. Maintainability: It is easy and simple to maintain and update the objects if there is any change in the future.

4. Flexibility: We can add new features to an existing code without modifying it.


OOPS Concepts

1. Inheritance

2. Abstraction

3. Polymorphism

4. Encapsulation


1.Inheritance

Inheritance means a class acquires the properties of another class. A class whose properties are inherited is known as the parent class and a class that inherits the properties of the parent class is known as the child class. Extends keyword is used to inherit a class.


Based on a parent class, a child class is defined. The child class extends from the parent class in order to inherit its properties.




In this example, my father extends my grandfather. He inherits the properties of my grandfather, so he has 2 houses. I extend my father, and I inherited the properties of my father and my grandfather as well. So I have 3 houses.


Types of Inheritance

1. Single Inheritance

In Single Inheritance, a child class extends a single-parent class.

2. Multi-level Inheritance

In Multi-level inheritance, a child class extends a parent class and the parent class extends another parent class.

3. Hierarchical Inheritance

In Hierarchical inheritance, multiple child classes extend a single-parent class.

4. Hybrid Inheritance

In Hybrid inheritance, two or more types of inheritance are combined.


Note:

· Java does not support multiple inheritances, but can be achieved by Interfaces concept.

· Object is the super most class. It is automatically inherited for every class. We don’t need to write it extensively.


Example:

Car.java


Honda.java


BMW.java


Truck.java


FordTruck.java

Vehicle.java

MyVehicle.java



Hierarchy


Honda and BMW extend Car. Car extends Vehicle. FordTruck extends Truck and Truck extends Vehicle.

When I create an object for Honda, I can inherit the properties from Honda as well as Car. But I cannot inherit the properties from Truck.


2. Abstraction

Abstraction is showing only the relevant information to the user and hiding unnecessary information. The car driver is only concerned about features like start the car, stop the car, accelerator, brake and etc. He/she is not much interested in the internal mechanism.


Any development starts with the design. Similarly, our test script should have a design before implementation. Here design is similar to Interface. Partial implementation is similar to the Abstract class. Full implementation is similar to the Concrete class.


We can achieve Abstraction through Interface. The interface has abstract methods and constants, but not the method body.


There are 2 ways to achieve abstraction. One is Interface, another is Abstract Class.


2.1 Interface

We can declare an interface using the keyword interface. All the methods in an interface should have an empty body. By default, all the fields are public, static, and final. These methods should be implemented by a class that implements the interface.


2.2 Abstract Class

We can declare an abstract class using the abstract keyword. It can have both abstract (not implemented) and non-abstract methods (already implemented).


Points to remember

1. It is not mandatory to implement an abstract class.

2. An Abstract class has both implemented and unimplemented methods.

3. Multiple inheritances can be achieved by using interfaces.

4. We cannot create an object for an interface, because the interface is a design.

5. A class extends another class. An interface extends another interface. A class implements an interface.

6. We cannot create an object for an abstract class, this is solved by creating an object for a concrete class.

7. An interface can have static and default methods since Java 8. It allows private methods since Java 9.


Example:

Interface Name : Mobile.interface


Abstract class name : Android.java



Concrete class name : OnePlus.java which extends Android

Class to call the concrete class: MyMobile.java

3. Polymorphism

Polymorphism means many forms of implementation, and it occurs when we have many classes that are related to each other by inheritance.


There are 2 types of Polymorphism. One is Overloading, another is Overriding.


3.1 Method Overloading

Method Overloading means that a class has multiple methods having the same name, same return type, and same modifier but different arguments (number of arguments, and data type).


3.2 Method Overriding

Method Overriding means that a child class method overrides the parent class method when a child class has the same method as declared in the parent class. Parent method signature should be the same as the child class. It works only with Inheritance. Using method overriding, we can override the old features with new features in a class.



4. Encapsulation

Encapsulation means wrapping up data under a single unit. This data-hiding concept prevents the data from being accessed by the code outside. This helps the data in a class hide from other classes which is achieved by making the members or methods of a class private. These private data can be set and get by public methods in the class.


Scenario: 1

When the variables are public, anyone can use or modify them. In the below example, anyone can change the creditCardNumber.

Scenario: 2

When the variables are private, it is hidden from other classes. It can be accessed by using getter and setter methods.

Getter – is used to providing read access.

Setter – is used to providing write access.

In the below example, the creditCardNumber is private. Anyone can read it, but cannot write it.




Conclusion

In this blog, we have covered the 4 pillars of Object Oriented Programming concepts. These concepts aim to implement real-world entities in programs.





























32 views0 comments

Recent Posts

See All
bottom of page